Home > System Tutorial > LINUX > body text

How to pass password to scp command in Linux

WBOY
Release: 2024-02-10 16:18:20
forward
1092 people have browsed it

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.

prerequisites

  • Server running Linux
  • The root password configured on your server

Use SCP command to copy files

The basic syntax for copying files from the local system to the remote system is as follows:

scp filename user@remotehost:/directory/path
Copy after login

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/
Copy after login

The basic syntax for copying files from the remote system to the local system is as follows:

scp user@remotehost:/file/path local/path
Copy after login

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/
Copy after login

You will be prompted to provide the password for the remote user, as shown below:

root@192.168.1.100 password:
Copy after login

Install sshpass in Linux

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
Copy after login
如何在 Linux 中将密码传递给 scp 命令

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

dnf install sshpass -y
Copy after login

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

How to pass password using SCP command

The basic syntax for passing passwords using the SCP command is as follows:

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

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/
Copy after login

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 conclusion

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!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template