- 10 SCP Commands to Transfer Files/Folders in Linux
- Basic Syntax of SCP Command
- Provide the detailed information of the SCP process using the -v parameter
- Provide modification times, access times, and modes from original files
- Make file transfer faster using -C parameter
- Change SCP Cipher to Encrypt Files
- Limiting Bandwidth Usage with SCP Command
- Specify the Specific port to use with SCP
- Copy files inside directory recursively
- Disable progress meter and warning / diagnostic message
- Copy files using SCP through Proxy
- Select different ssh_config file
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- Copying a Directory with SCP
- Downloading a Directory
- Uploading a Directory
- Conclusion
- 14 SCP Command Examples to Securely Transfer Files in Linux
- Example 1) Copy a file from local system to remote system using scp
- Example 2) Copy a file from remote System to local system using scp
- Example 3) Verbose Output while transferring files using scp (-v)
- Example 4) Transfer multiple files to remote system
- Example 5) Transfer files across two remote hosts
- Example 6) Copy files and directories recursively (-r)
- Example 7) Increase transfer speed by enabling compression (-C)
- Example 8) Limit bandwidth while copying ( -l )
- Example 9) Specify different ssh port while scp ( -P)
- Example 10) Preserves permissions, modes and access time of files while copying (-p)
- Example 11) Transferring files in quiet mode ( -q) in scp
- Example 12) Use Identify file in scp while transferring ( -i )
- Example 13) Use different ‘ssh_config’ file in scp ( -F)
- Example 14) Use Different Cipher in scp command (-c)
10 SCP Commands to Transfer Files/Folders in Linux
Linux administrators should be familiar with the CLI environment. Since GUI mode in Linux servers is not common to be installed. SSH may be the most popular protocol to enable Linux administrators to manage the servers via remote secure way. Built-in with SSH command there is SCP command. SCP is used to copy file(s) between servers in a secure way.
10 Linux SCP Commands
Basic Syntax of SCP Command
The below command will read as “copy source_file_name” into “destination_folder” at “destination_host” using “username account”.
There are many parameters in the SCP command that you can use. Here are the parameters that may use on daily basis usage.
Provide the detailed information of the SCP process using the -v parameter
The basic SCP command without parameters will copy the files in the background. Users will see nothing unless the process is done or some error appears.
You can use the “-v” parameter to print debug information into the screen. It can help you debugging connection, authentication, and configuration problems.
Sample Output
Provide modification times, access times, and modes from original files
The “-p” parameter will help you with this. An estimated time and the connection speed will appear on the screen.
Sample Output
Make file transfer faster using -C parameter
One of the parameters that can faster your file transfer is the “-C” parameter. The “-C” parameter will compress your files on the go. The unique thing is the compression-only happens in the network. When the file has arrived at the destination server, it will be returning to the original size as before the compression happen.
Take a look at these commands. It is using a single file of 93 Mb.
Sample Output
Copying files without the “-C” parameter will result in 1661.3 seconds. You may compare the result to the command below which using the “-C” parameter.
Sample Output
As you can see, when you are using compression, the transfer process is done in 162.5 seconds. It is 10 times faster than not using the “-C” parameter. If you are copying a lot of files across the network, the “-C” parameter would help you to decrease the total time you need.
The thing that we should notice is that the compression method will not work on any files. When the source file is already compressed, you will not find any improvement there. Files such as .zip, .rar, pictures, and .iso files will not be affected by the “-C” parameter.
Change SCP Cipher to Encrypt Files
By default SCP using “AES-128” to encrypt files. If you want to change to another cipher to encrypt it, you can use the “-c” parameter. Take a look at this command.
The above command tells SCP to use the 3des algorithm to encrypt the file. Please be careful that this parameter using “-c” not “-C“.
Limiting Bandwidth Usage with SCP Command
Another parameter that may be useful is the “-l” parameter. The “-l” parameter will limit the bandwidth to use. It will be useful if you do an automation script to copy a lot of files, but you don’t want the bandwidth is drained by the SCP process.
The 400 value behind the “-l” parameter is mean that we limit the bandwidth for the SCP process to only 50 KB/sec. One thing to remember is that bandwidth is specified in Kilobits/sec (kbps). It is mean that 8 bits are equal to 1 byte.
While SCP counts in Kilobyte/sec (KB/s). So if you want to limit your bandwidth for SCP maximum of only 50 KB/s, you need to set it into 50 x 8 = 400.
Specify the Specific port to use with SCP
Usually, SCP is using port 22 as a default port. But for security reasons, you may change the port into another port. For example, we are using port 2249. Then the command should be like this.
Make sure that it uses capital “P” not “p” since “p” is already used for preserved times and modes.
Copy files inside directory recursively
Sometimes we need to copy the directory and all files/directories inside it. It will be better if we can do it in 1 command. SCP supports that scenario using the “-r” parameter.
When the copy process is done, at the destination server you will found a directory named “documents” with all its files. The folder “documents” is automatically created.
Disable progress meter and warning / diagnostic message
If you choose not to see progress meter and warning / diagnostic messages from SCP, you may disable it using the “-q” parameter. Here’s the example.
As you can see, after you enter the password, there is no information about the SCP process. After the process is complete, you will see a prompt again.
Copy files using SCP through Proxy
The proxy server is usually used in the office environment. Natively, SCP is not proxy configured. When your environment using a proxy, you have to “tell” SCP to communicate with the proxy.
Here’s the scenario. The proxy address is 10.0.96.6 and the proxy port is 8080. The proxy also implemented user authentication. First, you need to create the “
/.ssh/config” file. Second, you put this command inside it.
Then you need to create file “
/.ssh/proxyauth” which contains.
After that, you can do SCP transparently as usual.
Please notice that the corkscrew is might not be installed yet on your system. On my Linux Mint, I need to install it first, using the standard Linux Mint installation procedure.
For other yum-based systems, users can install corkscrew using the following yum command.
Another thing is that since the “
/.ssh/proxyauth” file contains your “username” and “password” in clear-text format, please make sure that the file can be accessed by you only.
Select different ssh_config file
For mobile users who often switch between the company networks and public networks, it will be suffering to always change settings in SCP. It is better if we can put a different ssh_config file to match our needs.
Here’s a sample scenario
Proxy is used in the company network but not in the public network and you regularly switch networks.
By default “ssh_config” file per user will be placed in “
/.ssh/config“. Creating a specific “ssh_config” file with proxy compatibility will make it easier to switch between networks.
When you are on the company network, you can use the “-F” parameter. When you are on a public network, you can skip the “-F” parameter.
That’s all about SCP. You can see man pages of SCP for more detail. Please feel free to leave comments and suggestions.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
Copying a Directory with SCP
The Unix command scp (which stands for «secure copy protocol») is a simple tool for uploading or downloading files (or directories) to/from a remote machine. The transfer is done on top of SSH, which is how it maintains its familure options (like for specifying identities and credentials) and ensures a secure connection. It’s really helpful to be able to move around files between any machine that supports SSH.
Even if you don’t already know how to use the command, scp should be a bit more familiar to you thanks to its similarity to ssh . The biggest differences come with specifying file/directory paths. In this short article we’ll be dealing with directories specifically.
Downloading a Directory
In this use-case, we want to use scp to download a directory from a remote server to our local machine. To achieve this, we’ll use the -r flag, which tells scp to recursively copy all of the directory’s contents to our machine.
Here is an example of using scp to download a directory:
Pretty simple, right? The -r flag is the only difference between downloading a single file and downloading an entire directory. With -r specified, the directory tree is recursively traversed and each file encountered is downloaded.
One important thing to note is that scp does follow symbolic links within directories, so just be aware in case this matters for your purposes.
Uploading a Directory
The same exact concepts as downloading a directory apply here as well. You’ll probably notice that the only difference is where we specify the source directory within the actual command.
Here is an example of using scp to upload a folder:
When the source path comes first, like in the example above, it is assumed to be referring to a directory on your local machine, which is then recursively transferred to the destination machine thanks to the -r flag, as before.
Conclusion
For more information on the scp command, I’d highly encourage you to check out the docs with man scp . Not only is this the fastest way to learn about the command, but it’s a good habit to get in to for any Unix command.
Источник
14 SCP Command Examples to Securely Transfer Files in Linux
SCP (Secure Copy) is command line tool in Linux and Unix like systems which is used to transfer files and directories across the systems securely over the network. When we use scp command to copy files and directories from our local system to remote system then in the backend it makes ssh connection to remote system. In other words, we can say scp uses the same SSH security mechanism in the backend, it needs either password or keys for authentication.
In this tutorial, we will discuss scp command in linux with practical examples.
Syntax of scp command:
First syntax of scp command demonstrate how to copy files or directories from local system to target host under the specific folder.
Second syntax of scp command demonstrate how files from target host is copied into local system.
Some of the most widely used options in scp command are listed below,
- -C Enable Compression
- -i identity File or private key
- -l limit the bandwidth while copying
- -P ssh port number of target host
- -p Preserves permissions, modes and access time of files while copying
- -q Suppress warning message of SSH
- -r Copy files and directories recursively
- -v verbose output
Let’s jump into the examples now.
Example 1) Copy a file from local system to remote system using scp
Let’s assume we want to copy jdk rpm package from our local Linux system to remote system (172.20.10.8) using scp command, use the following command,
Above command will copy jdk rpm package file to remote system under /opt folder.
Example 2) Copy a file from remote System to local system using scp
Let’s suppose we want to copy a file from remote system to our local system under the /tmp folder, execute the following scp command,
Example 3) Verbose Output while transferring files using scp (-v)
In scp command, we can enable the verbose output using -v option, using verbose output we can easily find what exactly is happening in the background. This becomes very useful in debugging connection, authentication and configuration problems.
Example 4) Transfer multiple files to remote system
Multiple files can be copied / transferred to remote system using scp command in one go, in scp command specify the multiple files separated by space, example is shown below
Example 5) Transfer files across two remote hosts
Using scp command we can copy files and directories between two remote hosts, let’s suppose we have a local Linux system which can connect to two remote Linux systems, so from my local linux system I can use scp command to copy files across these two systems,
Example is shown below,
Example 6) Copy files and directories recursively (-r)
Use -r option in scp command to recursively copy the entire directory from one system to another, example is shown below,
Use below command to verify whether Download folder is copied to remote system or not,
Example 7) Increase transfer speed by enabling compression (-C)
In scp command, we can increase the transfer speed by enabling the compression using -C option, it will automatically enable compression at source and decompression at destination host.
In the above example we are transferring the Download directory with compression enabled.
Example 8) Limit bandwidth while copying ( -l )
Use ‘-l’ option in scp command to put limit on bandwidth usage while copying. Bandwidth is specified in Kbit/s, example is shown below,
Example 9) Specify different ssh port while scp ( -P)
There can be some scenario where ssh port is changed on destination host, so while using scp command we can specify the ssh port number using ‘-P’ option.
In above example, ssh port for remote host is “2022”
Example 10) Preserves permissions, modes and access time of files while copying (-p)
Use “-p” option in scp command to preserve permissions, access time and modes while copying from source to destination
Example 11) Transferring files in quiet mode ( -q) in scp
Use ‘-q’ option in scp command to suppress transfer progress, warning and diagnostic messages of ssh. Example is shown below,
Example 12) Use Identify file in scp while transferring ( -i )
In most of the Linux environments, keys-based authentication is preferred. In scp command we specify the identify file or private key file using ‘-i’ option, example is shown below,
In above example, “my_key.pem” is the identity file or private key file.
Example 13) Use different ‘ssh_config’ file in scp ( -F)
There are some scenarios where you use different networks to connect to Linux systems, may be some network is behind proxy servers, so in that case we must have different ssh_config file.
Different ssh_config file in scp command is specified via ‘-F’ option, example is shown below
Example 14) Use Different Cipher in scp command (-c)
By default, scp uses ‘AES-128’ cipher to encrypt the files. If you want to use another cipher in scp command then use ‘-c’ option followed by cipher name,
Let’s suppose we want to use ‘3des-cbc’ cipher in scp command while transferring the files, run the following scp command
Use the below command to list ssh and scp ciphers,
That’s all from this tutorial, to get more details about scp command, kindly refer its man page. Please do share your feedback and comments in comments section below.
Also Read : 11 ‘df’ Command Examples in Linux
Источник