How to Copy Files Between Machines Using SSH Access

Hakan Bayraktar
3 min readNov 28, 2023

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.

Installing 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:

For Ubuntu/Debian:

sudo apt-get install sshfs

For CentOS/RHEL:

sudo yum install fuse-sshfs

For macOS:

brew install sshfs

SCP Basic Syntax

The 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 root@134.209.237.239:/root/dosya* /Users/hakan/

This pattern will copy files that match the specified pattern, such as dosya1, 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 root@134.209.237.239:/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

example:

scp /Users/hakan/test root@134.209.237.239:/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

example:

scp -r /Users/hakan/Devops-lab/k10/ root@134.209.237.239:/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 username@example.com:/remote/path/to/file /local/path

example:

scp -i /Users/hakan/ssh/hakan.pem -r /Users/hakan/Devops-lab/hello-test/ root@134.209.237.239:/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.

--

--