Connect To A Remote Linux Server Without A Password Using In-line SSH Commands

If your work requires you to connect and manage multiple RHEL-based Linux distributions like AlmaLinux, CentOS, Fedora, Rocky Linux, or Debian-based distributions like Ubuntu and Mint, then opening an SSH terminal for each server can be tedious. And typing the password for each machine every time you log in is one of those tedious tasks. The good thing is there is a way to make this task easier for the end-users. Unlike if you are maintaining Windows machines, the approach in Linux is different.

Unix Shell Script

The steps below will provide a quick guide on accessing another Linux machine from another server. And we will show you in this article how you can connect to a remote Linux server without a password using in-line SSH commands.

Linux SSH Passwordless Remote Login

SSH Passwordless login is one of the quickest ways to automate tasks such as automatic backups with scripts, synchronizing files using the SCP command, or remote command execution if you operate with multiple Linux remote servers.

To make this happen, you must create an ssh-keygen and confirm that both the source server and the target server have them. The ssh-keygen can create public and private keys.

Then, we will use ssh-copy-id to transfer the public key from the local host to the remote host’s authorized keys file. Additionally, ssh-copy-id gives the remote host’s home directory, ~/.ssh, and ~/.ssh/authorized_keys the appropriate permissions.

3 Steps To Connect To A Remote Linux Server Without A Password

Now that we have provided the tools to allow users to connect to a remote Linux server without a password, how do we go about it?

Here is an example of how you can use the Linux commands above that will allow you to connect from one server to another passwordless.

1. Choose your source and target Linux servers – a client and a host. For example, we have the following servers:

  • (assuming this is your client or source server)
  • (and this will be your target server)

2. To start, create an ssh-keygen from the Source server by typing ssh-keygen. Then press the Enter key to all the questions.

[test@test ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/test/.ssh/id_rsa.
Your public key has been saved in /home/test/.ssh/
The key fingerprint is:
d7:82:ba:09:8e:7c:23:02:2e:75:68:22:dd:b9:83:fd remotedeploy@test.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|         . .     |
| . o .  S o .    |
|+ = +  . . .     |
|++ +...          |
|oooo*. o         |
|..oo.+E          |

3. The next step is to execute the command ssh-copy-id to copy the keygen to the target server. If asked if you want to proceed, you must respond “yes.” You must also enter the target server’s password once to sync or copy the keygen to the remote server.

[test@test ~]$ ssh-copy-id test@
The authenticity of host ' (' can't be established.
RSA key fingerprint is 13:ef:8e:9a:be:e2:0e:01:9a:5d:ae:9d:ca:58:36:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '' (RSA) to the list of known hosts.
test@'s password:
Now try logging into the machine, with "ssh 'test@'", and check in:
to make sure we haven't added extra keys that you weren't expecting.

And you are done! The SOURCE and TARGET servers’ keygens are currently in sync. You can run SSH commands to the remote target server remotely without a password. For example, you can use the ssh command to check the folders and files in the target server’s /tmp directory.

[test @test ~]$ ssh -f test @ 'ls -lrtha /tmp'
[test @test ~]$ total 553M
drwx------.  2 root           root            16K Apr 11  2018 lost+found
-rw-r--r--.  1 root           root           834K Apr 16  2019 httpd-2.2.15-1.x86_64.rpm
-rw-r--r--.  1 root           root           837K Apr 16  2019 httpd-2.2.15-69.el6.centos.x86_64.rpm
-rw-r--r--.  1 root           root            81K Apr 26  2019 httpd-tools-2.2.15-69.el6.centos.x86_64.rpm
-rw-rw-r--.  1 test test 166 Oct 16  2020
drwxrwxr-x.  2 test test 4.0K Oct 22  2020 R25Release

drwxrwxrwt.  2 root           root           4.0K Feb 24 11:33 .ICE-unix
dr-xr-xr-x. 26 root           root           4.0K Feb 24 11:39 ..
drwxrwxrwt. 10 root           root           4.0K Apr  1 03:41 .

Concluding Words

With these simple 3-steps, you can do your tasks efficiently and faster. However, be cautious if you have been used to opening multiple ssh clients to access servers. Now that you have limited open ssh windows, you might get confused about which server you are currently logged in to and executing the commands. You don’t want to end up deleting files in a Linux server when you are not supposed to, right?

If you are in doubt, you can use the commands uname or hostname combined with other in-line SSH commands.