☑️Day 55: Jenkins Job for Remote Server Management with SSH🚀

Today’s journey focused on Jenkins job setup for remote server tasks, involving two essential steps: connecting two virtual machines (VMs) through SSH and establishing a secure, password-less connection using SSH keys. Here’s a comprehensive look into the process, complete with commands and real-world applications.


1. Setting Up Two VMs

  • Created two virtual machines, VM1 (Jenkins server) and VM2 (Remote server).

  • Purpose: Use Jenkins on VM1 to control tasks on VM2, a typical DevOps setup where Jenkins runs tasks across distributed systems.

2. Connecting VMs via SSH

  • SSH (Secure Shell) enables secure access to a remote machine over a network.

  • Commands:

    • On VM1:

        ssh username@VM2_IP_address
      
    • Enter the password for the initial setup.

  • This basic setup is crucial but has limitations due to the requirement of entering passwords for each session, which isn’t efficient in automation workflows.

3. Setting Up SSH Key-Based Authentication (Password-less Login)

  • To enable Jenkins to perform tasks on VM2 without requiring a password, we set up SSH keys.

  • Generating SSH Key Pair on VM1:

      cssCopy codessh-keygen -t rsa -b 2048
    
    • This command generates a private key (id_rsa) and a public key (id_rsa.pub) stored in ~/.ssh/ directory.
  • Copying the Public Key to VM2:

      sqlCopy codessh-copy-id username@VM2_IP_address
    
    • This command installs the public key on VM2, granting VM1 password-less access to VM2.
  • Verification:

    • On VM1:

        ssh VM2_IP_address
      
    • You should now be able to connect without entering a password.

  • Real-World Use: Automated scripts and Jenkins jobs can now execute commands or deploy files across multiple servers securely and efficiently, eliminating manual password input.

4. Jenkins Job Setup for Remote Commands on VM2

  • Purpose: To set up a Jenkins job that performs actions on a remote server (VM2) using SSH.

  • Steps:

    • In Jenkins: Create a new job (Freestyle or Pipeline) and configure it to run SSH commands on VM2.

    • Use the SSH plugin or a script to run commands like mkdir or ls on the remote server.

  • Example:

    • In the Jenkins job, add a build step and enter commands such as:

        cssCopy codessh username@VM2_IP_address 'echo "Hello from VM1!" && mkdir /remote-directory && touch /remote-directory/file.txt'
      

5. Transferring Files between VM1 and VM2

  • Using SCP (Secure Copy Protocol):

    • Copy files securely between servers, helpful for deployment and backup tasks.

    • Example Command:

        rubyCopy codescp /path/to/local/file username@VM2_IP_address:/path/to/remote/destination
      
  • Real-World Use: Automatically transfer configuration files, backups, or scripts between Jenkins and remote servers as part of CI/CD.

6. Executing Remote Commands via Jenkins

  • Tasks like file management, service restarts, and log fetching can now be automated remotely.

  • Example Commands in Jenkins Job:

      javaCopy codessh username@VM2_IP_address 'sudo systemctl restart nginx'
      ssh username@VM2_IP_address 'cat /var/log/nginx/access.log'
    

7. Real-Time Scenarios for Jenkins SSH Setup

  • Deployments: Jenkins can push code, configurations, or updates to production servers without human intervention.

  • Automated Backups: Schedule jobs that transfer data from application servers to backup storage.

  • Monitoring and Alerts: Fetch logs or metrics from distributed servers and trigger alerts in Jenkins.

8. Commands Recap

  • Update packages:

      sudo apt update
    
  • SSH into remote server:

      ssh username@VM_IP
    
  • Generate SSH keys:

      ssh-keygen -t rsa -b 2048
    
  • Copy public key:

      ssh-copy-id username@VM_IP
    
  • Secure file transfer:

      scp /local/file username@VM_IP:/remote/path
    

Setting up remote connections through Jenkins has added significant efficiency to my automation workflow, enabling direct management of remote environments. The ability to automate deployments, backups, and monitoring on distributed servers opens up powerful DevOps possibilities!

🚀Thanks for joining me on Day 54! Let’s keep learning and growing together!

Happy Learning! 😊

#90DaysOfDevOps

💡
Follow for more updates on LinkedIn , Github and Twitter(X)
10
Subscribe to my newsletter

Read articles from Kedar Pattanshetti directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kedar Pattanshetti
Kedar Pattanshetti