Home >System Tutorial >LINUX >How to pass password to scp command in Linux
Copying and moving files and directories is a common task among system administrators. The Linux command line utility scp (Secure Copy) can securely copy or transfer files from one server to another. Using the scp command, you can copy files or directories from the local system to the remote system, from the remote system to the local system, or between the local system and two remote systems. When using the scp command, you need to provide the remote user password for file transfer. This article will introduce how to pass password to scp command in Linux.
The basic syntax for copying files from the local system to the remote system is as follows:
scp filename user@remotehost:/directory/path
For example, to copy a file named file1.txt from the local system to the remote system with the IP address 192.168.1.100 in the /mnt directory, please ****runThe following commands:
scp file1.txt root@192.168.1.100:/mnt/
The basic syntax for copying files from the remote system to the local system is as follows:
scp user@remotehost:/file/path local/path
For example, to copy a file named file1.txt from the remote system with the IP address 192.168.1.100 to the local system in the /opt directory, run the following command:
scp root@192.168.1.100:/mnt/file1.txt /opt/
You will be prompted to provide the password for the remote user, as shown below:
root@192.168.1.100 password:
sshpass is a simple and lightweight command line tool that allows you to provide a password for the command prompt itself. It is very useful in shell scripts when you want to take backup via cron job. By default, sshpass is not included in any Linux operating system, so you need to install the sshpass utility in your Linux system to pass passwords using SCP commands. For Ubuntu and Debian based operating systems, install sshpass using the following command:
linuxmi@linuxmi:~/www.linuxmi.com$ sudo apt install sshpass -y
For RHEL, Fedora, CentOS and Rocky Linux operating systems, use the following command to install sshpass:
dnf install sshpass -y
After installing the sshpass utility, you can proceed to the next step.
The basic syntax for passing passwords using the SCP command is as follows:
sshpass -p "remote-user-password" scp filename user@remotehost:/dir/path/
For example, if you want to copy the file named file1.txt to the remote server with IP 192.168.1.100, please use the following command:
sshpass -p "password" scp file1.txt root@192.168.1.100:/mnt/
As you can see, the sshpass SCP command will help you copy files and directories from one system to another using a single line command.
In this article, we explain how to pass passwords using SCP command with sshpass in Linux. sshpass will help you speed up the file copying process. You can also use it to automatically backup from one server to another.
The above is the detailed content of How to pass password to scp command in Linux. For more information, please follow other related articles on the PHP Chinese website!