Linux scp Command
The Linux scp command is used to copy files and directories between Linux systems.
scp is short for secure copy. scp is a command for secure remote file copying over ssh login on Linux systems.
scp is encrypted,rcp is not encrypted; scp is an enhanced version of rcp.
On Linux systems,scp command is a very useful tool scp is short for "secure copy". It is based on the SSH (Secure Shell) protocol, ensuring the security of data transfer.
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 [选项] [源文件] [目标路径]
[选项]: Used to specifyscpthe behavior of the command, such as recursive copying, preserving file attributes, etc.[源文件]: The path of the file or directory to copy.[目标路径]: The destination path to which the file or directory is copied.
Common options:
Here are some commonly used scp Command options:
-r: Recursively copy an entire directory.-P: Specify the SSH port number of the remote host (default is 22).-p: Preserve the modification time, access time, and permissions of files.-v: Display detailed debugging information to help troubleshoot problems.-C: Enable compression, which can speed up transfer.
Full 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 a password or passphrase during transfer)
- -C: Enable compression. (Pass the -C flag to ssh to turn on compression)
- -p: Preserve the modification time, access time, and access permissions of the original file.
- -q: Do not display the transfer progress bar.
- -r: Recursively copy an entire directory.
- -v: Display output in verbose mode. scp and ssh(1) will show debugging information for the entire process. This information is useful for debugging connection, authentication, and configuration issues.
- -c cipher: Encrypt the 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 available to the user, in Kbit/s.
- -o ssh_option: If you are used to the parameter-passing style in ssh_config(5),
- -P port: Note the uppercase P; port specifies the port number used for data transfer
- -S program: Specify the program used for encrypted transfer. This program must understand the options of ssh(1).
Copy files from local to a remote host
Suppose you have a file example.txt, and you want to copy it to the /home/user/ directory on the remote host, 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 a remote host to local
If you want to copy a file on a remote host example.txt to 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 -r option. For example, copy the local my_directory directory to the remote host's /home/user/ directory:
scp -r my_directory user@remote_host:/home/user/
Specify the port number
If the remote host's SSH service runs on a non-default port (for example, 2222), you can use the -P option to specify the port number:
scp -P 2222 example.txt user@remote_host:/home/user/
Preserve file attributes
If you want to preserve the modification time, access time, and permissions of files when copying, you can use -p Option:
scp -p example.txt user@remote_host:/home/user/
Enable compression
For large files or slow networks, you can use the -C option to enable compression to speed up transfer:
scp -C large_file.zip user@remote_host:/home/user/
Application Example
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 a username, so you need to enter the password after the command runs. The 1st specifies only the remote directory, and the filename stays the same; the 2nd specifies the filename.
- The 3rd and 4th do not specify a username, so you need to enter both the username and password after the command runs. The 3rd specifies only the remote directory, and the filename stays the same; the 4th specifies the filename.
Application examples:
scp /home/space/music/1.mp3 root@www.runoob.com:/home/root/others/music
scp /home/space/music/1.mp3 root@www.runoob.com:/home/root/others/music/001.mp3
scp /home/space/music/1.mp3 www.runoob.com:/home/root/others/music
scp /home/space/music/1.mp3 www.runoob.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 a username, so you need to enter the password after the command runs;
- the 2nd does not specify a username, so you need to enter the username and password;
Application examples:
scp -r /home/space/music/ root@www.runoob.com:/home/root/others/
scp -r /home/space/music/ www.runoob.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 local-to-remote command, as in the following examples
Application examples:
scp root@www.runoob.com:/home/root/others/music /home/space/music/1.mp3
scp -r www.runoob.com:/home/root/others/ /home/space/music/
Description
1. If the remote server's firewall has set a specific port for the scp command, we need to use the -P parameter to set the command's port number. The command format is as follows:
# scp command using port 4588
scp -P 4588 remote@www.runoob.com:/usr/local/sin.sh /home/administrator
2. When using the scp command, make sure the user has permission to read the corresponding files on the remote server; otherwise, the scp command will not work.
Notes
- Make sure you have sufficient permissions to access the source files and destination path.
- If the remote host's SSH service is not running or is misconfigured,
scpthe command will not work properly. - Use
-voption can help you debug connection problems.
Linux Command Reference