Copying files and directories from one system to another system is a common task for any system administrator. SCP, also called secure copy, is a Linux command-line utility used to securely copy or transfer files from one server to another server. The SCP command allows you to copy a file or directory from a local system to a remote system, from a remote system to a local system, or between two remote systems from your local system.

When you use the SCP command, you will need to provide a remote user’s password before transferring a file. In this post, we will explain how to pass passwords to the SCP command in Linux.

In This Article

Copying Files Using SCP Command

The basic syntax to copy a file from the local system to the remote system is shown below:

scp filename user@remotehost:/directory/path

For example, to copy a file named file1.txt from your local system to the remote system with IP address 192.168.1.100 inside the /mnt directory, run the following command:

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

The basic syntax to copy a file from the remote system to the local system is shown below:

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

For example, to copy a file named file1.txt from the remote system with IP address 192.168.1.100 to the local system inside /opt directory, run the following command:

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

You will be prompted to provide a remote user’s password as shown below:

[email protected] password:

Also Read

How to Install and Use Ack Command in Linux

Install sshpass in Linux

sshpass is a simple and lightweight command-line tool that allows you to provide passwords to the command prompt itself. It is very useful in a shell script when taking a backup via a cron job. By default, sshpass is not included in any Linux operating system, so you will need to install the sshpass utility in your Linux system to pass the password with the SCP command. For Ubuntu and Debian-based operating systems, install sshpass using the following command:

apt-get install sshpass -y

For RHEL, Fedora, CentOS, and Rocky Linux operating systems, install sshpass using the following command:

dnf install sshpass -y

After installing the sshpass utility, you can proceed to the next step.

How to Pass Password with SCP Command

The basic syntax to pass the password with the SCP command is shown below:

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

For example, if you want to copy a file named file1.txt to the remote server with IP 192.168.1.100, use the following command:

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

As you see, the sshpass + SCP command will help you copy files and directories from one system to another system using a one-line command.

Also Read

How to Change or Set User Password in Linux

Conclusion

In this post, we explained how to use the SCP command with sshpass to pass a password in Linux. sshpass will help you speed up your file-copying process. You can also use it to automate backup from one server to another server. Try it on dedicated hosting from Atlantic.Net!