There are times when you want to transfer files between your local system and a remote server. Several protocols and methods are available that allow you to handle file transmissions in a secure manner.
The scp command in Linux is one such tool that helps a user in sharing files remotely between local and remote hosts. In this article, we will discuss the scp command in detail, along with its usage and some additional features of the command.
What Is the Scp Command
Scp, an acronym for Secure Copy, is a command-line utility in Linux-based operating systems that allows a user to copy files between remote and local hosts. Since the command transfers files over a network to some other host, SSH access is required. SSH (Secure Shell) is a protocol that allows you to handle network services securely over any network.
The scp command also supports some additional features such as specifying authentication parameters, changing the port, transferring directories, and more.
Why Scp Is Better Than Other Methods
Scp is usually preferred over other file transfer methods because, during the transfer, the connection between the two hosts is encrypted. The SSH protocol is responsible for encrypting the files, passwords, and any other sensitive details.
Other transfer methods such as Telnet or FTP do not have any encryption. Also, the user/password keypair is also saved in plain text which is not a good practice at all. A cracker can easily access your information by sniffing your network.
How to Securely Transfer Files Using Scp
Using the scp command, you can transfer files between:
- A local host and a remote host
- A remote host and a local system
- Two remote hosts
Basic Syntax
The basic syntax of the scp command is:
scp [options] [source] [destination]
Transfer From Local Host to a Remote System
If you are a server administrator, then transferring files between a local host and remote hosts might be useful to you. To upload a file named document.txt to a remote host:
scp /home/document.txt user@remote-host:/home/document.txt
Note that the source is the path of the file on your local storage. And the destination is the path of the file on the remote host. You have to specify the username and domain name of the remote server as well. In the above command, user is the username and remote-host is the domain name.
The destination path is separated from the remote host details using the colon character (:). Keep in mind that the user must exist on the remote server if you want to transfer the files successfully. Also, the user should have write access to the directory in which you want to save the file.
After issuing the above-mentioned command, the system will ask you for the remote user’s password. Type the password and press Enter.
user@remote-host's password:
If the password is valid, the file transfer will initialize. If you entered an incorrect password, an error will occur.
Before trying to copy the file using the scp command, ensure that the remote host details and the password are correct by logging in to the server using SSH.
From a Remote Host to Local Host
To copy files from a remote host to a local host, just interchange the source and destination path in the scp command.
scp user@remote-host:/home/document.txt /home/document.txt
The system will ask you for the remote user’s password once again. Enter the password to confirm the transfer process.
Between Two Remote Hosts
To copy files between two remote servers, both the source and destination paths must be directories on the remote hosts.
scp user1@remote-host1:/home/document.txt user2@remote-host2:/home/folder/document.txt
Again, a prompt will appear asking you to enter the password for each of the two users.
Scp Command-Line Options
Apart from simply transferring the files from source to destination, scp has some additional options that can be invoked using specific arguments.
Change the Port
By default, the scp command works on port 22. However, you can always overwrite the default configuration and change the port. The -P flag allows you to do the same.
To use some other port number while copying files from a local host to a remote host:
scp -P 35 /home/document.txt user@remote-host:/home/document.txt
The aforementioned command will ensure that the scp command uses port 35 for transferring files.
Preserve File Timestamps
You might know that Linux sets timestamps for each file to store the modification time, access time, and change time associated with the file. When you transfer the file to another location using scp, the timestamps of the destination file are overridden by the current time.
If for any reason you want to preserve these timestamps, use the -p flag. Notice that -P and -p flags are different from each other.
scp -p /home/document.txt user@remote-host:/home/remote/document.txt
Copy Directories
If you want to copy directories instead of files, use the -r flag to transfer directories recursively.
scp -r user@remote-host:/home/videos /home/videos
Suppressed Mode
When you enter the scp command in order to transfer files, the terminal displays the progress bar and other related information on the screen. However, you can choose not to view this information using the -q flag.
scp -q user@remote-host:/home/document.txt /home/document.txt
Use a Keypair File for Authentication
If you want to authenticate the remote host connection using a keypair file, specify the path of the file using the -i flag.
scp -i /home/keypair.pem /home/document.txt user@remote-host:/home/document.txt
Chaining Multiple Flags Together
Just like any other Linux command, you can chain multiple arguments together to make the scp command more effective.
For example, to change the port and transfer files in suppressed mode:
scp -P 34 -q user@remote-host:/home/document.txt home/document.txt
If you want to use a keypair file for authentication and need to copy directories to the destination path:
scp -i /home/secret/keypair.pem -r /home/folder user@remote-host:/home/folder
File Transfer Between Linux Systems
In the world of the internet, transferring files between systems has become an essential task. For those who are administrating Linux servers, sometimes it is important to take a backup of the server before issuing a specific command. In situations like this, the scp command comes in handy.
Similarly, the cp command helps in copying the files from one location to another in a local system. There are many basic commands that are a must if you’re just getting started with Linux.
MUO – Feed