Skip to main content

Command Palette

Search for a command to run...

Part 3: Jenkins + SSH Remote Execution

SSH Automating agent

Published
5 min read
Part 3: Jenkins + SSH Remote Execution

Jenkins CI/CD Series

Part Article
1 Install Jenkins with Docker
2 Creating Your First Jenkins Pipeline
3 Jenkins SSH Remote Execution
4 Automating MySQL Backup to AWS S3
5 Making Jenkins Automation Scalable
6 Running Ansible from Jenkins
7 Jenkins Pipeline from GitHub
8 Multibranch Pipelines
9 Parameterized Pipelines
10 Environment Variables in Pipelines

Goal

The goal of this project is to extend the Jenkins environment created in Part 1 by integrating it with a remote server through SSH (Secure Shell). In this exercise, we will create a container that acts as a remote host with an SSH server and configure Jenkins to securely connect to it.

By the end of this project, Jenkins will be able to execute commands and scripts on a remote machine, enabling automation tasks such as running scripts, managing servers, and performing deployment-related operations.


Purpose

The purpose of this exercise is to demonstrate how Jenkins can interact with external systems through SSH. In real-world DevOps environments, Jenkins often needs to run commands on remote machines to perform operations such as application deployment, server configuration, and maintenance tasks.

By creating a dedicated SSH-enabled container and integrating it with Jenkins, we simulate a typical automation scenario where Jenkins acts as the orchestration tool while executing commands on remote infrastructure.

This setup helps us to understand how Jenkins communicates with remote systems securely using SSH keys and credentials, which is a fundamental concept in building automated CI/CD pipelines.


Prerequisites

Before starting this project, ensure the following requirements are completed:

  • Ready to use Host and the directory structure to run Dockerfiles and docker-compose.yml (Refer to Part 1)

  • Access to the Jenkins dashboard with administrative privileges.


Step-by-Step Implementation

The purpose of the SSH server is to run shell commands or scripts on other servers

The directory should look like this:

Note- Since we are using Docker, we will use Dockerfiles or images available on Docker Hub.

Create Containers

Jenkins - will download the latest image from Docker Hub
SSH server - Customized Dockerfile (name: remote-host)

  • Create a Dockerfile in the directory jenkins/jenkins_home/centos7

Note- centos7 is just random directory name I used

Dockerfile

FROM rockylinux:9

RUN dnf install -y openssh-server passwd sudo && dnf clean all

RUN useradd -m remote_user && \
    echo "remote_user:1234" | chpasswd

RUN mkdir -p /home/remote_user/.ssh

COPY remote-key.pub /home/remote_user/.ssh/authorized_keys

RUN chown -R remote_user:remote_user /home/remote_user/.ssh && \
    chmod 700 /home/remote_user/.ssh && \
    chmod 600 /home/remote_user/.ssh/authorized_keys

RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config

RUN ssh-keygen -A

EXPOSE 22

CMD ["/usr/sbin/sshd","-D"]
  • The YAML will create two containers, and I have placed it at jenkins/jenkins_home/

docker-compose.yml

services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins
    ports:
      - "8080:8080"
    volumes:
      - $PWD/jenkins_home:/var/jenkins_home
    networks:
      - net
  remote_host:
    container_name: remote-host
    image: remote-host
    build:
      context: centos7
    networks:
      - net
networks:
  net:
  • Build the remote-host image for the SSH server
docker compose build
  • Verify Image
docker images
  • Create containers by using the docker-compose.yml file from jenkins/jenkins_home/
docker compose up -d
  • Verify containers with docker ps
  • Log in to Jenkins

Note - host_ip:8080

By using the credentials you created (Refer First part of this article if needed)

  • Go to Manage Jenkins--> Plugins and install the SSH plugin
  • Go to Manage Jenkins --> Credentials --> SSH Username with Private key
  • Add username remote-user and copy the private key as a remote key below

Note- This is created part of docker compose and placed in container context (centos: it will act like persistant volume for remote-host container)

  • Go to Manage Jenkins --> SSH Servere and add details of our SSH server and Check connection

here: Integration completed

  • Let's test by creating a Freestyle project and testing
  • Go to Build Steps and add Execute shell script on remote host using SSH
  • Select our user details from the drop-down,
  • Verify if this Job Runs the command on the remote-host and creates a file

Click Build now and check console output from Job number

  • Logs
  • Verify if the file has been created or not on the remote-host

Done!!!


Conclusion

In this part of the series, we successfully created a remote server container running an SSH service and integrated it with Jenkins. We configured Jenkins to securely connect to the remote host using SSH credentials and executed commands on the remote system through a Jenkins job.

This setup demonstrates how Jenkins can control external infrastructure and automate tasks beyond its own environment. Remote command execution is widely used in DevOps pipelines for tasks such as application deployment, server maintenance, configuration management, and system monitoring.

With this integration in place, Jenkins is now capable of orchestrating tasks across multiple systems, which is a critical capability for modern CI/CD automation.

In the next part of the series, we will build on this foundation by creating a real-world automation workflow where Jenkins performs a MySQL database backup and uploads it to AWS S3 automatically.


🔗 Continue the Series

⬅️ Previous Article: Part 2 Creating Your First Jenkins Pipeline
➡️ Next Article: Part 4 Automating MySQL Backup to AWS S3


⭐ If you found this article useful, follow https://ask-abhi.com for more DevOps tutorials.