The scp command in Linux is used to transfer files between machines over a secure, encrypted connection. It is similar to rcp.
The general syntax to transfer a local file to a remote system is as follows:
-----
scp source_file_name username@destination_host:destination_folder
-----
The <source_file_name> specifies the source including path to the file, such as /var/log/maillog.
The <destination_folder> specifies the destination, which can be a new filename such as /tmp/hostname-maillog.
For the remote system, if you do not have a preceding /, the path will be relative to the home directory of username, typically /home/username/.
Some examples of scp command :
1. Verbose output :
Using verbose output, the scp program gives lots of information about what happens in background. This is very useful when the program fails or is unable to complete the request. The verbose output would then indicate the exact point where the program ran into issues.
-----
# scp -v ~/testfile.txt root@15x.x.45.x:/root/demo.txt
-----
2. Transfer multiple files :
-----
# scp text1.txt text2.txt username@remotehost:/path/directory/
-----
3. Copy entire directory (recursively) :
To copy an entire directory from one host to another use the r switch and specify the directory
-----
# scp -v -r ~/var/test username@remotehost:/path/directory/
-----
4. Copy files across 2 remote hosts :
Using Scp you can copy files from 1 remote host to another remote host as well.
-----
# scp user1@remotehost1:/path/of/the/file user2@remotehost2:/path/of/the/file
-----
5. Speed up the transfer with compression :
To speed up the transfer to save time and bandwidth you can use the C option to enable compression.
-----
# scp -vrC ~/var/test username@remotehost:/path/directory/
-----
6. Limit the bandwidth usage :
If you do not want scp to take up the entire available bandwidth, then use the l option to limit the maximum speed in Kbit/s.
-----
# scp -vrC -l 400 ~/var/test username@remotehost:/path/directory/
-----
7. Connect to a different port number on remote host :
If the remote server has ssh daemon running on a different port (default is 22), then you need to tell scp to use that particular port number using the '-P' option.
-----
# scp -vC -P 2200 ~/var/test username@remotehost:/path/directory/
-----
8. Preserve file attributes :
The '-p' option (smallcase), would preserve modification times, access times, and modes from the original file.
-----
# scp -C -p ~/var/test username@remotehost:/path/directory/
-----
9. Quiet mode :
In quiet mode ( '-q' option ), the scp output would get suppressed, and would disable the progress meter as well as warning and diagnostic messages.
-----
# scp -vCq ~/var/test username@remotehost:/path/directory/
-----
10. Use different cipher :
Scp by default uses the AES cipher/encryption. You can use a different cipher can speed up the transfer process. For example blowfish and arcfour are known to be faster than AES (but less secure).
-----
# scp -c blowfish -C ~/var/test username@remotehost:/path/directory/
-----
The general syntax to transfer a local file to a remote system is as follows:
-----
scp source_file_name username@destination_host:destination_folder
-----
The <source_file_name> specifies the source including path to the file, such as /var/log/maillog.
The <destination_folder> specifies the destination, which can be a new filename such as /tmp/hostname-maillog.
For the remote system, if you do not have a preceding /, the path will be relative to the home directory of username, typically /home/username/.
Some examples of scp command :
1. Verbose output :
Using verbose output, the scp program gives lots of information about what happens in background. This is very useful when the program fails or is unable to complete the request. The verbose output would then indicate the exact point where the program ran into issues.
-----
# scp -v ~/testfile.txt root@15x.x.45.x:/root/demo.txt
-----
2. Transfer multiple files :
-----
# scp text1.txt text2.txt username@remotehost:/path/directory/
-----
3. Copy entire directory (recursively) :
To copy an entire directory from one host to another use the r switch and specify the directory
-----
# scp -v -r ~/var/test username@remotehost:/path/directory/
-----
4. Copy files across 2 remote hosts :
Using Scp you can copy files from 1 remote host to another remote host as well.
-----
# scp user1@remotehost1:/path/of/the/file user2@remotehost2:/path/of/the/file
-----
5. Speed up the transfer with compression :
To speed up the transfer to save time and bandwidth you can use the C option to enable compression.
-----
# scp -vrC ~/var/test username@remotehost:/path/directory/
-----
6. Limit the bandwidth usage :
If you do not want scp to take up the entire available bandwidth, then use the l option to limit the maximum speed in Kbit/s.
-----
# scp -vrC -l 400 ~/var/test username@remotehost:/path/directory/
-----
7. Connect to a different port number on remote host :
If the remote server has ssh daemon running on a different port (default is 22), then you need to tell scp to use that particular port number using the '-P' option.
-----
# scp -vC -P 2200 ~/var/test username@remotehost:/path/directory/
-----
8. Preserve file attributes :
The '-p' option (smallcase), would preserve modification times, access times, and modes from the original file.
-----
# scp -C -p ~/var/test username@remotehost:/path/directory/
-----
9. Quiet mode :
In quiet mode ( '-q' option ), the scp output would get suppressed, and would disable the progress meter as well as warning and diagnostic messages.
-----
# scp -vCq ~/var/test username@remotehost:/path/directory/
-----
10. Use different cipher :
Scp by default uses the AES cipher/encryption. You can use a different cipher can speed up the transfer process. For example blowfish and arcfour are known to be faster than AES (but less secure).
-----
# scp -c blowfish -C ~/var/test username@remotehost:/path/directory/
-----