

Now, let’s try to connect to host2 from host1: ]$ ssh host2Īgain, ssh doesn’t ask for the password - as we expected. ]$ SSHPASS=my_secret_password sshpass –e ssh-copy-id įirst, we set the value of the environment variable SSHPASS to the password of alice on host2 using SSHPASS=my_secret_password. Additionally, it’s a good idea to encrypt the file. We may set the permissions of the file that stores the password so that others can’t get its content. This method is more secure than using the -p option. If the file consists of multiple lines, sshpass only uses the first line as the password. Ssh doesn’t ask for the password, and we can connect to host2. Now, let’s check whether connecting from host1 to host2 asks for a password while using ssh: ]$ ssh host2 Let’s check the content of passwd_file: ]$ cat passwd_file The -f passwd_file part of sshpass specifies that sshpass must get the password from the file passwd_file. After having deleted the file, let’s use sshpass with its -f option: ]$ sshpass –f passwd_file ssh-copy-id First, we must delete the file /home/alice/.ssh/authorized_keys on host2 to revert the permission we gave in the previous example. However, since we have to provide the password on the command line, this isn’t a secure choice. So, we can achieve our goal using the -p option. We’re able to connect to host2 - ssh doesn’t ask for the password this time. Now, let’s check whether we can connect to host2 from host1 using ssh: ]$ ssh host2 ssh-copy-id doesn’t ask for the password during the configuration, therefore, it’s possible to use ssh-copy-id in a script for the configuration of ssh for many hosts. Here, my_secret_password is the password of alice on host2. We also provide the password from the command line using -p my_secret_password.

This time, we pass the ssh-copy-id command as the command that will be run by sshpass.

Now, instead of directly using ssh-copy-id, let’s use it together with sshpass: ]$ sshpass –p my_secret_password ssh-copy-id In the earlier example, ssh-copy-id had copied our public key into this file.įinally, after logging out of host2 using exit, we try to reconnect from host1 to host2. Then, we delete the file /home/alice/.ssh/authorized_keys on host2. It would be nice to automate the process to use in shell scripts. If we’re the system administrator and want to configure the system for SSH connections without using passwords, then it’s tedious to enter the password manually for each host while using ssh-copy-id. Then, what’s the problem? We may have many remote hosts with the same username and password combination. Therefore, we’re able to successfully log in to host2 from host1 without using passwords. This time, ssh doesn’t ask for the password. Now, let’s test whether connecting from host1 to host2 asks for the password: ]$ ssh host2

Now try logging into the machine, with: "ssh ' '"Īnd check to make sure that only the key(s) you wanted were added.Īfter we enter the password of alice on host2, ssh-copy-id informs us that the key was added.
SSH COPY ID EXAMPLE INSTALL
usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keysĪfter printing a bunch of information, ssh-copy-id asks for the password of alice on host2. 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: Source of key(s) to be installed: "home/alice/.ssh/id_rsa.pub"
