How to SSH without password
We usually login to a remote SSH server using password authentication method. Username and password combination is the most common authentication method for SSH and is a suitable method for most people.
Sometimes, you might want or even need to automatically log in to an SSH server without entering your username and password. Passwordless SSH login is convenient for some, especially for those having to log in to ssh servers constantly. However, for those doing automation work, it's a necessity; an automated script could be used to run commands on a remote server or transfer files using scp command.
Related: How to copy file remotely via SSH
You can automatically log in to an SSH server without entering your username and password by configuring and using the public-key authentication method.
Steps to use passwordless login in SSH:
-
Launch terminal application.
-
Generate an SSH key pair on the local host if you don't already have one.
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/user/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/user/.ssh/id_rsa. Your public key has been saved in /home/user/.ssh/id_rsa.pub. ##### snipped
Make sure to not set any passphrase for the key pair
Related: How to generate SSH key pairs
-
Enable public key authentication in the remote SSH server.
$ sudo grep PubkeyAuthentication /etc/ssh/sshd_config [sudo] password for user: PubkeyAuthentication yes
Public key authentication is normally enabled by default.
-
Copy your SSH public key from local host to the SSH server.
$ ssh-copy-id user@remote-host /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 2 key(s) remain to be installed -- if you are prompted now it is to install the new keys user@remote-host's password: Number of key(s) added: 2 Now try logging into the machine, with: "ssh 'user@remote-host'" and check to make sure that only the key(s) you wanted were added.
Related: How to add SSH public key to server
-
Log in to the remote server to test.
$ ssh user@remote-host Last login: Fri Jun 28 00:12:15 2019 from 192.168.111.135 [user@remote-host ~]$
You will no longer be prompted for a password when logging in to the server.
Guide compatibility:
Operating System |
---|
Ubuntu Linux |
Debian Linux |
Red Hat Enterprise Linux |
Fedora Linux |
CentOS Linux |
openSUSE Linux |
SUSE Linux Enterprise Server |
FreeBSD |
OpenBSD |
NetBSD |
macOS |