
{{ $('Map tags to IDs').item.json.title }}
How to Copy Files over SSH with scp
The scp
command (secure copy) is a method to securely transfer files between a local machine and a remote server or between two remote servers using SSH (Secure Shell). This tutorial will guide you through the basic usage and options of the scp
command for efficient file transfers.
1. Basic Syntax of scp
The basic syntax of the scp
command is as follows:
scp [options] source destination
Where source
is the path of the file to copy, and destination
is where it will be copied to.
2. Copying Files from Local to Remote
To copy a file from your local machine to a remote server, use the following command:
scp /path/to/local/file username@remote_host:/path/to/remote/directory/
In this command:
- username: Your username on the remote server.
- remote_host: The IP address or hostname of the remote server.
- path/to/remote/directory: The destination directory on the remote server.
3. Copying Files from Remote to Local
To copy a file from a remote server to your local machine, reverse the source and destination:
scp username@remote_host:/path/to/remote/file /path/to/local/directory/
This command will download the specified file from the remote server to your local directory.
4. Using Recursive Option
If you want to copy entire directories, use the -r
option with scp
:
scp -r /path/to/local/directory username@remote_host:/path/to/remote/directory/
This command will copy the entire directory and its contents to the remote server.
5. Copying Files Between Two Remote Hosts
You can also use scp
to copy files directly between two remote servers:
scp username1@remote_host1:/path/to/file username2@remote_host2:/path/to/destination/
This requires SSH access to both remote servers.
6. Specifying Port Number
If your SSH server runs on a non-standard port (i.e., not 22), use the -P
option to specify the port:
scp -P port_number /path/to/local/file username@remote_host:/path/to/remote/directory/
7. Conclusion
By following this tutorial, you have learned how to use scp
for securely transferring files between local and remote systems via SSH. This command is essential for managing file transfers in a secure and efficient manner. Continue to explore the various options and features of scp
to enhance your file management capabilities in your Linux environment!