SCP commonly known as 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. In the article, we will learn some useful SCP commands.
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 protected]:/root/Linuxways
Where , file.txt is the file name, root is the remote host’s username ,192.168.178.10 is the remote server’s address and /root/Linuxways is remote directory.
Find the copy status
Scp command along with option -v can be used to show the copy status.
$ scp -v file [email protected]:/root/Linuxways
Preserving 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 protected]:/root/Linuxways
Hide SCP output
Scp outputs including warnings, errors, and progress meter can be suppressed by using SCP with option -q as:
$ scp -q file [email protected]:/root/Linuxways
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 4022 file [email protected]:/root/Linuxways
Where 4022 is the custom SSH port of the remote host.
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 [email protected]:/root/Linuxways
Where private.pem is the key file for remote server authentication
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 protected]:/root/Linuxways
Copy file from a remote host
Files/Folders can be copied to localhost from a remote server using SCP command as:
$ scp [email protected]:/root/Linuxways/file .
Where root is the username of the remote machine 192.168.178.10, /root/Linuxways 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 protected]:/root/Linuxways .
Where Linuxways is the name of the remote directory.
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 [email protected]:/root/Linuxways
If you need to use the IPV6 address only, run the SCP command as:
$ scp -6 file [email protected]:/root/Linuxways
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 [email protected]:/root/Linuxways/documents.tar.gz .
Where 192.168.178.10 is the remote host and documents.tar.gz is the filename.
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 protected]:/root/Linuxways
Copy Directory instead of files
You can copy directory instead of individual files separately using SCP command with option -r as:
$ scp -r example [email protected]:/tmp
Where example is the name of the folder.
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 protected]:/root/Linuxways
Where file file1 file2 file3 and file4 is the filename.
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. In this example, I have used 3des-cbc cipher to encrypt the files.
$ scp -c 3des-cbs file [email protected]:/root/Linuxways
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 protected]:/root/Linuxways/file [email protected]:/root/Linuxways
Where, 192.168.178.10 -> Remote host (host1)
192.168.178.11 -> Remote host (host2)
In this article, we have learned some useful SCP commands to copy files/folders between hosts in the network. If you have any suggestions and feedback, please leave a comment.