How to Install Jenkins on Ubuntu 24.04 in AWS EC2 Instance? (A Step-by-Step Guide)
Jenkins is a Continuous Integration/Continuous Delivery or Deployment (CI/CD)tool in DevOps. Jenkins is developed in the “Java” language. It implements the CI/CD workflows using pipelines or automation.
Jenkins works on “Master-Slave” architecture. This means you have to set up two nodes named “Master” & “Slave”. On the “Master” node, you will configure “Jenkins” with “Java”, while on “Slave”, you have to install “Java” only.
While “Ubuntu” is an open-source operating system based on Debian Linux that runs from the desktop or any cloud platform such as “AWS”, “Google Cloud Platform” and “Microsoft Azure”. Ubuntu is used because it is easy to use, has a better user experience & has incredible support for hardware tools.
The latest version of Ubuntu is 24.04, which is the latest version & released in “April 2024”. In this blog post, we will provide you with a detailed guide, on how to install Jenkins on Ubuntu 24.04 in AWS EC2 Instance with Master-Slave architecture.
To know about, how to setup Jenkins on Amazon Linux in EC2 Instance, check the Jenkins Assignment 1 given below:
A. Create Two Amazon EC2 Instances Named As Jenkins Master & Jenkins Slave
Step 1: Go to the “Services” section & search “EC2” here. Put the cursor over “EC2” & click on “Instances”.
Step 2: Click on “Launch Instance”.
Step 3: Choose “Name” as “Jenkins-“ in the “Name and tags” section.
Step 4: Choose “AMI” as “ubuntu”.
Step 5: Choose “Instance type” as “t2.micro”.
Step 6: Choose “key pair (login)” as “Jenkins2”.
Step 7: In “Network settings”, choose “Firewall (security groups)” as “Select existing security group”. While we have chosen “launch-wizard-9” as the “Common security groups”.
Step 8: In “Summary”, write “Number of Instances” as “2”. Click on “Launch Instance”.
Step 9: Two Instances will be successfully launched. Click on “Instances” in “Breadcrumbs”.
Step 10: Rename the First “Instances” as “Jenkins-Master” & second one as “Jenkins-Slave”.
b. Install “Jenkins” Over the Master (Jenkins-Master) Machine
Step 1: Select the “Jenkins-Master” & click on “Connect”.
Step 2: Click on “Connect” Again.
Step 3: The “Jenkins-Master” machine will be opened. Run this command to update the machine:
sudo apt-get update
Step 4: Now, we will install Jenkins on the “Ubuntu” machine. Run this given command:
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian/jenkins.io-2023.key
Press “Enter” from the keyboard to run this command.
Jenkins Repo will be configured on the machine.
Step 5: Now, append the Debian package repository to the server’s sources.list.
Run this given command:
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
https://pkg.jenkins.io/debian binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
Press “Enter” from the keyboard to run this command.
Step 6: Run this command to install the “java” on “Ubuntu Machine”:
sudo apt install fontconfig openjdk-17-jre
Press “Enter” from the keyboard to run this command.
Press “Y” to execute the “Openjdk “ installation.
“Java Package” will be successfully installed.
Step 7: First, update the machine & run the below-given commands to update & install the “Jenkins” over the “ubuntu machine.”:
sudo apt-get update
sudo apt-get install jenkins -y
Press “Enter” from the keyboard to run these commands.
Step 8: Now, enable the Jenkins to start the boot. Run the given command:
sudo systemctl enable jenkins
Press “Enter” from the keyboard to run this command.
Step 9: Now, start the “Jenkins” as a service:
sudo systemctl start jenkins
Press “Enter” from the keyboard to run this command.
Step 10: Now, check the “Jenkins service” status using this command:
sudo systemctl status jenkins
Press “Enter” from the keyboard to run this command.
Step 11: Copy the “Public IP” of “Jenkins-Master”. Put in the browser address bar with port no 8080 [http://3.15.3.52:8080/]. The “Unlock Jenkins” option will be shown.
Copy the given command.
Step 12: Put the above-given command with sudo cat in “Jenkins-Master”.
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Press “Enter” from the keyboard.
A token will be given. Copy this token:
3bdc19525af24e9f97b0d6690e2e3b41
Step 13: Paste the given token in the “Administrator password” section. Click on “Continue”.
Step 14: Click on “Install Suggested Plugins”.
Step 15: Plugin will be started installing like this:
Step 16: Now, create your admin user. Put the following entries here:
Username: — admin
Password: — admin
Confirm Password: — admin
Full Name: — admin
E-mail Address: — admin@admin.com
Click on “Save and Continue”.
Step 17: Jenkins has been successfully installed. A configuration URL will be given. Click on “Save and Finish”.
Step 18: Click on “Start using Jenkins”.
Step 19: Your “Jenkins Dashboard” setup will be successfully done.
c. Install “Java” Over Slave (Jenkins-Slave) Machine
Step 1: Select the “Jenkins-Slave” & click on “Connect”.
Step 2: Click on “Connect” Again.
Step 3: The “Jenkins-Slave” machine will be opened. Run this command to update the machine:
sudo apt-get update
Step 4: Run the command to install the “java” on “Ubuntu”.
sudo apt install fontconfig openjdk-17-jre
Press “Enter” from the keyboard to run this command.
Type “Y” to install java.
“Java Package” will be successfully installed.
Step 5: Run this command to check the Java version:
java --version
d. Create Public & Private Key on “Jenkins-Master” to Add the “Slave” Machine
Step 1: First, go to the .ssh directory using this command: cd .ssh/
You will go to the “ssh” directory.
Step 2: Run this command to create “public” & “private” keys:
ssh-keygen
Press “Enter” from the keyboard & it will start generating the keys. Press “Enter” from the keyboard three times.
Your public key (id_ed25519.pub) & private key (id_ed25519) will be generated.
Step 3: Run this command to find the public key:
sudo cat id_ed25519.pub
Your “public key” content will be shown here. Copy the content of the keys.
e. Paste the Public Key (id_ed25519.pub) in “authorized_keys” file
Step 1: Go to the “Jenkins-Slave” machine. Run this command to go inside the ssh directory.
cd .ssh/
You will land inside the ssh directory.
Step 2: Do “ls” & “authorized_keys” will be displayed here in the .ssh directory.
Step 3: To edit the “authorized_keys”, use this command to open the authorized_keys file:
vim authorized_keys
Step 4: Paste the “id_ed25519.pub” content here.
Step 5: Press “ESC” from the keyboard & type this command to save & quit the “authorized_keys” file.
wq!
Step 6: Your “authorized_keys” file will be successfully saved.
f. Add a Node in Jenkins-Master using “Private Key”
Step 1: Click on “Manage Jenkins”.
Step 2: Click on “Nodes”.
Step 3: Click on “New Node”.
Step 4: Choose “Node name” as “Slave-1” & “Type” as “Permanent Agent”. Click on “Create”.
Step 5: Put the following entries here:
Name: Slave1
Description: This is a slave machine.
Number of executors: 1
Choose where you want to save the “Jenkins” data. We have installed Jenkins over “ubuntu”.
While choosing “label” as “slave”. Because Jenkins identifies the node using the “label” feature.
Usage will remain as it is.
Choose “Launch Method” as “Launch Agents via SSH”.
Host: — 172.31.11.164
Click on “Add” in “Credentials”.
Click on “Jenkins”.
Choose the following entries here:
Domain: — Global credentials (unrestricted) — Remain as it is.
Kind: — SSH username with private key
Scope: — Global (Jenkins, nodes, items, all child items, etc)
Id & Description: — pwdless
Username: — ubuntu
Choose “Enter Directly” in “Private Key”.
Click on “Add”.
Paste the “Private Key” from “Master” here.
Go to “Jenkins-Master” & run this command: cat id_ed25519.
Copy the private key from here.
Go to “Jenkins” & paste the private key in the “Private Key” section.
Click on “Add”.
Choose “Credentials” as “ubuntu (pwdless)”.
While choosing “Host Key Verification Strategy” as “Non verifying Verification Strategy”.
Click on “Save”.
Step 6: Agent “Slave-1” has been successfully created in “Jenkins” using the “passwordless authentication”.
g. If the Private Key Method Does not Work, Use .pem Key Method
All steps remain the same as a private key, you have to add .pem key content in the private section. Most of the time, the above-given method doesn’t work.
In this method, you don’t need to add public & private keys to the authorized_keys section. Ignore the previous method, if it doesn't work.
Step 1: Click on “Manage Jenkins”.
Step 2: Click on “Nodes”.
Step 3: Click on “New Node”.
Step 4: Choose “Node name” as “Slave-1” & “Type” as “Permanent Agent”. Click on “Create”.
Step 5: Put the following entries here:
Name: Slave1
Description: This is a slave machine.
Number of executors: 1
Choose where you want to save the “Jenkins” data. We have installed Jenkins over “ubuntu”.
While choosing “label” as “slave”. Because Jenkins identifies the node using the “label” feature.
Usage will remain as it is.
Choose “Launch Method” as “Launch Agents via SSH”.
Host: — 172.31.11.164
Click on “Add” in “Credentials”.
Click on “Jenkins”.
Choose the following entries here:
Domain: — Global credentials (unrestricted) — Remain as it is.
Kind: — SSH username with private key
Scope: — Global (Jenkins, nodes, items, all child items, etc)
Id & Description: — pwdlesss
Username: — ubuntu
Choose “Enter Directly” in “Private Key”.
Click on “Add”.
Copy the “Jenkins2.pem” content from here.
Paste the .pem key in “Enter Directly” in the “Private Key” section.
Choose “Credentials” as “ubuntu (pwdlesss)”.
While choosing “Host Key Verification Strategy” as “Non verifying Verification Strategy”.
Click on “Save”.
Step 6: Agent “Slave-1” has been successfully created in “Jenkins” using the “passwordless authentication”.
h. Verifying Node is Created or Not
Condition: If the “slave-1” is successfully “SSH” from “Jenkins-Master” then it’s verified that “Slave-1” is successfully created because “Slave-1” has the “Slave Private IP Address”.
Step 1: Go to Master and type this command: ssh slave private IP address
ssh 172.31.11.164
Press “Enter” from the keyboard.
Type “Yes” & press “Enter” from the keyboard.
Your “Jenkins Slave” has been successfully accessed from “Jenkins Master”.
More Jenkins Resources:
Integration of DevOps Tools with Jenkins — Jenkins Case Study
Add 2 Nodes in Jenkins & Made Push to Branches & Pull Content to Git Folder — Jenkins Assignment 2