Configuring SSH connection to a remote host in Jenkins (SSH-plugin)

Abhishek Attri
3 min readDec 7, 2020

--

Image source: Naresh Rayapati @ https://www.jenkins.io/

SSH is the recommended way to jump into a remote host for the sake of security and easiness. We’ll use the Jenkins SSH plugin to connect to a remote host and execute shell commands.

Part-1: Setting up the remote host using docker (Skip this step if you already have a remote server running)

  • Generate SSH key-pair for the remote_user(namely remote_key). Keep the passphrase blank for now.
ssh-keygen -t rsa -m PEM -f remote_key
  • Generate the Dockerfile for the centos-7 remote host. This will copy the public key generated above to the ~/.ssh directory with appropriate permissions.
  • Here’s the docker-compose file to maintain the Jenkins and remote-host service locally. These two services share the same network ‘net’. You can ignore the Jenkins service if not running locally.
  • Bring up the Jenkins and remote server remote-host
$ docker-compose build
$ docker-compose up -d
  • Check for the running containers using docker ps, whereas Jenkins can be accessed @localhost:8080
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
0ca1b7f2ae55        remote-host         "/bin/sh -c '/usr/sb…"   46 minutes ago      Up 46 minutes                                           remote-host
4a3f39c10d5b        jenkins/jenkins     "/sbin/tini -- /usr/…"   2 hours ago         Up 56 minutes       0.0.0.0:8080->8080/tcp, 50000/tcp   jenkins

Part-2: Configuring SSH plugin and establishing the connection to the remote host

  • Log into Jenkins as the admin user
  • Installing the SSH plugin:
    Navigate to Jenkins Dashboard> Manage Jenkins > Manage Plugins and install the SSH plugin. Check the ‘Restart Jenkins once the plugin is installed’ option.
  • Configuring the key for remote-user:
    Navigate to Jenkins Dashboard> Manage Jenkins > Manage Credentials to configure the SSH private key for the remote user. This user will be configured in Jenkins and can be used by the Jenkins builds.
    Enter the private key content as shown below.
  • Configuring the remote host to be connected via SSH:
    Navigate to Jenkins Dashboard> Manage Jenkins > Configure System. Add this remote host with the same name mentioned in the docker-compose file and 22 as the default SSH port. Use the SSH credentials configured in the previous step to check the connection. It should prompt you to be successful!
  • Testing the remote-host connection in the Jenkins build:
    Select ‘execute shell script on the remote host using SSH’ as one of the build steps. Note that we are executing the same echo command in the Jenkins node/server and the remote host. The host identifier is pre-appended in the beginning.

The console output contains both the commands that run on the servers.

Thank you for taking the time to read my article! I hope you found it informative and valuable. If you have any feedback, comments, or questions, please feel free to reach out 😄
I look forward to sharing more content with you in the future! 🤘

--

--

Abhishek Attri
Abhishek Attri

Written by Abhishek Attri

Animal lover ❤️ Technology enthusiast 🖥️ Always fascinated by kindness | Glad to connect! 🤗 https://www.linkedin.com/in/abhishek-attri

No responses yet