Atlantic.Net Blog

How to Pass Password to SCP Command in Linux

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.

Prerequisites

  • A server running Linux on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing any Linux operating system with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

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 [email protected]:/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 /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 [email protected]:/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 you want to take backup via 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 system, 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 [email protected]:/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 to 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 to 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!

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year