If you are wondering how to automatically authenticate to a linux server with ssh and password authentication, this article is for you. sshpass is for ssh password authentication automation.
SSH password authentication is primarily used for keyboard-interactive use cases. sshpass gives you the option to run ssh with password authentication in a non-interactive mode. Using an SSH password requires manual intervention when used in a shell script and in many cases this is undesirable, especially when the script needs to be automated. Additionally, entering the SSH password each time you connect can be tedious. sshpass is the utility to workaround these issues.
This option is in our opinion inferior to using ssh public key authentication. If your environment requires password authentication then sshpass may be a suitable option with notable security risks. Namely, if supplying the password in the sshpass command then the passord will be visible in plain text in history. If the password is stored in a file, file permissions should be locked down to only the user(s) needing to access it. If the password is stored in an environment variable, any user with visibility into the environment variable can view it. These shortcomings should be considered before using sshpass.
sshpass Linux Install
While sshpass comes already installed on many OS’s, you can install it with the following commands if needed.
Installing sshpass on Ubuntu
sudo apt-get install sshpassInstalling sshpass on Redhat
sudo dnf install sshpasssshpass Examples
sshpass with password
sshpass -p "password" ssh <user>@<host>sshpass with password file
Where pass_file is the file containing the password. It’s permissions should be 0400.
sshpass -f pass_file ssh <user>@<host>sshpass with environment variable
In this example the environment variable SSHPASS must be set.
sshpass -e ssh <user>@<host>sshpass with rsync
rsync --rsh='sshpass -p <password> ssh -l test' <host>:path .sshpass with scp
sshpass -f ssh_pass scp <file> <user>@<host>:<path> sshpass no host key checking
Many times sshpass is used for automation in which you may not want host key checking to break your automated script. Host key checking may cause a silent failure in which you are not even sure what happened. To disable host key checking when using sshpass run this command.
sshpass -f pass_file ssh -oStrictHostKeyChecking=no <user>@<host>sshpass use deprecated key algorithms
If you are connecting to an older linux os you may be required to pass a deprecated key algorithm. To do so run this command:
sshpass -f pass_file ssh -oHostKeyAlgorithms=+ssh-rsa <user>@<host>Conclusion – sshpass ssh password authentication automation
Using sshpass for ssh password authentication automation can be an effective measure for automating password authenticated functions. Again, make sure to take the appropriate security measures to keep your password secure and not visible by unwelcome spectators.
Leave a Reply