As a tool, SSH requires you to provide a password for the SSH user or an SSH key for the target host. In this tutorial, we will explore how to copy files and directories using SCP command.
SCP Command
The SCP command is available in any system that has the ssh-client and server packages installed. You can then invoke the SCP command with the following syntax:
The previous command takes few parameters such as:
- user@source_host – refers to the username and the address of the source machine. You also need to specify the source filename after the colon as shown in the previous syntax.
- user@destination_host – specifies the username and address of the target host to which you wish to transfer the files.
Ensure that the specified user has the read and write permissions on the target file or directory of the remote host.
You can also specify other options in the SCP command to alter its default behavior. Accepted options include:
- -P – sets the port for the remote host
- -c – allows SCP to compress the data during transfer
- -r – allows SCP to copy files and directories recursively
- -q – quiet mode, hides the progress meter and non-error messages
- -p – allows SCP to preserve files and directory attributes
- -v – Enables verbose mode. Show all descriptive messages and progress state
- -l – Limits the bandwidth used by the SCP utility
You can check the other options by running the following command:
Note: Although SCP shows the descriptive messages of the files and directories you are copying, it will not prompt you when overwriting the files. Hence, ensure to provide unique names for the files you copy to avoid data loss.
Copy File from Local to Remote Host
You can copy a file from your local machine to a remote host by running the following command:
For example, to copy the file hello.txt to /home/debian/files directory, we can run the following command:
The command will prompt you for the password of the specified remote user. If you have added your SSH key to the remote host, the command will automatically login and transfer the target files.
Keep in mind that the SCP command will preserve the original filename unless specified. For example, to save the file under a different directory, you can run this following command:
This will change the filename from hello.txt to hello_new.txt on the remote machine.
Copy Directory from Local to Remote Host
If you wish to copy a directory from your local machine to a remote host, use the recursive option as shown in the following syntax:
For example, to copy all the files in the /var/logs directory of the local machine, you can run the following command:
The -r flags tells the SCP command to copy the directory recursively. If the target directory does not exist on the remote host, the SCP will create it automatically.
Copy Files from Host to Remote Host
In some cases, you may need to copy files from a remote host to another remote host. You can accomplish that by running the following command:
The command will copy the files specified in the src_file parameter to the path specified in the target_path parameter.
Ensure that both machines are online and that they can communicate via SSH.
Conclusion
This tutorial walks you through using the SCP command to copy the files and directories via SSH.