Securely transferring files between a local machine and a remote server is a common need, and the
scp (secure copy) command simplifies this process by leveraging SSH (Secure Shell) for secure file transfers.
scp Command on Your Machine
Firstly, you’ll need to install the
scp command on your machine. Depending on your operating system, use one of the following commands:
sudo apt-get install sshfs
sudo yum install fuse-sshfs
brew install sshfs
SCP Basic Syntax
scp command follows a basic syntax that includes various options to manage file transfers:
scp [options] [source_username@source_host:]source_file [dest_userid@dest_host:]destination_dir
Commonly used options include:
- -C: Compress the file data.
- -i: Use the specified private key for the remote system.
- -l: Set a bandwidth limit for the file transfer.
- -P: Use the specified port for SSH.
- -p: Copy over the file modification and access time.
- -q: Use quiet mode. Quiet mode suppresses the progress meter and informational messages. Error messages are still printed.
- -r: Copy directories recursively.
- -v: Print debug messages.
Examples of SCP Usage
Copying from a Remote Server to a Local Machine
To copy a file from a remote server to your local machine, use the following syntax:
scp username@remotemachineIP:/remote-path/file /local-path/
For copying multiple files using a pattern:
scp firstname.lastname@example.org:/root/dosya* /Users/hakan/
This pattern will copy files that match the specified pattern, such as
dosya2, and others.
Copying a Directory from a Remote Server to a Local Machine
When dealing with directories, include the
-r flag to ensure the entire directory and its contents are copied:
scp -r email@example.com:/root/db-init /Users/hakan/
Copying from a Local Machine to a Remote Server
For copying a file from your local machine to a remote server, use the command structure:
scp /local-path/file username@remoteIP:/remote/path
scp /Users/hakan/test firstname.lastname@example.org:/root/
Copying a Directory from a Local Machine to a Remote Server
Similarly, for directories, include the
-r flag to copy the entire directory and its contents:
scp -r /local-path username@remoteIP:/remote-path
scp -r /Users/hakan/Devops-lab/k10/ email@example.com:/root/
Copying Files Using RSA SSH Keys
If an RSA key is in place instead of a password for authentication, integrate the
-i flag to specify the identity key:
scp -i /path/to/key firstname.lastname@example.org:/remote/path/to/file /local/path
scp -i /Users/hakan/ssh/hakan.pem -r /Users/hakan/Devops-lab/hello-test/ email@example.com:/root/
By following these
scp commands, you can efficiently and securely transfer files and directories between local and remote locations, offering convenience and flexibility in managing your data across systems.