SSH can be used not only for remote login but also for file transfer in a secure way. There are two protocols for transferring files with SSH – SCP (Secure Copy Protocol) and SFTP (Secure File Transfer Protocol). In this section, we'll explain SCP.
To transfer a file from the local computer to the remote server, the following five sets of information are needed for the SCP command.
For a better understanding, please go through the following practice section.
The following are the paths of the file to be transferred and the destination directory.
Note: For this practice, open two terminals (command lines). One for a local operation and another one for a remote operation.
Create a directory for receiving a file.
For this operation, use the terminal connected to the remote server.
cd ~
mkdir dir_ch7_remote
Create a sample file for transferring to the remote server.
For this operation, use the local terminal that is not connected to the remote server.
cd ~
mkdir dir_ch7_local
cd dir_ch7_local
touch sample.txt
Run the scp
command with the private key path, source file path, and destination directory path.
scp -i ~/.ssh/id_rsa sample.txt ubuntu@18.143.143.190:dir_ch7_remote
sample.txt 100% 0 0.0KB/s 00:00
If you set a passphrase for the private key, you need to enter the passphrase to move forward.
Using the terminal connected to the remote server, check if the file is successfully transferred.
ls dir_ch7_remote
You can see that the file is successfully transferred.
sample.txt
When you want to transfer a directory with files underneath, use the -r
option. The following is the example command.
Command Line - INPUT
scp -i ~/.ssh/id_rsa -r sample_dir ubuntu@18.143.143.190:dir_ch7_remote
To transfer a file from the remote server to the local computer, the following five sets of information are needed for the SCP command.
For a better understanding, please go through the following practice section.
The following are the paths of the directory to be transferred and the destination directory.
Note: This practice can be done only with the local terminal.
Run the scp
command with the private key path, source directory path, and destination directory path.
cd ~
scp -i ~/.ssh/id_rsa -r ubuntu@18.143.143.190:dir_ch7_remote dir_ch7_local
sample.txt 100% 0 0.0KB/s 00:00
If you set a passphrase for the private key, you need to enter the passphrase to move forward.
Check if the directory is successfully transferred from the remote server to the local computer.
ls dir_ch7_local
You can see that the file is successfully transferred.
dir_ch7_remote sample.txt
There are two typical mistakes you might make when you run the scp
command.
1. Forgetting to add : (colon)
When you forget to add : (colon)
, the command will have a different meaning. The command will create a file copy on your local computer. For example, if you run the following command (without a colon at the end), you'll create a file named ubuntu@18.143.143.190 in the same directory.
scp -i ~/.ssh/id_rsa sample.txt ubuntu@18.143.143.190
ubuntu@18.143.143.190
2. Misusing spacing
Omitting or adding an extra space is also a common mistake. Check spaces when you encounter an error message in your command line.
SSH can be used not only for remote login but also for file transfer in a secure way. There are two protocols for transferring files with SSH – SCP (Secure Copy Protocol) and SFTP (Secure File Transfer Protocol). In this section, we'll explain SCP.
To transfer a file from the local computer to the remote server, the following five sets of information are needed for the SCP command.
For a better understanding, please go through the following practice section.
The following are the paths of the file to be transferred and the destination directory.
Note: For this practice, open two terminals (command lines). One for a local operation and another one for a remote operation.
Create a directory for receiving a file.
For this operation, use the terminal connected to the remote server.
cd ~
mkdir dir_ch7_remote
Create a sample file for transferring to the remote server.
For this operation, use the local terminal that is not connected to the remote server.
cd ~
mkdir dir_ch7_local
cd dir_ch7_local
touch sample.txt
Run the scp
command with the private key path, source file path, and destination directory path.
scp -i ~/.ssh/id_rsa sample.txt ubuntu@18.143.143.190:dir_ch7_remote
sample.txt 100% 0 0.0KB/s 00:00
If you set a passphrase for the private key, you need to enter the passphrase to move forward.
Using the terminal connected to the remote server, check if the file is successfully transferred.
ls dir_ch7_remote
You can see that the file is successfully transferred.
sample.txt
When you want to transfer a directory with files underneath, use the -r
option. The following is the example command.
Command Line - INPUT
scp -i ~/.ssh/id_rsa -r sample_dir ubuntu@18.143.143.190:dir_ch7_remote
To transfer a file from the remote server to the local computer, the following five sets of information are needed for the SCP command.
For a better understanding, please go through the following practice section.
The following are the paths of the directory to be transferred and the destination directory.
Note: This practice can be done only with the local terminal.
Run the scp
command with the private key path, source directory path, and destination directory path.
cd ~
scp -i ~/.ssh/id_rsa -r ubuntu@18.143.143.190:dir_ch7_remote dir_ch7_local
sample.txt 100% 0 0.0KB/s 00:00
If you set a passphrase for the private key, you need to enter the passphrase to move forward.
Check if the directory is successfully transferred from the remote server to the local computer.
ls dir_ch7_local
You can see that the file is successfully transferred.
dir_ch7_remote sample.txt
There are two typical mistakes you might make when you run the scp
command.
1. Forgetting to add : (colon)
When you forget to add : (colon)
, the command will have a different meaning. The command will create a file copy on your local computer. For example, if you run the following command (without a colon at the end), you'll create a file named ubuntu@18.143.143.190 in the same directory.
scp -i ~/.ssh/id_rsa sample.txt ubuntu@18.143.143.190
ubuntu@18.143.143.190
2. Misusing spacing
Omitting or adding an extra space is also a common mistake. Check spaces when you encounter an error message in your command line.