How to Pass Password to SCP Command in Linux using SSHPass

ssh+pass on a blue gradient background

SCP stands for secure copy and is used to securely copy files or directories from one Linux environment to another.

Using the SCP command you can copy files or directories from a remote environment to a local environment, from a local environment to a remote environment, or between two remote environments in your local environment.

There are a number of benefits to using the SCP command to copy files i.e. you can limit the bandwidth which the channel can occupy using the -l tag.

SCP command supports password encryption to protect the data from leaking or files being snooped during transfer. Password encryption is what makes SCP secure.

Using the SCP Command

The SCP command can take in a number of parameters depending on your needs. A general SCP command to copy files from a local environment to a remote environment will look like this.

scp filename user@remotehost:/directory/path

Let’s say we want to copy a file name Linux1.txt from the local environment to the remote environment over the IP address 192.168.1.100 inside the /mnt directory. The above provided generalized command will be edited and it will look as follows.

scp file1.txt [email protected]:/mnt/

The execution of the above SCP command will lead to a password entry prompt because SCP is password protected which makes it secure.

[email protected] password:

After providing the proper user associated password, the files will start copying.

Now, let’s take a look at what the basic SCP command to copy files from the remote environment to the local environment looks like.

scp user@remotehost:/file/path local/path

For example we want to copy a file named Linux1.txt from the remote environment to the local environment with the IP address 192.168.1.100 inside the /opt directory. The SCP command to do this will look as follows.

scp [email protected]:/mnt/file1.txt /opt/

When the above command is executed, you will be prompted to provide a local user’s password like shown below.

[email protected] password:

The purpose of an SCP command is to copy files or directories from a host environment to a destination environment. The SCP command is not sufficient enough to encapsulate password authentication in a one-line command and thus leads to a password prompt requiring the user’s password.

But there is another way in which the user can pass a one-liner command and does not have to wait for the password prompt before files can start copying. It includes another command-line tool known as SSH pass. But before we can use it, we need to install it.

Install SSHPass

SSHPass is a lightweight command-line tool that allows users to pass passwords to the command line prompt itself. SSHPass is not included in any Linux system and you need to install this utility separately if you want to pass passwords along with the SCP command.

To download SSHPass on your Ubuntu or Debian system, execute the following code.

sudo apt-get install sshpass -y

For RHEL, Fedora, or CentOS execute the following command to install the SSHPass utility.

dnf install sshpass -y

Pass Password to SCP using SSHPass

Now that we have installed the SSHPass utility, we can incorporate this with the SCP command and execute a one-line command to copy files without having to wait for the password prompt.

The basic syntax to pass a password along with the SCP command for copying files from a local environment to the remote environment will look like this:

sshpass -p "remote-user-password" scp filename user@remotehost:/dir/path/

For example if we want to copy a file named Linux1.txt to the remote environment with the IP address 192.168.1.100, the command to do this will look like this:

sshpass -p "password" scp file1.txt [email protected]:/mnt/

As we are passing the password along with the SCP command, it will not lead to a password prompt and will start copying the files as soon as the command is executed.

Conclusion

In this tutorial we learned what the SCP command is and what are some of the basic use cases. Then we learned how we can incorporate another useful utility tool called SSHPass and combine it with the SCP command to write one-liner commands that do not lead to a password prompt.

This speeds up the process of copying files. You can also use this method to automate backup from one server to another.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like