How to Put Password in SSH Command

It’s possible to supply a password in an SSH command with the sshpass utility on Linux. This guide will show how to install sshpass and then go over the correct command syntax for putting a password into the SSH command.

As you might have guessed, putting your password in an SSH command is a bad security practice. Anyone looking over your shoulder or going through the command history of the system will be able to see your password. A better option would be to setup SSH without passwords using RSA keys.

Install sshpass on Linux

Install the sshpass tool with your system’s package manager by using the appropriate command below.

Ubuntu, Debian, and Linux Mint:

$ sudo apt install sshpass

Fedora, AlmaLinux, CentOS, and RHEL:

$ sudo dnf install sshpass

Arch Linux and Manjaro:

$ sudo pacman -S sshpass

sshpass Command Syntax

Now that sshpass is installed, you can use the following syntax to supply your password in the SSH command:

$ sshpass -p my_password ssh user@hostname

If your password contains special characters, try putting single or double quotes around it.

$ sshpass -p 'my_p@$$word' ssh user@hostname

A more secure way of using sshpass is by creating a text file that contains your password, and putting proper permissions on it so that only your user can view its contents.

$ echo "password" > ~/.sshpass.txt
$ chmod 400 ~/.sshpass.txt

Now we can reference this file from our SSH command whenever we want to specify the password.

$ sshpass -p $(cat ~/.sshpass.txt) ssh user@hostname

sshpass is also compatible with other utilities that utilize SSH. For example, you can use sshpass with rsync:

$ sshpass -p my_password rsync -ae ssh /src/ user@hostname:/dst/

If our content helped you, please consider buying us a coffee. We appreciate your support!

Buy me a coffee donation button

Leave a Comment

Your email address will not be published. Required fields are marked *