The scp
command in Linux is used to securely transfer files between a local host and a remote host or between two remote hosts over an SSH (Secure Shell) connection. It provides a secure and efficient way to copy files and directories between different machines.
Here’s an overview of how to use the scp
command:
- Copying Files from Local to Remote:
To copy a file from your local machine to a remote machine, use the following syntax:
scp source_file username@remote_host:destination_directory
For example, to copy a file named file.txt
to the home directory of a remote machine with the IP address 192.168.0.100
using the username user
, you would use:
scp file.txt user@192.168.0.100:~
- Copying Files from Remote to Local:
To copy a file from a remote machine to your local machine, use the following syntax:
scp username@remote_host:source_file destination_directory
For example, to copy a file named file.txt
from the home directory of a remote machine to the current directory on your local machine, you would use:
scp user@192.168.0.100:file.txt .
- Copying Files between Remote Hosts:
To copy a file between two remote machines, use the following syntax:
scp username1@remote_host1:source_file username2@remote_host2:destination_directory
For example, to copy a file named file.txt
from remote_host1
to remote_host2
, you would use:
scp user1@192.168.0.100:file.txt user2@192.168.0.200:~
- Copying Directories:
To copy an entire directory and its contents, use the-r
(recursive) option. For example:
scp -r directory username@remote_host:destination_directory
- Specifying Custom SSH Port:
If the SSH server on the remote host is listening on a non-default port, you can specify the port using the-P
option. For example:
scp -P 2222 file.txt username@remote_host:~
- Examples:
- Copying a file from local to remote:
scp file.txt user@192.168.0.100:~
- Copying a file from remote to local:
scp user@192.168.0.100:file.txt .
- Copying a file between two remote hosts:
scp user1@192.168.0.100:file.txt user2@192.168.0.200:~
- Copying a directory and its contents:
scp -r directory user@192.168.0.100:~
- Copying a file specifying a custom SSH port:
scp -P 2222 file.txt user@192.168.0.100:~
The scp
command provides a secure and efficient way to transfer files between different machines over an SSH connection. It is commonly used for remote file backup, synchronization, and copying files to remote servers.
For more information about the scp
command and its options, you can refer to the manual page by typing man scp
in your terminal.