Automate Secure SSH Login to Remote Servers
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:
- Access to a remote server (for this guide, we use an Ubuntu VPS with fresh install of Ubuntu 22.04)
- An internet-connected device with SSH client (for this guide, we use AlmaLinux VPS with fresh install of AlmaLinux 8)
- A working knowledge of the Linux command-line
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.
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:
-
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.
-
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:
userwith your remote system’s username.remote_hostwith the IP address or hostname of the remote server.
If
ssh-copy-idis 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"
-
Test the SSH Connection
Now, you should be able to log in without a password:
ssh user@remote_host
-
Automate SSH Login with an Alias (Optional)
You can create an alias in
~/.ssh/configto simplify access:nano ~/.ssh/config
Add the following entry:
Host myserver HostName remote_host User user IdentityFile ~/.ssh/id_rsaNow, you can connect simply by running:
ssh myserver
-
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
-
(Optional) Enable SSH Agent for Seamless Authentication
If you have multiple keys, use
ssh-agentto 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.
-
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.
- Ensure SSH permissions are correctly set:
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.
-
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.
-
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
-
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
-
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 .
-
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.
- Ensure correct file permissions:
Final Outcome
- Login is automated (passwordless).
- Remote commands execute seamlessly.
- File transfers work smoothly.
Now, SSH key-based authentication is fully functional! 🚀
Conclusion
You now know how to 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👇











[…] Automate Secure SSH Login to Remote Servers for Instant and Effortless Connectivity […]