The Linux scp command is used to copy files and directories between Linux systems.
\n\nscp is the abbreviation for secure copy. It is a secure remote file copy command under Linux systems based on SSH login.
\n\nscp is encrypted, rcp is not encrypted, scp is an enhanced version of rcp.
\n\nIn Linux systems, the scp command is a very practical tool. scp is an abbreviation for "secure copy", and it is based on the SSH (Secure Shell) protocol to ensure the security of data transmission.
Syntax
\n\nscp [[user@]host1:]file1 [...] [[user@]host2:]file2\n\n
Simple syntax:
\n\nscp\n\n
- \n
: Specifies the behavior of thescpcommand, such as recursive copying, preserving file attributes, etc. \n: The path of the file or directory to copy.\n: The target path where the file or directory should be copied to.\n
Common Options:
\n\nHere are some commonly used scp command options:
- \n
-r: Recursively copy entire directories. \n-P: Specify the SSH port number of the remote host (default is 22). \n-p: Preserve the modification time, access time, and permissions of files. \n-v: Display detailed debugging information, which helps in troubleshooting. \n-C: Enable compression, which can speed up transfer. \n
Complete Option Description:
\n\n- \n
- -1: Force scp command to use protocol ssh1 \n
- -2: Force scp command to use protocol ssh2 \n
- -4: Force scp command to use only IPv4 addressing \n
- -6: Force scp command to use only IPv6 addressing \n
- -B: Use batch mode (do not ask for transfer password or passphrase during transfer) \n
- -C: Allow compression. (Pass the -C flag to ssh, enabling compression) \n
- -p: Preserve the original file's modification time, access time, and access permissions. \n
- -q: Do not display transfer progress bar. \n
- -r: Recursively copy entire directories. \n
- -v: Display output in verbose mode. scp and ssh(1) will show debugging information for the entire process. This information is used for debugging connections, authentication, and configuration issues. \n
- -c cipher: Encrypt data transfer using cipher, this option is directly passed to ssh. \n
- -F ssh_config: Specify an alternative ssh configuration file, this parameter is directly passed to ssh. \n
- -i identity_file: Read the key file used for transfer from the specified file, this parameter is directly passed to ssh. \n
- -l limit: Limit the bandwidth that users can use, in Kbit/s. \n
- -o ssh_option: If accustomed to using the parameter passing method in ssh_config(5), \n
- -P port: Note that it is uppercase P, port specifies the port number used for data transfer \n
- -S program: Specify the program used for encrypted transfer. This program must be able to understand the options of ssh(1). \n
Copy Files from Local to Remote Host
\n\nAssuming 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/\n\n
In this command:
\n\n- \n
example.txtis the local file. \nuser@remote_hostis the username and address of the remote host. \n/home/user/is the target path on the remote host. \n
Copy Files from Remote Host to Local
\n\nIf you want to copy the file example.txt from the remote host to the local current directory, you can use the following command:
scp user@remote_host:/home/user/example.txt .\n\n
In this command:
\n\n- \n
user@remote_host:/home/user/example.txtis the file path on the remote host. \n.represents the current directory. \n
Recursively Copy Directories
\n\nIf you want to copy an entire directory and its contents, you can use the -r option. For example, to copy the local my_directory directory to the /home/user/ directory on the remote host:
scp -r my_directory user@remote_host:/home/user/\n\n
Specify Port Number
\n\nIf the SSH service on the remote host runs on a non-default port (e.g., 2222), you can use the -P option to specify the port number:
scp -P 2222 example.txt user@remote_host:/home/user/\n\n
Preserve File Attributes
\n\nIf you want to preserve the modification time, access time, and permissions of files when copying, you can use the -p option:
scp -p example.txt user@remote_host:/home/user/\n\n
Enable Compression
\n\nFor 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/\n\n
\n\n
Application Examples
\n\n1. Copy from Local to Remote
\n\nCommand format:
\n\nscp local_file remote_username@remote_ip:remote_folder or scp local_file remote_username@remote_ip:remote_file or scp local_file remote_ip:remote_folder or scp local_file remote_ip:remote_file\n\n
- \n
- The first and second specify the username, and the command will prompt for a password after execution. The first specifies only the remote directory, keeping the filename unchanged; the second specifies the filename. \n
- The third and fourth do not specify the username, and the command will prompt for both username and password after execution. The third specifies only the remote directory, keeping the filename unchanged; the fourth specifies the filename. \n
Application examples:
\n\nscp /home/space/music/1.mp3 root@www..com:/home/root/others/music scp /home/space/music/1.mp3 root@www..com:/home/root/others/music/001.mp3 scp /home/space/music/1.mp3 www..com:/home/root/others/music scp /home/space/music/1.mp3 www..com:/home/root/others/music/001.mp3\n\n
Command format for copying directories:
\n\nscp -r local_folder remote_username@remote_ip:remote_folder or scp -r local_folder remote_ip:remote_folder\n\n
- \n
- The first specifies the username, and the command will prompt for a password after execution. \n
- The second does not specify the username, and the command will prompt for both username and password after execution. \n
Application examples:
\n\nscp -r /home/space/music/ root@www..com:/home/root/others/ scp -r /home/space/music/ www..com:/home/root/others/\n\n
The above commands copy the local music directory to the remote others directory.
2. Copy from Remote to Local
\n\nTo copy from remote to local, simply swap the last two parameters of the command used for copying from local to remote, as shown in the following examples.
\n\nApplication examples:
\n\nscp root@www..com:/home/root/others/music /home/space/music/1.mp3 scp -r www..com:/home/root/others/ /home/space/music/\n\n
Notes
\n\n1. If the remote server firewall has set a specific port for the scp command, we need to use the -P parameter to set the port number for the command, as shown in the following command format:
\n\n#scp command uses port number 4588 scp -P 4588 remote@www..com:/usr/local/sin.sh /home/administrator\n\n
2. When using the scp command, ensure that the user has read permissions for the corresponding files on the remote server; otherwise, the scp command will not work.
\n\n\n\n
Precautions
\n\n- \n
- Ensure you have sufficient permissions to access the source files and target paths. \n
- If the SSH service on the remote host is not running or is misconfigured, the
scpcommand will not function properly. \n - Using the
-voption can help you debug connection issues. \n
YouTip