Linux SCP Command Tutorial – Transfer Files Securely

scp or secure copy is a command-line tool used to copy files and folders securely over the network or internet. Files or folders transferred over the network are secured by encrypting them with different algorithms. Also, parties are authenticated by using passwords or certificated for security reasons.

scp Command Syntax

The scp command has the following syntax. SRC part of the command is the source part where the source system, source user, source file, and folder are specified. DST is destination user, destination system, and destination file and folder.

scp OPTION SRC_USER@SRC_SYSTEM:SRC_FILE DST_USER@DST_SYSTEM:DST_FILE
  • OPTION is used to set different features for the scp command.
  • SRC_USER is generally used to set the SRC_SYSTEM user.
  • SRC_SYSTEM is the system that provides the SRC_FILE.
  • DST_USER is the user of the DST_SYSTEM.
  • DST_SYSTEM is the system where the DST_FILE will be copied.
  • DST_FILE is the destination file that will be copied from SRC_FILE.

scp Parameters and Options

scp provides different parameters and options which can be used to preserve file attributes, compression during file transfer etc.

PARAMETERDESCRIPTION
-1Use scp protocol version 1
-2 Use scp protocol version 2
-4Use IPv4
-6Use IPv6
-BUse batch mode (no password)
-CEnable compression during transfer
-iSpecify private key for authentication
-lSet bandwidth limit
-oPass SSH option
-PSpecify port number
-pPreserve modification and access date and times
-qQuit mode
-rRecursive copy
-vVerbose mode

Copy Local File To Remote System

scp is generally used to copy a local file or source files to the remote or destination system and file. We will use the syntax defined previously. As we will copy from the current local system we will not provide the SRC_USER or SRC_SYSTEM as we have already logged in.

$ scp db.txt [email protected]:/home/ismail/mydb.txt
  • db.txt is the source file we want to copy to the remote system 192.168.10.10.
  • ismail is the user of the remote system.
  • 192.168.10.10 is the remote system IP address.
  • /home/ismail/ is the destination file path or location.
  • mydb.txt is the destination file name.

While copying from the local system to the remote system you can also use the remote system or destination system name as hostname like below. In the following example, we will use srv.linuxtect.com as the destination system name.

$ scp db.txt [email protected]:/home/ismail/mydb.txt

You can also use the -r option in order to copy local files or folders recursively. This will copy all specified path folders and sub-folders with files to the destination.

$ scp -r /home/ahmet/Downloads [email protected]:/home/ismail/income/

Copy Remote System File To Local System

scp command can be also used to copy remote files and folders to the local system.

$ scp  [email protected]:/home/ismail/mydb.txt db.txt
  • ismail is the user of the remote system which is the source system user.
  • 192.168.10.10 is the remote system IP address which is the source system.
  • /home/ismail/ is the source file path on the source system.
  • mydb.txt is the source file name that is located in the source system.
  • db.txt is the destination file name which will be copied to our local system.

While copying from the remote system to the local system you can also use the remote system or source system name as hostname like below. In the following example, we will use srv.linuxtect.com as the source system name.

$ scp [email protected]:/home/ismail/mydb.txt db.txt

You can also use the -r option in order to copy local files or folders recursively. This will copy all specified path folders and sub-folders with files to the destination.

$ scp -r [email protected]:/home/ismail/income/ /home/ahmet/Downloads

Copy From Remote System To Remote System

scp is a very flexible command which can be used to copy files and folders from the remote system to the remote system where both source and destination systems are remote.

$ scp [email protected]:/etc/hosts [email protected]:/home/ismail/myhosts
  • root is the user of the remote system which is the source system user.
  • 192.168.10.10 is the remote system IP address which is the source system.
  • /etc is the source file path on the source system.
  • hosts is the source file name that is located in the source system.
  • ismail is the destination system user name.
  • 192.168.10.20 is the remote system IP address which is the destination system.
  • /home/ismail is the destination file name which will be copied to our local system.
  • myhosts is the destination file name.

We can also specify the source or destination system name as hostname like below. linuxtect.com is the source system domain name. pythontect.com is the destination system domain name.

$ scp [email protected]:/etc/hosts [email protected]:/home/ismail/myhosts

We can also use the -r option in order to copy files and folders recursively. All given folders and their sub-folders and files will be copied from the source to the destination.

$ scp -r [email protected]:/etc/ [email protected]:/home/ismail/etc

Set scp Port Number

By default, scp command uses the default SSH port which is TCP 22. This port number is generally not specified explicitly if it is the default. But in some cases, the port number can be different than 22 and we can specify the port number with the -P option. In the following example, we will set the scp port number as 2222.

$ scp -P 2222 db.txt [email protected]:/home/ismail/mydb.txt

Enable Compression

scp is used to transfer different file types. The files can be in different sizes. The transferred files and data can be compressed in order to preserve transmission size and decrease transmission time. The -c option is used to compress files but mostly works with text and similar ASCII-type files.

$ scp -C db.txt [email protected]:/home/ismail/mydb.txt

Set Bandwidth Limit

By default, the scp command will use existing bandwidth for the transfer. If the bandwidth is limited and you shouldn’t fill all bandwidth you can set bandwidth limit with the -l option which will not exceed the given bandwidth limit. In the following example, we will set scp transfer limit as 1000 Kbps.

$ scp -l 1000 db.txt [email protected]:/home/ismail/mydb.txt

Pass SSH Option To Scp Command

The scp command is part of the SSH command or a subsystem. We can pass option for the SSH during the usage of the scp. The -o option can be used to pass the SSH option.

$ scp -o Compression db.txt [email protected]:/home/ismail/mydb.txt

Leave a Comment