Installing Jenkins in Ubuntu 22.04 LTS and some admin tasks

Overview
Jenkins is a highly popular open-source web application designed to streamline and automate the software development process. It's a cornerstone tool for Continuous Integration (CI) and Continuous Delivery (CD) pipelines, allowing developers and DevOps teams to build, test, and deploy applications efficiently..
Installing Jenkins
There are many methods to install Jenkins in Ubuntu. Before we go to install Jenkins, we need to have already installed Java JRE (Java Runtime Environment). OpenJDK is an open-source implementation of the Java Platform It provides the core components necessary for running Java applications.
We will work on Ubuntu-22.04 version:

Let’s install OpenJDK-11 release. The steps to install this distribution in our system via putty interface. We going to download the file OpenJDK-11 as archived file from the official website [https://jdk.java.net/archive/] locally,

Then via WinSCP app we can access our Linux (Ubuntu) environment to transfer it as Binary file.

After we have the archive file in our environment, we need first to extract it via cli.
tar -xvzf openjdk-17.0.1_linux-x64_bin.tar.gz

As we have the file in just read and write permission, we are going to give it the execute permission like:

Then when we list, it gives:

Jetzt können wir OpenJDK-17 mit dem folgenden Befehl installieren:
sudo apt install openjdk-17-jdk -y

Da jedoch OpenJDK-17 bereits in meiner Umgebung installiert ist, wird die neueste Version angezeigt.

After Installation we check if the Jenkins service is active.

root@master:/home/bekber1# systemctl
status jenkins
● jenkins.service
- Jenkins Continuous Integration Server
Loaded: loaded (/lib/systemd/system/jenkins.service; enabled; vendor
preset: enabled)
Active: active (running) since Tue 2024-09-03 06:34:13 UTC; 2h 58min
ago
Main PID: 18413 (java)
Tasks: 63 (limit: 2178)
Memory: 791.1M
CGroup: /system.slice/jenkins.service
└─18413 /usr/bin/java
-Djava.awt.headless=true -jar /usr/share/java/jenkins.war
--webroot=/var/cache/jenkins/war --httpPort=xxx
So, we can see that Jenkins service is Active. Now to be able to access the Jenkins from the web browser we need to setting up the default UFW firewall.
Open port 8080 by running the following commands:
root@master:/home/bekber1#
ufw allow 8080
Rule
added
Rule
added (v6)
root@master:/home/bekber1#
ufw status
Status:
active

In the putty cli we use the command to get the admin password:

We copy the password provided by default and paste it in the filed shown in the picture above and then click Continue button. After this we getting started:

After the installation and configuration, we must create our first Admin user as show in the picture below:

Now we can click Save and Continue and then getting the Jenkins URL as shown below:

Now Jenkins is ready, and we can start and using it.



How to change Jenkins Administrator password if we lost it:
If we forget the Jenkins Admin password for web browser, to reset it we should follow the steps as below:
First, we need to find where is the config.xml file is located in:
root@master:/home/bekber1# cd /var/lib/jenkins/
First, we need to find where is the config.xml file is located in:
root@master:/var/lib/jenkins# vim config.xml
So, concatonating this file in cli you can see inside the line named: "<useSecurity>true</useSecurity>"
We change it fromtruetofalseand we restart the Jenkins service afterwards like:
systemctl restart jenkins
After these steps we refresh the web browser page and we will get inside the Jenkins page to make the changes because it is unsecured.
First step is to choose theManage Jenkinsand after we click onSecurity Realm“ „Jenkins‘ own user database. We save it and close.
After this we can go to the underManage Jenkins“ -> „Security“ -> „Userswe click onSettingsclick and underPasswordwe change the password.
How-create-agent-for-built-in-node
In Jenkins, agents are essential for executing build jobs. Steps that we should follow are:
We have to configuring .ssh inside the /var/lib/jenkins/.ssh/known_host
- we create the folder named .ssh
root@master:/var/lib/jenkins#mkdir -p /var/lib/jenkins/.ssh
root@master:/var/lib/jenkins# chown -R jenkins:jenkins .ssh
root@master:/var/lib/jenkins#ssh-keyscan -H agent1 >> /var/lib/jenkins/.ssh/known_host
If doesn't work as e.g.
root@master:/etc/ssh# ssh-keyscan -H agent1 >> /var/lib/jenkins/.ssh/known_hosts
getaddrinfo agent1: Temporary failure in name resolution
getaddrinfo agent1: Temporary failure in name resolution
getaddrinfo agent1: Temporary failure in name resolution
getaddrinfo agent1: Temporary failure in name resolution
getaddrinfo agent1: Temporary failure in name resolution
we need to add agent1 server ip in the /etc/hosts
-Datei hinzufügen.
To find the agent1 ip address, we use the command:
root@master:/etc/ssh# dig agent1
; <<>> DiG 9.16.48-Ubuntu <<>> agent1
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 15329
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;agent1. IN A
;; Query time: 0 msec
;; SERVER: <MyIpAddress>#53(<MyIpAddress>)
;; WHEN: Sun Mar 10 10:33:06 UTC 2024
;; MSG SIZE rcvd: 35
and edit the /etc/hosts with the nano editor like:
root@master:/etc# cat hosts
<MyIpAddress>localhost
<MyIpAddress>linux-0
<MyIpAddress>master
<MyIpAddress>node1
<MyIpAddress>node2
<MyIpAddress> agent1 ----------> mit dem Nano- oder Vim-Editor müssen wir die IP-Adresse von agent1 hinzufügen.
/etc/hosts
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
Nach Ausführung des Befehls ssh-keyscan -H agent1 >> /var/lib/jenkins/.ssh/known_hosts erhalten wir die Auflösung:
root@master:/etc# ssh-keyscan -H agent1 >> /var/lib/jenkins/.ssh/known_hosts
# agent1:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.11
# agent1:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.11
# agent1:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.11
# agent1:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.11
# agent1:22 SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.11
Jenkins agent1 in port 22 is resolved:
root@master:/etc# cd /var/lib/jenkins/.ssh/
root@master:/var/lib/jenkins/.ssh# ll
total 12
drwxr-xr-x 2 jenkins jenkins 4096 Mar 10 10:11 ./
drwxr-xr-x 15 jenkins jenkins 4096 Mar 10 10:05 ../
-rw-r--r-- 1 jenkins jenkins 978 Mar 10 10:36 known_hosts
root@master:/var/lib/jenkins/.ssh# cat known_hosts
|1|UMnpRmZeDMTxf8bAmPYanyzeE54=|C7tpVgwzVbu7FDJG4fUiDVN3L4I= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAunO9x3A5IInM2Qqqf4wwWBEokfH8pRiMVcrjNTNn7hCZrgi75hbRbV+dG8hy2xhUtDlywP0k0S/+qpSwoVd7E=
|1|nPGx2tNNjsRAU6ZYf7UqlI1iGcM=|TqWQT18KAS56FgREOVEfVUvvILQ= ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDTy0gPfw8tQ7T2VkFlp82regznVnrZx7tlsgLqlOWVqzhCPrA4/V0DlOsHRjDKUFNnrH+LgOSSAmZwqRrHYyy6Oe+BkmXU4v5a8oZPA1xYRSL/Q1YDYhiMeySilDDzUQwy6HqcmlpPHdPFsl/S/vQIoCAlP5PbS9/GYXILJJj2bxVjqq/nWeC3QywDsoch02XxPpQORk3qFPKDAbc8AWn26v/zHXFgjEig0vW+el1gjkKe8scNTvjVhRot31gD3SdM39SpEspD+Unkw0EhI60hnQeZjiunRUM9/Za2sw3cFGOCXOySDPrkZ2mm10geTZVDF4JTm+pdsv8cmLOUSooRWg+t8R6MK7ZDmeW1aRScpy5R8je0fuGU7XX/P+RNs27TFJQevE0EwWcTYMtBMQunuuqGFBVUBIOMZzwKeipojx9mGXhDp+TiHsQRXq/9nIOdiDfrub658RUJBgZsaX7fAsnnMMbjf0qBSXdikNoQHm+SrvxI4I7L7XawidSqWkU=
|1|1JIhNCOumg+yjaTgMMYtcGAkC6o=|B/ZgF81ZU+t+8AbByBU3hwp68X0= ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFLr95lmAUl1k0+zxfhzG4B9fvrvPtddpxOCII+abRfO
root@master:/var/lib/jenkins/.ssh#
Jenkins-Logs:
Warning: no key algorithms provided; JENKINS-42959 disabled
SSHLauncher{host='agent1', port=22, credentialsId='linux-0', jvmOptions='', javaPath='', prefixStartSlaveCmd='', suffixStartSlaveCmd='', launchTimeoutSeconds=60, maxNumRetries=10, retryWaitTime=15, sshHostKeyVerificationStrategy=hudson.plugins.sshslaves.verifiers.KnownHostsFileKeyVerificationStrategy, tcpNoDelay=true, trackCredentials=true}
[03/10/24 10:43:30] [SSH] Opening SSH connection to agent1:22.
Searching for agent1 in /var/lib/jenkins/.ssh/known_hosts
Searching for agent1:22 in /var/lib/jenkins/.ssh/known_hosts
[03/10/24 10:43:30] [SSH] SSH host key matches key in Known Hosts file. Connection will be allowed.
[03/10/24 10:43:31] [SSH] Authentication successful.
[03/10/24 10:43:32] [SSH] The remote user's environment is:
BASH=/usr/bin/bash
.
.
.
<===[JENKINS REMOTING CAPACITY]===>channel started
Remoting version: 3206.vb_15dcf73f6a_9
Launcher: SSHLauncher
Communication Protocol: Standard in/out
This is a Unix agent
Agent successfully connected and online
Conclusion
Jenkins is a valuable tool for automating software development processes. By understanding its core features, installation, configuration, security, and agent management, you can effectively leverage Jenkins to improve your CI/CD pipelines and deliver high-quality software.
Referenz:
https://community.jenkins.io/t/can-i-create-agent-for-built-in-node/1183