SCP,which stands for 'secure copy command' is a utility tool used by Linux systems to copy files and directories between hosts in the network. SSH is used to transfer files and authenticate users. As you require credentials to access a remote server using ssh, in a similar way you need to know the credentials of the remote hosts while running SCP commands.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux SSH queries.
In this context, we shall look into some useful SCP commands.
1. How to Copy file from one server to another ?
To copy a single file from one server to another in the network, use the following command:
$ scp file.txt email@example.com:/root/Linuxapt
Where , file.txt is the file name, root is the remote host's username ,192.168.100.10 is the remote server's address and /root/Linuxapt is remote directory.
2. How to Find the copy status ?
Scp command along with option -v can be used to show the copy status:
$ scp -v file firstname.lastname@example.org:/root/Linuxapt
3. How to Preserve file attributes
Once we copy files/folders to the destination server, the copied files will take the destination server's latest timestamp. Use SCP command with option -rvp to preserve attributes of file such as permission, timestamp:
$ scp -rvp file email@example.com:/root/Linuxapt
4. How to Hide SCP output ?
Scp outputs including warnings, errors, and progress meter can be suppressed by using SCP with option -q as:
$ scp -q file firstname.lastname@example.org:/root/Linuxapt
5. How to Transfer file using a random port ?
Scp command with option -P can be used to transfer the files using a different port other than the default 22 port. Specify the SSH port after -P command and run SCP command as:
$ scp -P 1022 file email@example.com:/root/Linuxapt
Where 1022 is the custom SSH port of the remote host.
6. How to Use an authentication key instead of password ?
If the remote host is configured with ssh key authentication instead of a password then you need to use the ssh key file to access the remote host. To transfer the files/directories specify the ssh key file and run SCP command with option -i as:
$ scp -i private.pem file firstname.lastname@example.org:/root/Linuxapt
Where private.pem is the key file for remote server authentication.
7. How to Limit bandwidth ?
The bandwidth of file transfer can be limited using SCP command with option -l. In this example, I have limited bandwidth to 200kbit/s:
$ scp -l 200 centos-iso.tar.gz email@example.com:/root/Linuxapt
8. How to Copy file from a remote host ?
Files/Folders can be copied to localhost from a remote server using SCP command as:
$ scp firstname.lastname@example.org:/root/Linuxapt/file
Where root is the username of the remote machine 192.168.100.10, /root/Linuxapt is the remote directory and . is the current directory in the local server.
Scp command with option -r can be used to copy directories from the remote host as:
$ scp -r email@example.com:/root/Linuxapt
Where Linuxapt is the name of the remote directory.
9. How to Transfer files based in IPV4 only ?
Only IPV4 address can be used to contact remote hosts to transfer files/directories using SCP command with option -4 as:
$ scp -4 file firstname.lastname@example.org:/root/Linuxapt
If you need to use the IPV6 address only, run the SCP command as:
$ scp -6 file email@example.com:/root/Linuxapt
10. How to Disable strict file checking ?
Strict file checking while copying files/folders from remote host to the local server can be disabled by using SCP command with option -T as:
$ scp -T firstname.lastname@example.org:/root/Linuxapt/documents.tar.gz .
Where 192.168.100.10 is the remote host and documents.tar.gz is the filename.
11. How to Compress files/folders for faster transfer ?
Files/Directories can be compressed using SCP command with option -C while copying to a remote server. The compression of the file takes place at the network level and at the destination, the file size will be the same as the source:
$ scp -C centos.iso.tar.gz email@example.com:/root/Linuxapt
12. How to Copy Directory instead of files ?
You can copy directory instead of individual files separately using SCP command with option -r as:
$ scp -r example firstname.lastname@example.org:/tmp
Where example is the name of the folder.
13. How to Copy multiple files ?
Multiple files can be copied to a remote server using SCP command by specifying the name of the files as:
$ scp file file1 file2 file3 file4 email@example.com:/root/Linuxapt
Where file file1 file2 file3 and file4 is the filename.
14. How to Use another cipher to encrypt files/folders ?
During the file transfer, Linux uses AES-128 algorithm to encrypt the files. Other encryption algorithms can be used using SCP command with option -c. Here, we used 3des-cbc cipher to encrypt the files:
$ scp -c 3des-cbs file firstname.lastname@example.org:/root/Linuxapt
15. How to perform Remote to remote host copy ?
Files/Directories can be copied from one remote host(example host1) to another remote host (host2) from localhost (host) as:
$ scp email@example.com:/root/Linuxapt/file firstname.lastname@example.org:/root/Linuxapt
This article covers some useful SCP commands to copy files/folders between hosts in the network. Linux administrator should be familiar with CLI environment. Since GUI mode in Linux servers is not a common to be installed. SSH may the most popular protocol to enable Linux administrator to manage the servers via remote in secure way. Built-in with SSH command there is SCP command. SCP is used to copy file(s) between servers in secure way.
SCP Basic syntax:
scp [options] username1@source_host:/location1/file1 username2@destination_host:/location2/file2
Some common scp command options include: