Linux Commands

SCP Remote to Local


This tutorial explains how to fetch files and directories from a remote host to our local device.

SCP or Secure Copy Protocol is an SSH-based protocol that allows sharing of files between remote devices. Using SCP, you can send a receive file from and to a local device or between remote devices.

Currently, SCP is considered outdated, and it has been replaced by SFTP and RSYNC. At the end of the SCP instructions, I added tips to download files from remote hosts using SFTP with a similar syntax as SCP.

Copy or Download a File From Remote to Local Using SCP

SCP syntax is pretty simple. Just invoke SCP followed by the remote username, @, the IP address or host, colon, and the path to the file. If not specified, the default path is the remote user’s home directory. Then, define the local path where the file will be stored locally.

The scp command was designed to be similar to the cp command. The file copy’s location is specified at the end of the command.

A simple scp example to copy a remote file to the local device would be:

scp <Username>@<IPorHost>:<PathToFile>   <LocalFileLocation>

In my case, I will copy the file named linuxhint from the remote device 192.168.1.100. The linuxhint file is stored on the kali user’s home directory, the user I will authenticate. Therefore after the colon, I don’t specify the path, which is the home directory by default, and I just type the filename (“linuxhint”). Then, I specify the current directory as the local location to store the file by typing a dot:

scp [email protected]:linuxhint .

As you can see, the linuxhint file was copied into the current local directory.

In the following example, I download the file named linuxhint2 from the remote device to the /home/linuxhint/locdir directory. The linuxhint2 file is stored in the dir directory named within the remote user (kali) home directory:

scp [email protected]:dir/linuxhint2 /home/linuxhint/locdir/

Copy or Download a Directory Recursively Using SCP

To download or copy directories from remote to local using SCP, you need to implement the -r (recursively) flag.

With the exception of the -r flag added as shown in the following example, the syntax to download directories is the same as downloading files from remote to local devices:

scp -r [email protected]:dir/ /root/

As you can see, the directory named dir was downloaded with all its content.

Upload a File or Directory From Local to Remote Using SCP

The previous section of this tutorial explained how to fetch or download files from a remote device using SCP. This section of the tutorial explains how to upload or send a file to a remote device.

The example below shows how to send or push a file named linuxhintfile to the remote device’s default directory or the user’s home. The difference with receiving files is you need to define the file to send or its path just after invoking SCP, as shown in the following screenshot.

scp linuxhintfile [email protected]:

Remember, the scp command was designed to keep the cp command syntax. Therefore, the file destination directory is always specified at the end of the command, both when sending or fetching files.

Defining directories is the same as when receiving or downloading files. The example below emphasized how to send the file named file2 and stored it under the linuxhint user home directory. The file will be saved in the remote device’s dir subdirectory, located in the default home directory.

scp /home/linuxhint/file2 [email protected]:dir/

To send directories to apply a similar syntax, just add the -r flag to send files recursively, as shown in the image below. The linuxhintdir directory is sent to the remote device’s /tmp directory:

Fetch or Download Files and Directories Using SFTP

Downloading files using SCP is an obsolete method replaced mainly by SFTP (Secure File Transference Protocol). The current proper ways to transfer files are SFTP or RSYNC. SFTP can be used in interactive mode, but this section shows how to use it with a similar syntax as SCP.

In the example below, The sftp command is used to download the file named linuxhint from the remote host to the local /tmp directory.

sftp [email protected]:linuxhint /tmp

Fetching directories recursively using SFTP requires the -r flag, just like when using the scp command, as shown in the screenshot below. The linuxhintdir directory is downloaded to the local /tmp directory.

sftp -r [email protected]:linuxhintdir /tmp

As you can see, the directory was downloaded recursively.

The SFTP command is primarily used in its interactive mode, which is deeply explained in this tutorial. Also, consider using the rsync command, another up-to-date and secure alternative to the outdated scp command.

 Conclusion

Downloading files from a remote host to a local device using SCP is probably the main choice for most Linux users, including system administrators. As you can see scp is a simple command, almost as easy to use as the cp command. We always assumed it is secure since its name says it’s secure. However, it is no longer secure, and it was flagged as deprecated by its developers.

The main alternative, the Secure File Transference Protocol or SFTP protocol, isn’t so user-friendly as the scp command in its interactive mode. That’s one of the reasons behind the scp command popularity even after being deprecated. However, users should consider adopting SFTP or RSYNC as the main choices.

Thank you for reading this tutorial, keep following Linux Hint for additional Linux tips and tutorials.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.