Secure Shell (SSH) is a safe way of connecting to a remote machine, such as a server from the remote client. The client and server machines must have the SSH installed. Once the connection is created, you may transfer the files from the client to the remote host or vice-versa.
A reliable way of copying the files from one machine to the other is using a secure copy protocol (SCP). With SCP, you can copy the files and folders from the client to the remote host with a line of code. Similarly, you can download the files from the remote host to the local machine. This guide covers the steps to use SCP to copy the files with SSH and transfer them to the remote host.
How to Use SCP to Copy and PIPE the Files via SSH to Remote Host
SSH and SCP work together. SSH creates a secure connection between a client and a server. On the other hand, SCP comes in handy as a network file transfer protocol which facilitates a secure way of transferring the files between a local and remote host. SCP relies on the authentication created by SSH to authorize the transfer of files.
There are different options to use with SCP; you can refer to its man page. Meanwhile, let’s see the different ways of using SCP for file transfer:
1. Copying Files from Local Client to Remote Server
When connecting to a server, you may need to transfer the files to the remote server. In that case, SCP gets the job done. If you have the username and password of the remote host, you can use the following syntax to transfer the files:
Here’s an example. Running the command requires SSH authentication of the key before you proceed:
Once you authenticate the key, you must enter the password of the remote server to connect to it and transfer the files.
Once the file gets transferred, a percentage and the file’s name are displayed on the screen to confirm that the file is transferred successfully. We transfer the file1 to the remote host.
Accessing our server, we can list the directory’s contents to verify if the file is transferred. In this case, our file is transferred from the local client to the remote server.
To transfer multiple files, add their names separated by a space. The files should be in the same directory. Execute the command, like the one in the following, and SCP will transfer all included files to the remote host:
When the transfer is successful, the terminal displays the transfer percentage and all the files, as in the following case:
When copying the files, you can name them differently when copied to the remote host. For instance, in the following example, we are copy a file named linuxhint.sh and save it as coied.sh. The trick is to add the path to where you want to save the copied file and the preferred name to save it with, as shown in the following illustration:
Copying a directory is the same as copying a file. The difference is that for the directory, you must add its full path on the local host. In the following example, we are copying /copy-me:
2. Copying Files from the Remote Server and PIPE to the Local Client
The syntax for copying the files from the remote host and storing them on the local host is slightly different. You start with the SSH connection to the remote server and the path to the file or directory that you want to copy. Lastly, add the path to copy the file to the local host.
Here’s an example of copying a file named linuxhint to the current directory on the local host:
To copy a directory, add the -r option, as demonstrated in the following example. Verify that the remote directory is downloaded and stored in the specified path using the ls command:
To save the copied file or directory from the remote host with a different name, add the path where you want to save it and specify what name to use, as shown in the following:
Your target file is successfully copied and saved with the added name.
SSH supports creating a secure connection to a remote host. Moreover, you can transfer the files between a local host and the remote server with SCP. This guide presented various examples of sharing the files and directories between local and remote hosts. Try it out!