In this guide, you will learn to Pass the password to the scp command in Linux using the sshpass example. As you must know, the scp command is used to securely copy files and directories from a local machine to a remote machine. The scp command supports password encryption to protect the data during transfer. Password encryption makes the SCP secure. But there is another way that you can use to pass the password by using the sshpass. We try to explain it in this guide to how you can do it.
Steps To Pass password to scp command in Linux using sshpass example
You can use the scp command with a number of parameters. A way of transferring files with SCP is to use the SSH keys. To get more information about its usage, you can visit this guide on Using SCP to Transfer Files with SSH keys.
Now follow the steps below to pass the password in the scp command with the sshpass command-line utility in Linux.
Step 1 – Install sshpass Utility on Linux
The sshpass is a utility that you can pass the passwords to the command line prompt itself. It is not installed by default in the Linux distros. First, you must install it on your server.
On Debian-based distros, you can run the command below to install the sshpass tool:
sudo apt install sshpass -y
To install sshpass on RHEL-based distros, you can run the following command:
sudo dnf install sshpass -y
Step 2 – Use sshpass To Pass the Password in scp Command
Now that you have the sshpass utility installed on your server, you can use it in the scp command to pass the password.
The syntax of using this utility in the scp command is as follows:
sshpass -p "remote-user-password" scp filename user@remotehost:/dir/path/
For example, we want to transfer testfile to the remote host IP 192.168.1.100 and the dir path is /usr/local. The syntax is like:
sshpass -p "password" scp testfile [email protected]:/usr/local
Remember to enter the remote user password in the command. With this option, you will not be asked to enter the password and the copying process starts immediately.
At this point, you have learned to Pass the password to the scp command in Linux using the sshpass example. You just need to install the sshpass utility on your Linux server and use the -p option in the scp command with the remote user password. In this way, your copying process starts immediately.
Hope you enjoy it. You may be interested in these articles: