SSH is one of the most used means to access Linux servers. In fact, ssh is also used to access a number of firewalls, routers & other such devices. In this tutorial, we will learn how to use SSH command with a password in a single line command.
But why do we need that?
There can be any number of reasons like you want to access a server or run a command from a script that runs automatically using crontab etc or you are just lazy.
Whatever the case, one thing is for sure that THIS IS ONE OF THE MOST UNSAFE WAYS TO ACCESS SERVERS as you are giving away the username & password to anyone having access to the system as the commands are stored in history & one can also see the passwords in a script.
There are other ways to SSH servers securely without having to enter the username & password.
For that we can use SSH keys, public/private ssh keys allow a server to authenticate the server credentials with the use of certificates & we are not required to enter any usernames or passwords (though we can use pass-phrase also for certificates). You can refer to the article below to set up PASSWORDLESS SSH AUTHENTICATION.
But even if you need to use a one-liner command to use ssh command with the password, then read the article ahead. We will discuss two ways how to use ssh command with a password in a single line.
Recommended Read: Simple guide to install SSH on Ubuntu
Also Read: SSH server: Restrict SSH access on Linux
1 - Using the ‘SSHPASS’ command
Sshpass command is a very good Linux command that provides a simple way to non-interactive ssh login & it will input the ssh password for you. We are required to install this on our system,
$ sudo apt install sshpass
SSHPASS command is available with the EPEL repository, so we need to install that first,
# yum install epel-release
Once it’s installed, run the following command,
# yum install sshpass
Now let’s see how we can use the ssh command with a password using the sshpass command,
# sshpass -p “ENTER PASSWORD HERE” ssh [email protected]
An example would be,
Notice here, we used option ‘p’ to mention the password here with the sshpass command. But what if we need to use a custom port to access ssh on a server. Here is an example of that also,
So the basic syntax for sshpass is, using the sshpass command followed by the option for sshpass & then using the ssh command that you would have used normally. Now let’s discuss the 2nd method.
2- Using the ‘EXPECT’ command
This is another one of the important commands that can be used to ssh command with a password. To install EXPECT command on your Linux systems, run the following command,
$ sudo apt install expect
Expect command is available with the EPEL repository, so we need to install that first,
# yum install epel-release
Once it’s installed, run the following command to install expect command on your system,
# yum install expect
Now the once liner command to use expect the command to ssh is,
So to explain this a bit,
-c when used with expect command prefaces a command to be executed before any other command,
expect “password:” will look for the prompt for the password,
send "[email protected]\r" will send the password when prompted (add \r after you enter your password)
This can also be used in a script, for example,
spawn ssh [email protected]
send "[email protected]\r"
So this completes our tutorial on how to use the SSH command with a password in a single line. Please do send in any questions or queries using the comment box below.