Linux scp Command
The Linux scp command is used to copy files and directories between Linux systems.
scp stands for secure copy. It is a command used in Linux systems for securely transferring files over SSH.
scp is encrypted, while rcp is not. scp is an enhanced version of rcp.
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
Simplified syntax:
scp [optional parameters] file_source file_target
Parameter Description:
-1: Force scp to use the ssh1 protocol.
-2: Force scp to use the ssh2 protocol.
-4: Force scp to use only IPv4 addressing.
-6: Force scp to use only IPv6 addressing.
-B: Use batch mode (does not ask for a passphrase during transmission).
-C: Enable compression. (Passes the -C flag to ssh, enabling compression).
-p: Preserve the original file's modification time, access time, and permissions.
-q: Suppress the progress bar.
-r: Recursively copy entire directories.
-v: Display detailed output. scp and ssh(1) will show debugging information for the entire process. This information is useful for debugging connections, authentication, and configuration issues.
-c cipher: Encrypt data transmission with the specified cipher. This option is directly passed to ssh.
-F ssh_config: Specify an alternative ssh configuration file. This parameter is directly passed to ssh.
-i identity_file: Read the key file for transmission from the specified file. This parameter is directly passed to ssh.
-l limit: Limit the bandwidth usage for the user, measured in Kbit/s.
-o ssh_option: If you are accustomed to passing parameters in the ssh_config(5) style.
-P port: Note that this is a capital P, port specifies the port number used for data transmission.
-S program: Specify the program used for encrypted transmission. This program must be able to understand ssh(1) options.
Examples
1. Copy from Local to Remote
Command format:
scp 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
The first and second specify a username, and a password needs to be entered after the command is executed. The first specifies only the remote directory, with the file name remaining unchanged, while the second specifies the file name.
The third and fourth do not specify a username, and both a username and password need to be entered after the command is executed. The third specifies only the remote directory, with the file name remaining unchanged, while the fourth 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.tutorialpro.org:/home/root/others/music
scp /home/space/music/1.mp3 www.tutorialpro.org:/home/root/others/music/001.mp3
Directory copy command format:
scp -r local_folder remote_username@remote_ip:remote_folder
or
scp -r local_folder remote_ip:remote_folder
The first specifies a username, and a password needs to be entered after the command is executed.
The second does not specify a username, and both a username and password need to be entered after the command is executed.
Application example:
scp -r /home/space/music/ [email protected]:/home/root/others/
scp -r /home/space/music/ www.tutorialpro.org:/home/root/others/
The above commands copy the local music directory to the remote others directory.
2. Copy from Remote to Local
To copy from a remote location to a local one, simply reverse the order of the last two parameters of the command used to copy from local to remote, as shown in the examples below.
Application Examples:
scp [email protected]:/home/root/others/music /home/space/music/1.mp3
scp -r www.tutorialpro.org:/home/root/others/ /home/space/music/
Explanation
If the remote server's firewall has set a specific port for the scp command, we need to use the -P parameter to specify the port number in the command, as follows:
# scp command using port number 4588 scp -P 4588 [email protected]:/usr/local/sin.sh /home/administrator
Ensure that the user using the scp command has the necessary permissions to read the corresponding files on the remote server; otherwise, the scp command will not work.