How to connect without password using SSH
Make sure that SSH server is running¶
Check service status using: sudo service sshd status or or sudo systemctl status sshd
If the SSH service is closed, run the following command to start the ssh service: sudo service sshd start or sudo systemtcl start sshd.
Connect to remote machine¶
Run the command ssh remote_username@remote_server_ip_address if it is the first time you logged into this host, you will get something like this :
The authenticity of host XXXXXX can’t be established.
RSA key fingerprint is 7c:e7:51:3b:86:70:07:ab:65:a9:bf:2d:c0:7b:1b:a7.
Are you sure you want to continue connecting (yes/no)?
yes and then it will ask you to enter the password.
You are now connected to the remote machine with the password.
Generate private and public keys¶
Let’s back to our localhost machine, a key pair must be created with the command
ssh-keygen -t rsa
~/.ssh/id_rsa.pub and a private key file ~/.ssh/id_rsa will be generated.
If your more interested in private and public keys using ssh please referee to this article.
Copy the public key file to the remote machine¶
Now that you have generated an SSH key pair, in order to be able to login to your machine without a password you need to copy the public key to the server you want to manage.
ssh-copy-id remote_username@remote_server_ip_address
To test it just try to login to your server via SSH:
ssh remote_username@remote_server_ip_address
Setting Up Passwordless Login with SSH Config¶
The ~/.ssh/config file allows users to define custom settings for different SSH connections, simplifying SSH commands and improving connection efficiency.
Basic Syntax¶
Each configuration entry typically starts with Host, followed by specific parameters. For example:
Host myserver
HostName example.com
User myuser
Port 22
IdentityFile ~/.ssh/id_rsa
In the terminal, users can simply enter:
ssh myserver
Setting Up Passwordless Login with SSH Config¶
Generate SSH Key Pair¶
If no key pair exists locally, generate one with the following command:
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa
id_rsa (private key) and id_rsa.pub (public key).
Copy Public Key to Remote Server¶
Use ssh-copy-id to transfer the public key to the remote server:
ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote-host
ssh-copy-id is unavailable, manually append the id_rsa.pub contents to the remote server’s ~/.ssh/authorized_keys file:
cat ~/.ssh/id_rsa.pub | ssh user@remote-host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Ensure Correct Permissions¶
The ~/.ssh/ directory and authorized_keys file on the remote server must have the correct permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Configure ~/.ssh/config¶
Add the following configuration to ~/.ssh/config for passwordless login:
Host myserver
HostName example.com
User myuser
IdentityFile ~/.ssh/id_rsa
PasswordAuthentication no
ssh myserver will work without requiring a password.
Testing Configuration¶
Use ssh -G <host> to check the final parsed configuration. For example:
ssh -G myserver