Automate secure ssh login to remote servers for instant and effortless connectivitySave time and automate secure SSH login to remote servers for instant and effortless connectivity. With this guide, we intend to achieve reduced time and effort to access remote command execution and file transfers.

For a quick-start SSH Public Key Authentication guide, check out:

Pre-requisites

Before we can show you how to automate secure SSH login to remote servers, we must ensure the following environment conditions are met:

After confirming the above pre-requisites are met, you are ready to learn how to automate secure SSH login to remote servers for effortless and instant access! Let’s get started.
Launch 100% ssd vps from $2. 49/mo!

Automatically Connect to a Remote Server Using SSH Public Key Authentication

To connect to a remote system automatically using SSH public key authentication, follow these steps:

  1. Generate an SSH Key Pair (If Not Already Created)

    Run the following command on your local system to generate an SSH key pair:

    ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -N ""
    
    • This generates a 4096-bit RSA key pair.
    • The private key is stored in ~/.ssh/id_rsa (DO NOT share this file).
    • The public key is stored in ~/.ssh/id_rsa.pub.
  2. Copy the Public Key to the Remote Server

    Use the following command to copy your public key to the remote system:

    ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote_host
    

    Replace:

    • user with your remote system’s username.
    • remote_host with the IP address or hostname of the remote server.

    If ssh-copy-id is not available, you can manually copy the key:

    cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
    

    Then set the correct permissions on the remote server:

    ssh user@remote_host "chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"
    
  3. Test the SSH Connection

    Now, you should be able to log in without a password:

    ssh user@remote_host
  4. Automate SSH Login with an Alias (Optional)

    You can create an alias in ~/.ssh/config to simplify access:

    nano ~/.ssh/config

    Add the following entry:

    Host myserver HostName remote_host User user IdentityFile ~/.ssh/id_rsa

    Now, you can connect simply by running:

    ssh myserver
  5. Automate SSH Login in a Script

    To automate SSH login in a script, use:

    #!/bin/bash ssh -i ~/.ssh/id_rsa user@remote_host "your-command-here"

    Example: Automatically run a command on the remote server:

    ssh -i ~/.ssh/id_rsa user@remote_host "uptime"

    If you need to run a script remotely:

    ssh -i ~/.ssh/id_rsa user@remote_host "bash -s" < local_script.sh
  6. (Optional) Enable SSH Agent for Seamless Authentication

    If you have multiple keys, use ssh-agent to manage them:

    eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa

    Now you can SSH into the remote system without specifying the key every time.

  7. Troubleshooting

    • Ensure SSH permissions are correctly set:
      chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
    • If SSH still prompts for a password, check:
      ssh -v user@remote_host

    This verbose mode helps debug authentication issues.

How to Automatically Login and Execute Commands on Remote Server

After setting up SSH key-based authentication, the login process becomes passwordless and fully automated. Below is a demonstration of how you can log in and execute commands on the remote system.

  1. Log in to the Remote System

    Simply run:

    ssh user@remote_host
    

    or, if you configured ~/.ssh/config:

    ssh myserver
    

    Expected Output:

    Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.0-91-generic x86_64)
    Last login: Sat Feb 23 10:00:12 2025 from 192.168.1.100
    user@remote_host:~$
    
    • No password prompt should appear.
    • You are now logged into the remote system.
  2. Run a Remote Command

    To check system uptime:

    ssh user@remote_host "uptime"

    Expected Output:

     10:05:30 up 10 days, 4:37, 1 user, load average: 0.12, 0.07, 0.01

    To list files in the home directory:

    ssh user@remote_host "ls -l"

    Example Output:

    total 12 drwxr-xr-x 2 user user 4096 Feb 23 09:55 Documents drwxr-xr-x 2 user user 4096 Feb 23 09:55 Downloads drwxr-xr-x 2 user user 4096 Feb 23 09:55 Projects
  3. Automate SSH in a Script

    You can create a Bash script to automate SSH login and execution:

    #!/bin/bash echo "Checking system uptime on remote server..." ssh user@remote_host "uptime"

    Save the script as check_uptime.sh, then run:

    chmod +x check_uptime.sh ./check_uptime.sh

    Expected Output:

    Checking system uptime on remote server... 10:06:45 up 10 days, 4:38, 1 user, load average: 0.10, 0.05, 0.01
  4. Transfer Files Using SCP

    To copy a local file to the remote server:

    scp myfile.txt user@remote_host:/home/user/

    Expected Output:

    myfile.txt 100% 4KB 2.3MB/s 00:00

    To copy a file from the remote server:

    scp user@remote_host:/home/user/server.log .
  5. Troubleshooting

    If SSH still asks for a password:

    • Ensure correct file permissions:
      chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
    • Run SSH in verbose mode:
      ssh -v user@remote_host

      This will show debugging information.

Final Outcome

  • Login is automated (passwordless).
  • Remote commands execute seamlessly.
  • File transfers work smoothly.

Now, SSH key-based authentication is fully functional! 🚀
Launch 100% ssd vps from $2. 49/mo!

Conclusion

You now know how to save time and automate secure SSH login to remote servers for instant and effortless connectivity.

Now, how to spend all of your new free time… 🤔May I suggest?: explore more guides👇

Top Guides to Explore:

Share this:
Avatar of editorial staff

Editorial Staff

Rad Web Hosting is a leading provider of web hosting, Cloud VPS, and Dedicated Servers in Dallas, TX.
lg