Linux scp Command
The Linux scp command is used to copy files and directories between Linux systems.
scp is the abbreviation of secure copy. scp is a command for secure remote file copying based on SSH login in Linux systems.
scp is encrypted,rcprcp is not encrypted; scp is an enhanced version of rcp.
In Linux systems,scpthe command is a very practical toolscpis the abbreviation of "secure copy". It is based on the SSH (Secure Shell) protocol to ensure the security of data transmission.
Syntax
scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 [...] [[user@]host2:]file2
Simple syntax:
scp [选项] [源文件] [目标路径]
[Options]: used to specifyscpthe behavior of the command, such as recursive copying, preserving file attributes, etc.[Source File]: the path of the file or directory to be copied.[Target Path]: the destination path where the file or directory will be copied.
Common options:
The following are some commonly usedscpcommand options:
-r: recursively copy the entire directory.-P: specify the SSH port number of the remote host (default is 22).-p: preserve the file's modification time, access time, and permissions.-v: display detailed debugging information, which helps troubleshoot problems.-C: enable compression to speed up transmission.
Complete option description:
- -1: Force the scp command to use protocol ssh1
- -2: Force the scp command to use protocol ssh2
- -4: Force the scp command to use only IPv4 addressing
- -6: Force the scp command to use only IPv6 addressing
- -B: Use batch mode (do not ask for passwords or passphrases during transfer)
- -C: Allow compression. (Passes the -C flag to ssh, thereby enabling compression)
- -p: Preserve the original file's modification time, access time, and access permissions.
- -q: Do not display the transfer progress bar.
- -r: Recursively copy the entire directory.
- -v: Display output in verbose mode. scp and ssh(1) will show debugging information for the entire process. This information is used for debugging connection, authentication, and configuration problems.
- -c cipher: Encrypt data transfer with cipher; this option is passed directly to ssh.
- -F ssh_config: Specify an alternative ssh configuration file; this parameter is passed directly to ssh.
- -i identity_file: Read the key file used for transfer from the specified file; this parameter is passed directly to ssh.
- -l limit: Limit the bandwidth that the user can use, in Kbit/s.
- -o ssh_option: If you are used to the parameter passing method in ssh_config(5),
- -P port: Note that it is an uppercase P. port specifies the port number used for data transfer.
- -S program: Specify the program used for encrypted transfer. This program must be able to understand the options of ssh(1).
Copy files from local to remote host
Suppose you have a fileexample.txt, and you want to copy it to the remote host's/home/user/directory, you can use the following command:
scp example.txt user@remote_host:/home/user/
In this command:
example.txtis the local file.user@remote_hostis the username and address of the remote host./home/user/is the destination path on the remote host.
Copy files from remote host to local
If you want to copy the file on the remote hostexample.txtto the current local directory, you can use the following command:
scp user@remote_host:/home/user/example.txt .
In this command:
user@remote_host:/home/user/example.txtis the file path on the remote host..represents the current directory.
Recursively copy directories
If you want to copy an entire directory and its contents, you can use the-roption. For example, copy the localmy_directorydirectory to the remote host's/home/user/directory:
scp -r my_directory user@remote_host:/home/user/
Specify port number
If the remote host's SSH service runs on a non-default port (e.g., 2222), you can use the-Poption to specify the port number:
scp -P 2222 example.txt user@remote_host:/home/user/
Preserve file attributes
If you want to preserve the file's modification time, access time, and permissions when copying files, you can use the-poption:
scp -p example.txt user@remote_host:/home/user/
Enable compression
For large files or slow network conditions, you can use the-Coption to enable compression to speed up transfer:
scp -C large_file.zip user@remote_host:/home/user/
Application examples
1. Copy from local to remote
Command format:
scp local_file remote_username@remote_ip:remote_folder 或者 scp local_file remote_username@remote_ip:remote_file 或者 scp local_file remote_ip:remote_folder 或者 scp local_file remote_ip:remote_file
- The 1st and 2nd specify the username. After executing the command, you need to enter the password. The 1st only specifies the remote directory, so the file name remains unchanged; the 2nd specifies the file name.
- The 3rd and 4th do not specify a username. After executing the command, you need to enter the username and password. The 3rd only specifies the remote directory, so the file name remains unchanged; the 4th specifies the file name.
Application example:
scp /home/space/music/1.mp3 [email protected]:/home/root/others/music scp /home/space/music/1.mp3 [email protected]:/home/root/others/music/001.mp3 scp /home/space/music/1.mp3 www.example.com:/home/root/others/music scp /home/space/music/1.mp3 www.example.com:/home/root/others/music/001.mp3
Command format for copying directories:
scp -r local_folder remote_username@remote_ip:remote_folder 或者 scp -r local_folder remote_ip:remote_folder
- The 1st specifies the username, and you need to enter the password after executing the command;
- The 2nd does not specify a username, and you need to enter the username and password after executing the command;
Application example:
scp -r /home/space/music/ [email protected]:/home/root/others/ scp -r /home/space/music/ www.example.com:/home/root/others/
The above command copies the local music directory to the remote others directory.
2. Copy from remote to local
To copy from remote to local, simply swap the order of the last two parameters of the command used to copy from local to remote, as in the example below
Application example:
scp [email protected]:/home/root/others/music /home/space/music/1.mp3 scp -r www.example.com:/home/root/others/ /home/space/music/
Notes
1. If the remote server firewall has set a designated port for the scp command, we need to use the -P parameter to set the port number for the command. The command format is as follows:
#scp 命令使用端口号 4588 scp -P 4588 [email protected]:/usr/local/sin.sh /home/administrator
2. When using the scp command, make sure that the user used has permission to read the corresponding files on the remote server; otherwise, the scp command will not work.
Precautions
- Make sure you have sufficient permissions to access the source file and destination path.
- If the remote host's SSH service is not running or is not configured correctly,
scpthe command will not work properly. - Using
-vthe option can help you debug connection problems.