SCP Command on Linux: 30 Example Commands

Secure Copy Protocol (SCP) is a command-line tool that transfers files securely between remote servers using the SSH (Secure Shell) protocol. It is a convenient and secure way to move files between servers, particularly for system administrators who manage multiple servers. This guide will cover 30 useful SCP commands that will help you transfer files between servers efficiently and securely.

What is SCP?

SCP is a file transfer protocol that securely copies files between remote hosts. SCP is based on the SSH protocol and encrypts data and authentication information, and SCP allows users to transfer files between hosts and across networks securely. SCP uses the same authentication and security mechanisms as SSH, meaning users do not need to provide additional authentication information when using SCP.

SCP provides a simple and secure way to transfer files between servers, and it is a popular tool among system administrators. SCP is installed by default on most Linux distributions, making it an accessible tool for anyone who needs to transfer files securely. SCP commands are similar to the basic Linux commands, and users familiar with the Linux command line should be able to use SCP easily. The next section will cover 30 useful SCP commands to help you transfer files between servers efficiently and securely.

Basic SCP Commands

Here are some basic SCP commands that can help you transfer files securely between local and remote servers.

1. Copy a file from the local to a remote server

This command copies a file from the local server to a remote server.

scp /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred.

2. Copy a file from the remote to the local server

This command copies a file from a remote server to the local server.

scp user@remote:/path/to/remote/file /path/to/local/directory/

In the above command, replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/file with the path of the file on the remote server that needs to be transferred. Replace /path/to/local/directory/ with the directory on the local server where the file will be transferred.

3. Copy a directory from the local to a remote server

This command copies a directory and its contents from the local server to a remote server.

scp -r /path/to/local/directory user@remote:/path/to/remote/directory/

In the above command, replace -r with the option to copy directories recursively. Replace /path/to/local/directory with the path of the directory on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the directory will be transferred.

4. Copy a directory from the remote to the local server

This command copies a directory and its contents from a remote server to the local server.

scp -r user@remote:/path/to/remote/directory /path/to/local/directory/

In the above command, replace -r with the option to copy directories recursively. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory with the path of the directory on the remote server that needs to be transferred. Replace /path/to/local/directory/ with the directory on the local server where the directory will be transferred.

Advanced SCP Commands

Here are some advanced SCP commands that can help you transfer files securely between remote servers and manage your files more efficiently.

5. Copy a file from one remote server to another remote server

This command copies a file from one remote server to another remote server.

scp user1@remote1:/path/to/file user2@remote2:/path/to/directory/

In the above command, replace user1 with the username used to log in to the first remote server (remote1). Replace remote1 with the IP address or hostname of the first remote server. Replace /path/to/file with the path of the file on the first remote server that needs to be transferred. Replace user2 with the username used to log in to the second remote server (remote2). Replace remote2 with the IP address or hostname of the second remote server. Replace /path/to/directory/ with the directory on the second remote server where the file will be transferred.

6. Copy a file using a non-standard SSH port

This command copies a file from the local server to a remote server using a non-standard SSH port.

scp -P 2222 /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -P 2222 with the option to specify a non-standard SSH port (2222 in this example). Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred.

7. Copy a file using a different SSH identity file

This command copies a file from the local server to a remote server using a different SSH identity file.

scp -i /path/to/private/key /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -i /path/to/private/key with the option to specify a different SSH identity file (/path/to/private/key in this example). Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred.

8. Copy a file while preserving file attributes

This command copies a file from the local server to a remote server while preserving its file attributes like permissions, modification time, and access time.

scp -p /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -p with the option to preserve file attributes. Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred.

9. Copy a file with verbose output

This command copies a file from the local server to a remote server with verbose output, showing the progress of the transfer.

scp -v /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -v with the option to display a verbose output. Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred.

10. Copy a file with the quiet output

This command copies a file from the local server to a remote server with quiet output, suppressing normal output.

scp -q /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -q with the option to display quiet output. Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred.

SCP Commands for Secure File Transfer

Here are some SCP commands that can help you transfer files securely between remote servers and protect your files from unauthorized access.

11. Copy a file securely using SSH key authentication

This command copies a file from the local server to a remote server using SSH key authentication for secure and passwordless authentication.

scp -i /path/to/private/key /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace /path/to/private/key with the path of the private SSH key used for authentication. Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred.

12. Copy a file securely using SCP with encryption

This command copies a file from the local server to a remote server using SCP with encryption to protect the data during the transfer.

scp -c aes256 /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -c aes256 with the option to specify the encryption algorithm (aes256 in this example). Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred.

13. Copy a file securely using SCP with compression

This command copies a file from the local server to a remote server using SCP with compression to reduce the data size during the transfer.

scp -C /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -C with the option to enable compression. Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace `/path/to/remote/directory/` with the directory on the remote server where the file will be transferred.

14. Copy a file securely using SCP with port forwarding

This command copies a file from the local server to a remote server using SCP with port forwarding to transfer the data through a firewall securely.

scp -P 2222 -o ProxyCommand="ssh -W %h:%p user@firewall" /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -P 2222 with the option to specify the SSH port. Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred. Replace the firewall with the hostname or IP address of the firewall server. This command creates an SSH tunnel through the firewall server using the ProxyCommand option to transfer the file securely.

15. Copy a file securely using SCP with two-factor authentication

This command copies a file from the local server to a remote server using SCP with two-factor authentication to provide an extra layer of security.

scp -o PubkeyAuthentication=yes -o KeyboardInteractiveAuthentication=yes /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred. This command uses two-factor authentication to log in to the remote server using SSH public key and keyboard-interactive authentication.

SCP Commands for File Synchronization

Here are some SCP commands that can help you synchronize files between local and remote servers and keep them up-to-date.

16. Synchronize a directory from the local to a remote server

This command synchronizes a directory and its contents from the local server to a remote server, updating only the changed files.

scp -r --delete /path/to/local/directory user@remote:/path/to/remote/directory/

In the above command, replace -r with the option to copy directories recursively. Replace –delete with the option to delete files on the remote server that do not exist on the local server. Replace /path/to/local/directory with the path of the directory on the local server that needs to be synchronized. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the directory will be synchronized.

17. Synchronize a directory from the remote to the local server

This command synchronizes a directory and its contents from a remote server to the local server, updating only the changed files.

scp -r --delete user@remote:/path/to/remote/directory /path/to/local/directory

In the above command, replace -r with the option to copy directories recursively. Replace –delete with the option to delete files on the local server that do not exist on the remote server. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server that needs to be synchronized. Replace /path/to/local/directory with the directory on the local server where the directory will be synchronized.

18. Synchronize a directory between two remote servers

This command synchronizes a directory and its contents between two remote servers, updating only the changed files.

scp -3 user1@remote1:/path/to/directory user2@remote2:/path/to/directory

In the above command, replace user1 with the username used to log in to the first remote server (remote1). Replace remote1 with the IP address or hostname of the first remote server. Replace /path/to/directory with the path of the directory on the first remote server that needs to be synchronized. Replace user2 with the username used to log in to the second remote server (remote2). Replace remote2 with the IP address or hostname of the second remote server. Replace /path/to/directory with the directory on the second remote server that needs to be synchronized.

19. Synchronize a directory with exclude list

This command synchronizes a directory and its contents from the local server to a remote server, excluding certain files or directories.

scp -r --exclude=*.log --exclude=/tmp/* /path/to/local/directory user@remote:/path/to/remote/directory/

In the above command, replace -r with the option to copy directories recursively. Replace –exclude=*.log with the option to exclude files with a certain extension (in this case, *.log). Replace –exclude=/tmp/* with the option to exclude directories (in this case, /tmp/). Replace /path/to/local/directory with the path of the directory on the local server that needs to be synchronized. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the directory will be synchronized.

20. Synchronize a directory, include a list

This command synchronizes a directory and its contents from the local server to a remote server, including only certain files or directories.

scp -r --include=*.txt --include=*/ --exclude=* /path/to/local/directory user@remote:/path/to/remote/directory/

In the above command, replace -r with the option to copy directories recursively. Replace –include=*.txt with the option to include files with a certain extension (in this case, *.txt). Replace –include=*/ with the option used to include directories. Replace –exclude=* with the option used to exclude everything else. Replace /path/to/local/directory with the path of the directory on the local server that needs to be synchronized. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the directory will be synchronized.

SCP Commands for Resuming Interrupted Transfers

Here are some SCP commands that can help you resume interrupted transfers and avoid re-transferring large files.

21. Resume an interrupted upload

This command resumes an interrupted upload of a large file from the local server to a remote server.

scp -r /path/to/local/file user@remote:/path/to/remote/directory/

In case the upload was interrupted, you can resume the transfer by running the same command again.

22. Resume an interrupted download

This command resumes an interrupted download of a large file from a remote server to the local server.

scp -r user@remote:/path/to/remote/file /path/to/local/directory/

In case the download was interrupted, you can resume the transfer by running the same command again.

23. Check file integrity after interrupted transfer

This command checks the integrity of a file transfer using SCP after an interrupted transfer.

scp -r -C /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -C with the option to enable compression. Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred. This command checks the file’s integrity by comparing its checksum before and after the transfer.

SCP Commands for Advanced Users

Here are some SCP commands that can help advanced users customize their file transfers and optimize performance.

24. Set SCP buffer size

This command sets the buffer size for SCP transfers, optimizing performance for large files.

scp -B 8192 /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -B 8192 with the option to set the buffer size in bytes (8192 in this example). Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred.

25. Limit SCP transfer rate

This command limits the transfer rate for SCP transfers, preventing network congestion and optimizing performance.

scp -l 1024 /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -l 1024 with the option to set the maximum transfer rate in kilobits per second (1024 in this example). Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred.

26. Transfer multiple files

This command transfers multiple files from the local server to a remote server using a wildcard character (*) to match a pattern.

scp /path/to/local/files/*.txt user@remote:/path/to/remote/directory/

In the above command, replace /path/to/local/files/*.txt with the path of the files on the local server that match the pattern *.txt (all files with the .txt extension in this case). Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the files will be transferred.

27. Transfer files between two remote servers

This command transfers files directly between two remote servers, avoiding the need to transfer files through the local server.

scp user1@remote1:/path/to/file user2@remote2:/path/to/directory/

In the above command, replace user1 with the username used to log in to the first remote server (remote1). Replace remote1 with the IP address or hostname of the first remote server. Replace /path/to/file with the path of the file on the first remote server that needs to be transferred. Replace user2 with the username used to log in to the second remote server (remote2). Replace remote2 with the IP address or hostname of the second remote server. Replace /path/to/directory/ with the directory on the second remote server where the file will be transferred.

28. Transfer files with custom SSH options

This command transfers files using custom SSH options, allowing advanced users to configure SSH connections according to their needs.

scp -o ConnectTimeout=30 -o StrictHostKeyChecking=no /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -o ConnectTimeout=30 with the option to set the connection timeout to 30 seconds. Replace -o StrictHostKeyChecking=no with the option to disable strict host key checking. Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred.

29. Transfer files with verbose output

This command transfers files with verbose output, providing detailed information about the transfer process.

scp -v /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -v with the option to enable verbose output. Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred.

30. Transfer files with compression

This command transfers files with compression, reducing the file size and optimizing transfer speed.

scp -C /path/to/local/file user@remote:/path/to/remote/directory/

In the above command, replace -C with the option to enable compression. Replace /path/to/local/file with the path of the file on the local server that needs to be transferred. Replace the user with the username to log in to the remote server. Replace the remote with the IP address or hostname of the remote server. Replace /path/to/remote/directory/ with the directory on the remote server where the file will be transferred.

Conclusion

SCP is a powerful tool for securely transferring files between servers. With these 30 SCP commands, you can customize your file transfers according to your needs, optimize performance, and avoid common errors. Whether a novice or an advanced user, SCP can help you simplify your file transfers and keep your data safe.

Frequently Asked Questions

Q: What is the difference between SCP and SFTP?

A: Both SCP and SFTP are file transfer protocols that use SSH for security. The main difference between SCP and SFTP is that SCP is a simple protocol that can only transfer files. At the same time, SFTP is a more complex protocol that can perform various file operations, such as renaming, deleting, and creating directories.

Q: Is SCP faster than FTP?

A: SCP is generally faster than FTP because it uses compression and encryption to transfer files. However, the speed of file transfers depends on various factors, such as the size of the files, the network speed, and the server load.

Q: Can SCP transfer files between different operating systems?

A: Yes, SCP can transfer files between different operating systems as long as the remote servers have SCP installed and the user has the necessary permissions to access the files.

Q: How do I transfer a large file using SCP?

A: To transfer a large file using SCP, you can use the -C option to enable compression and the -l option to limit the transfer rate. You can also use the -P option to specify a non-default SSH port if necessary.

Q: How do I troubleshoot SCP connection errors?

A: If you are experiencing SCP connection errors, you can try to diagnose the problem by checking the SSH logs on the remote server, verifying the server hostname and IP address, checking the firewall settings, and testing the SSH connection using the ssh command.

Q: How do I securely transfer sensitive files using SCP?

A: To securely transfer sensitive files using SCP, you should use encryption and authentication mechanisms provided by SSH, such as public key authentication, password authentication, and port forwarding. You should also ensure that the file permissions and ownership are set correctly on both the source and destination servers.

Q: Can I transfer multiple files using SCP?

A: Yes, you can transfer multiple files using wildcards or create an archive file and transfer it using SCP.

Q: Can SCP transfer files between two remote servers?

A: Yes, SCP can transfer files directly between two remote servers without transferring files through the local server. To transfer files between two remote servers, you need SSH access to both servers.

Q: Can I resume an interrupted SCP transfer?

A: Yes, you can resume an interrupted SCP transfer by running the same SCP command again. SCP will detect the partially transferred files and continue the transfer from where it left off.

Q: How do I verify the integrity of a transferred file using SCP?

A: To verify the integrity of a transferred file using SCP, you can use the -c option to compare the checksum of the local and remote files. If the checksums match, the transfer is successful.

Q: Can I transfer files with custom SSH options using SCP?

A: Yes, you can transfer files with custom SSH options using SCP by the -o option to specify the SSH options.

Q: Is SCP the most secure way to transfer files?

A: SCP is a secure way to transfer files, but it may not be the most secure way depending on the specific security requirements of your organization. In some cases, other secure file transfer protocols, such as SFTP and FTPS, may be more suitable.

Q: How do I transfer files using SCP with specific permission or ownership?

A: To transfer files with specific permission or ownership using SCP, you can use the chmod and chown commands to set the permissions and ownership on the source files and then transfer the files using SCP.

Q: Using SCP, how do I transfer files with a specific time stamp?

A: SCP does not preserve the time stamp of the files by default. However, you can use the -p option to preserve the time stamp of the files during the transfer.

Q: Can I use SCP to transfer files over a VPN?

A: Yes, you can use SCP to transfer files over a VPN if the VPN connection allows SSH traffic. You can transfer files between the local machine and the remote server as if on the same network.

Share to...