2023-07-05 11:06:31 -05:00

3.5 KiB

title description synopsis date tags imageURL imageAlt
How to Transfer Files Securely with the “scp” Command How to transfer files to and from a remote Server with the scp / secure copy command How to transfer files to and from a remote Server with the scp / secure copy command 2023-06-12
GNU/Linux
Tutorial
CLI Tools
/img/terminal.svg A stylized illustration of a terminal prompt.

Copying files to and from remote servers is a common chore that can initially seem uncommonly tricky. Thankfully, we have the scp command, also known as the secure copy command, as a simple way to initiate secure transfers to and from your remote boxes.

First, make sure your permissions are in order

Just like when you ssh into your server, when using scp, you need to ensure that the user you intend to use to log in to the remote server has access to the files and directories you'll need to access on your remote machine. For instance, if you want to copy a file from a remote server that only the root user has access to, you will need to either:

  1. Specify the root user in your scp command;
  2. ssh into the remote server as root and edit the file's permissions and / or move it somewhere accessible to the user you will specify in your scp command;
  3. ssh into the remote server as a user with sudo permissions and edit the file's permissions and / or move it somewhere accessible to the user you will specify in your scp command;

Hopefully, you have disabled remote root login to your server via ssh for security reasons, so you will use option three, if necessary, prior to executing your scp command.

Using scp is very similar to using the ssh command. Both tools share some flags, and their commands look similar, the key difference being that scp is not interactive; every action you wish to take on your remote machine must be encapsulated in your scp command, which is why you must first ensure that the user specified in your scp command has access to any files or directories you want to transfer to or from.

Examples

Copying from a remote server

Just like you might when logging in via the ssh command, if you've changed your default ssh port to something other than 22, you'll need to specify it with the -P flag; if you're using key-based authentication, you'll also need to specify your key with the -i flag:

scp -P [port] -i [keyPath] [remoteUser]@[remoteServerNameOrIP]:[filePath] [downloadPath]

Copying to a remote server

When uploading, your command should look like this, with the local file path before your username and server address:

scp -P [port] -i [keyPath] [filePath] [remoteUser]@[remoteServerNameOrIP]:[uploadPath]

Other options

Copy the contents of a directory recursively using the -r flag:

scp -r [remoteUser]@[remoteServerNameOrIP]:[filePath] [downloadPath]

Copy a file between two remote hosts directly:

scp [remoteUser1]@[remoteServerNameOrIP1]:[filePath] [remoteUser2]@[remoteServerNameOrIP2]:[filePath]
Copy a file between two remote hosts, routing through your local machine:
``` bash
scp -3 [remoteUser1]@[remoteServerNameOrIP1]:[filePath] [remoteUser2]@[remoteServerNameOrIP2]:[filePath]

That's all there is to it.

And there you have it: one simple command to transfer files to and from your remote servers. So long as your remote users have the prerequisite permissions to access the correct remote files and directories, and there's nothing wrong with your ssh setup, transferring files with scp is a breeze.