How to manage Jenkins remotely using CLI and SSH
Here in this article we will try to explore on the different options that are available to manage Jenkins instance remotely such as jenkins cli and ssh client.
Test Environment
- Docker 27.5.1
- Docker Compose v2.32.4
- Jenkins 2.504.3-lts-jdk17
Jenkins CLI
Jenkins CLI is a feature rich command line interface that helps jenkins users and administrators to manage jenkins from a script or shell environment. This can be very useful to run tasks such as creating, updating, listing or deleting jenkins jobs from a remote environment.
This command line CLI can be accessed over SSH or with the jenkins cli client which is a jar file distributed with jenkins.
If you are interested in watching the video. Here is the YouTube video on the same step by step procedure outlined below.
Procedure
As a first step ensure that you have docker and docker-compose installed on your system. You can following the official documentation from docker to install these tools.
admin@linuxser:~$ docker -v
Docker version 27.5.1, build 9f9e405
admin@linuxser:~$ docker compose version
Docker Compose version v2.32.4
Start up the docker services if its not already started.
admin@linuxser:~$ sudo systemctl start docker.service
Step2: Ensure Jenkins service is up and running
In this step we will be instantiating Jenkins services using docker compose file. Here is the minimalistic docker compose file that we will be using for this demo.
admin@fedser:jenkinscli-demo$ cat docker-compose.yml
services:
jenkins:
image: "jenkins/jenkins:2.504.3-lts-jdk17"
container_name: jenkins
ports:
- "8082:8080"
volumes:
- jenkins_home:/var/jenkins_home
networks:
- jenkins-net
networks:
jenkins-net:
volumes:
jenkins_home:
NOTE: Jenkins is configured to listen on port 8082 on host machine
Once the file is created, you can launch the jenkins service as shown below.
admin@fedser:jenkinscli-demo$ docker compose up -d
Ensure that you retrieve the initial password for the admin user that has been generated in the logs.
admin@fedser:jenkinscli-demo$ docker logs -f jenkins
...
*************************************************************
*************************************************************
*************************************************************
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:
e8ccb4b1b3624c899daabfa032d32665
This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
*************************************************************
*************************************************************
*************************************************************
Now you can try to access your jenkins portal available on port 8082 and initialize it by installing the suggested plugins and setup the admin user credentials for later access.
URL: http://fedser.stack.com:8082/
Step3: Enable Jenkins CLI access over SSH
By default, the Jenkins installation does not come with SSH service enabled, We need to install the following plugin “SSH server” to “Add SSH server functionality to Jenkins, exposing CLI commands through it.”

Now navigate to Manage Jenkins – Security and under the SSH server section, let’s enable SSH service on a fixed available port “52222” as shown below and save the settings.

With this we have jenkins server with SSH enabled on port 52222 along with Basic Authentication setup for “admin” user.
Step4: Download Jenkins CLI
Navigate to Manage Jenkins – Jenkins CLI to download the client jar distribution as shown below.

admin@fedser:jenkinscli-demo$ ls -ltr jenkins-cli.jar
-rw-r--r--. 1 admin admin 3743053 Nov 7 10:46 jenkins-cli.jar
Let’s try to connect to jenkins instance using the cli by passing the authentication credentials and list the tasks that can be carried out using the cli as shown below.
Step5: Connection using CLI
Here the connection that we are establishing with Jenkins instance is using the default WebSocket protocol.
admin@fedser:jenkinscli-demo$ java -jar jenkins-cli.jar -s http://fedser.stack.com:8082/ -auth admin:admin@1234 help
add-job-to-view
Adds jobs to view.
build
Builds a job, and optionally waits until its completion.
cancel-quiet-down
Cancel the effect of the "quiet-down" command.
clear-queue
Clears the build queue.
connect-node
Reconnect to a node(s)
...
NOTE: There are two additioan modes ie. http and ssh which can be used from within the jenkins cli as well
Step6: Add Public Key for SSH access
Generate your own SSH keypair for user “admin”. It will generate public and private key for your user. Ensure to keep your private key “admin_rsa” secured.
admin@fedser:jenkinscli-demo$ ssh-keygen -t rsa -f ./admin_rsa -C admin
admin@fedser:jenkinscli-demo$ ls -ltr admin_rsa*
-rw-r--r--. 1 admin admin 559 Nov 7 11:30 admin_rsa.pub
-rw-------. 1 admin admin 2590 Nov 7 11:30 admin_rsa
Let’s navigate to Manage Jenkins – Users and add the SSH public key (ie. admin_rsa.pub) content to the section shown below and save the changes.

Step7: Update Docker Compose to enable SSH port access
First let’s stop the jenkins instance without removing the persistant configuration stored locally.
admin@fedser:jenkinscli-demo$ docker compose down
Update the docker compose file to expose SSH port “52222” within container to Host port “52222” as shown below.
admin@fedser:jenkinscli-demo$ cat docker-compose.yml
services:
jenkins:
image: "jenkins/jenkins:2.504.3-lts-jdk17"
container_name: jenkins
ports:
- "8082:8080"
- "52222:52222"
volumes:
- jenkins_home:/var/jenkins_home
networks:
- jenkins-net
networks:
jenkins-net:
volumes:
jenkins_home:
Now it’s time to start up our container with the updated port configuration.
admin@fedser:jenkinscli-demo$ docker compose up -d
Step7: Connect using CLI over SSH
Now that we have enabled SSH server and configure the user with the public key, we should be able to access the Jenkins instance using the CLI over SSH as shown below.
Here is the command which try to jenkins with CLI over SSH and running the “who-am-i” task which shows the details of the current authenticated user.
admin@fedser:jenkinscli-demo$ ssh -i ./admin_rsa -l admin -p 52222 fedser.stack.com who-am-i
Authenticated as: admin
Authorities:
authenticated
Step8: Manage Jenkins Job
Let’s say we want to create a sample helloworld jenkins job using the below “hellojenkins.xml” file.
admin@fedser:jenkinscli-demo$ cat hellojenkins.xml
<?xml version='1.1' encoding='UTF-8'?>
<project>
<description>A sample Jenkins Freestyle project</description>
<keepDependencies>false</keepDependencies>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>echo "Hello from Jenkins!"
ls -l</command>
<label></label>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
</project>
Now let’s run the following command to “create-job” by passing the config from file “hellojenkins.xml” as standard input.
Using CLI over SSH
Create Jenkins Job
admin@fedser:jenkinscli-demo$ ssh -i ./admin_rsa -l admin -p 52222 fedser.stack.com create-job hellojenkins < hellojenkins.xml
Build Jenkins Job
admin@fedser:jenkinscli-demo$ ssh -i ./admin_rsa -l admin -p 52222 fedser.stack.com build hellojenkins
Print Console Output
admin@fedser:jenkinscli-demo$ ssh -i ./admin_rsa -l admin -p 52222 fedser.stack.com console hellojenkins
Started from command line by ha:////4LXnLFdeXzDVGfE67yREtcHhk/4UUJgM61NjvtnWtrTZAAAAlx+LCAAAAAAAAP9b85aBtbiIQTGjNKU4P08vOT+vOD8nVc83PyU1x6OyILUoJzMv2y+/JJUBAhiZGBgqihhk0NSjKDWzXb3RdlLBUSYGJk8GtpzUvPSSDB8G5tKinBIGIZ+sxLJE/ZzEvHT94JKizLx0a6BxUmjGOUNodHsLgAzWEgZu/dLi1CL9xJTczDwAj6GcLcAAAAA=admin
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/hellojenkins
[hellojenkins] $ /bin/sh -xe /tmp/jenkins9405560189842894629.sh
+ echo Hello from Jenkins!
Hello from Jenkins!
+ ls -l
total 0
Finished: SUCCESS
Delete Jenkiins Job
admin@fedser:jenkinscli-demo$ ssh -i ./admin_rsa -l admin -p 52222 fedser.stack.com delete-job hellojenkins
Using CLI
Validate Authenticated User
admin@fedser:jenkinscli-demo$ java -jar jenkins-cli.jar -s http://fedser.stack.com:8082/ -auth admin:admin@1234 who-am-i
Authenticated as: admin
Authorities:
authenticated
Create Jenkins Job
admin@fedser:jenkinscli-demo$ java -jar jenkins-cli.jar -s http://fedser.stack.com:8082/ -auth admin:admin@1234 create-job hellojenkins < hellojenkins.xml
Build Jenkins Job
admin@fedser:jenkinscli-demo$ java -jar jenkins-cli.jar -s http://fedser.stack.com:8082/ -auth admin:admin@1234 build hellojenkins
Here are the screenshots of create and build job pages.


Hope you enjoyed reading this article. Thank you..
Leave a Reply
You must be logged in to post a comment.