To connect two servers over SSH, create the same user on both servers, give that user sudo rights, allow password authentication in the SSH configuration, and restart the SSH service. After that you can log in from one server to the other with ssh, and add an SSH key with ssh-copy-id so the login no longer asks for a password.
This guide uses two Ubuntu servers on the same private network. Replace the example user "devuser" and the placeholders <server-two-private-ip> with your own values.
What you need before you start
- Two Linux (Ubuntu) servers that can reach each other on their private IP addresses
- Root or sudo access on both servers
- Port 22 open between the two servers (check your firewall or cloud security group)
How do I prepare the first server?
The preparation is four steps. Run them as the root user on Server One, then repeat all four on Server Two.
Step 1: Create a new user
Create the user that will be used for the SSH connection. Enter and confirm a strong password when asked, and press Enter to accept the default values for the other questions.
$ adduser devuserStep 2: Give the user sudo access
Open the sudoers file with visudo, which checks the file for mistakes before saving.
$ visudoUnder the line "User privilege specification", add this line for your user: devuser ALL=(ALL:ALL) NOPASSWD:ALL
Save and exit the editor with CTRL+X, then Y, then Enter. NOPASSWD:ALL lets the user run sudo without a password, so use it only on private servers you control. For production, give the user only the commands it needs.
Step 3: Allow password authentication
Open the SSH server configuration file.
$ nano /etc/ssh/sshd_configFind the PasswordAuthentication line and make sure it is set to: PasswordAuthentication yes. If it says no, change it, because the first login in this guide uses a password.
Save and exit the file.
Step 4: Restart the SSH service
Restart SSH so the new setting is applied.
$ service ssh restartNow repeat Steps 1 to 4 on Server Two.
How do I log in from Server One to Server Two with a password?
Switch to the new user on Server One, then connect to Server Two using its private IP address.
$ su devuser
$ ssh <server-two-private-ip>The first time you connect, SSH shows the server fingerprint and asks whether to continue. Type yes. SSH saves the server in the known hosts list, then asks for the password of the user on Server Two. Enter it, and you are logged in to Server Two.
To return to Server One, type exit. You will see the message that the connection to the other server is closed.
How do I set up password-less SSH login?
Password-less login uses an SSH key pair instead of a password. It is the safer choice and the one automation scripts need. Do this on Server One, as the new user.
First, generate the key pair. Press Enter to accept the default file location and, if you want a fully password-less login, leave the passphrase empty.
$ ssh-keygenNext, move into the home directory of your user.
$ cd /home/devuserCopy the public key to Server Two.
$ ssh-copy-id <server-two-private-ip>Enter the user password one last time when asked. The key is now stored on Server Two.
Finally, connect again. This time you are logged in without a password.
$ ssh <server-two-private-ip>Security tips
- Once key login works, set PasswordAuthentication back to no in sshd_config and restart SSH, so nobody can guess a password
- Keep the private key (id_rsa) only on Server One and never share it
- Limit sudo rights to what the user really needs
- Allow port 22 only from the servers or IP addresses that need it
Common problems and fixes
- Connection timed out: the port 22 is blocked. Check the firewall and the cloud security group.
- Permission denied: the user or password is wrong, or PasswordAuthentication is still set to no. Check sshd_config and restart SSH.
- Still asked for a password after ssh-copy-id: check that the .ssh folder on Server Two is owned by the user and has permission 700.
Need help with servers and DevOps?
Trishaksha Technologies helps businesses set up secure cloud servers, CI/CD pipelines and integrations. If you want a hand with your infrastructure, contact our team through the Contact page.
Frequently asked questions
What is SSH?
SSH (Secure Shell) is a protocol that lets you log in to another computer over the network and run commands on it, with the connection encrypted.
Do I need to create the same user on both servers?
Yes. The user you connect with must exist on the destination server, so repeat the user setup on both servers.
How do I log in over SSH without a password?
Generate a key pair with ssh-keygen on the first server and copy the public key to the second server with ssh-copy-id. After that, ssh logs you in without asking for a password.
Is password-less SSH safe?
Yes, key-based login is safer than passwords when the private key is kept secret. For best security, turn password authentication off once key login works.
Which port does SSH use?
SSH uses port 22 by default. Make sure it is open between your two servers.