- Копирование файлов scp
- Что такое SCP?
- Копирование файлов scp
- Выводы
- 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:
- How to use SCP and SSH Linux Commands: Tips and Tricks
- Accelerate your career
- SCP command
- How to use SCP
- Specifying a port with scp
- SSH command
- The most simple case
- Specifying a username
- Move the ssh service to an other port
- Running a command on the remote server
- Differences between SCP and SSH
- Tips & tricks with SCP and SSH
- Level up your cloud career
- Final Words
Копирование файлов scp
Если у вас есть домашний сервер или сервер в интернете, то вам понадобится способ для безопасного перемещения файлов между машинами. Между локальными машинами мы могли бы просто переносить данные с помощью флешки, но на удаленный сервер нужно передавать данные только через сеть. Есть много способов реализации этой задачи.
В этой статье мы рассмотрим как выполняется копирование файлов с помощью утилиты scp (Secure Copy Command), а также что из себя представляет команда Linux scp. При передаче файлы шифруются, так что больше никто не сможет получить к ним доступ и самое интересное, что вам не нужно настраивать FTP или другое дополнительное программное обеспечение. Будет достаточно сервера SSH.
Что такое SCP?
Команда scp — это утилита, которая работает по протоколу SSH, а значит, все что вам нужно для передачи файла на компьютер, это чтобы на нем был запущен SSH сервер, а также вы должны знать логин и пароль для подключения к нему. С помощью команды scp вы можете не только перемещать файлы между локальной и удаленной системой, но и между двумя удаленными системами. Для этого тоже будет достаточно знать пароли от них. И в отличие от Rsync вам не нужно авторизоваться на одном из серверов.
Копирование файлов scp
Эта статья рассчитана в первую очередь на начинающих, поэтому я попытаюсь объяснять все как можно подробнее. Перед тем, как мы перейдем к практике, нужно рассмотреть общий синтаксис команды:
$ scp опции пользователь1@хост1 : файл пользователь2@хост2: файл
Опции утилиты больше касаются протокола SSH и настраивают общее ее поведение. Дальше следует адрес первого и второго файла. Каждый из них может быть расположен как на локальной, так и на удаленной машине. А теперь рассмотрим основные опции, которые могут нам понадобиться:
- -1 — использовать протокол SSH1;
- -2 — использовать протокол SSH2;
- -B — пакетный режим для передачи нескольких файлов;
- -C — включить сжатие;
- — l — установить ограничение скорости в кбит/сек;
- -o — задать нужную опцию SSH;
- -p — сохранять время модификации;
- -r — рекурсивное копирование директорий;
- -v — более подробный режим.
scp /home/sergiy/file root@losst.pro:/root/
В качестве пользователя нужно использовать любого пользователя, который есть на сервере. Вместо IP вы можете также домен сервера. Только необходимо, чтобы у выбранного пользователя были права записи в указанную папку.
С помощью опции -r вы можете скопировать на удаленную машину целый каталог. Команда будет выглядеть вот так:
scp -r /home/sergiy/photos root@losst.pro:/root/
Убедитесь, что у исходного каталога нет косой черты, а у каталога назначения она должна обязательно быть. Немного изменив синтаксис с косой чертой можно передать все файлы из каталога на удаленный сервер:
scp -r /home/sergiy/photos/* root@losst.pro:/root/
Если поменять местами локальный путь и сервер, то вы сможете копировать файлы scp c удаленного сервера на локальный компьютер:
scp root@losst.pro:/root/file /home/sergiy/
Таким же самым образом вы можете копирование файлов scp или папок с сервера:
scp -r root@losst.pro:/root/photos /home/sergiy/
Обратите внимание, что папка, в которую вы собираетесь копировать должна завершаться косой чертой, иначе папка, которую вы копируете запишется вместо той, в которую нужно скопировать.
Чтобы скачать файл scp из одного сервера на другой, просто необходимо указать данные аутентификации на каждом из них. Например, скопируем один файл:
scp root@losst.pro:/home/root/index.html root@losst.pro:/home/root/www/
Скопируем папку с того же удаленного сервера:
scp root@losst.pro:/root/photos root@losst.pro:/home/root/www/
Выводы
В этой статье мы рассмотрели как выполняется передача файлов scp, как это работает и что вы можете сделать с помощью этой утилиты. Как вы видите, перемещать файлы с одного сервера на другой очень просто. В Linux достаточно понять основы чтобы начать работать эффективнее! А команда scp один из таких инструментов.
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.
How to use SCP and SSH Linux Commands: Tips and Tricks
This tutorial also documents a few important differences between the Linux commands.
The Linux job market continues to grow and expand, and our LFCS course will help prepare you for one of the standard industry Linux administration certifications.
Accelerate your career
Get started with ACG and transform your career with courses and real hands-on labs in AWS, Microsoft Azure, Google Cloud, and beyond.
Difficulty: Basic
Before we start: in this tutorial, you will come across both SSH and ssh. The difference is this: SSH is the general protocol, and ssh is the linux SSH client command.
SCP command
The scp command allows you to copy files over ssh connections. This is pretty useful if you want to transport files between computers, for example to backup something. The scp command uses the ssh command and they are very much alike. However, there are some important differences.
How to use SCP
- To copy from a (remote) server to your computer
- To copy from your computer to a (remote) server
- To copy from a (remote) server to another (remote) server
In the third case, the data is transferred directly between the servers; your own computer will only tell the servers what to do.
These options are very useful for a lot of things that require files to be transferred, so let’s have a look at the syntax of this command:
The scp command above will transfer the file “examplefile” to the directory “/home/yourusername/” at the server “yourserver”, trying to get ssh acces with the username “yourusername”. That’s quite a lot information, but scp really needs it all. Well, almost all of it. You could leave out the “yourusername@” in front of “yourserver”, but only if you want to login on the server with your current username on your own computer.
Let’s have a closer look at the end of the command. There’s a colon over there, with a directory after it. Just like Linux’s normal cp command, scp will need to know both the source file(s) and the target directory (or file). For remote hosts, the file(s)/directory are given to the scp command is this way. You can also copy a file (or multiple files) from the (remote) server to your own local computer. Let’s have a look at an example of that:
Note: The dot at the end means the current local directory. This is a handy trick that can be used about everywhere in Linux. Besides a single dot, you can also type a double dot ( .. ), which is the parent directory of the current directory.
This will copy the file “/home/yourusername/examplefile” to the current directory on your own computer, provided that the username and password are correct and that the file actually exists.You probably already guessed that the following command copies a file from a (remote) server to another (remote) server:
Note: To make the above command work, the servers must be able to reach each other, as the data will be transferred directly between them.
If the servers somehow can’t reach each other (for example, if port 22 is not open on one of the sides) you won’t be able to copy anything. In that case, copy the files to your own computer first, then to the other host. Or make the servers able to reach each other (for example by opening the port). Well, those are the main uses of scp.
We’ll go a bit more in-depth about the differences between ssh and scp. Actually you can also use it just like the normal cp command, without any ssh connections in it, but that’s quite useless. It requires you to type an extra ‘s’.
Specifying a port with scp
The scp command acts a little different when it comes to ports. You’d expect that specifying a port should be done this way:
However, that will not work. You will get an error message like this one:
This is caused by the different architecture of scp. It aims to resemble cp, and cp also features the -p option. However, in cp terms it means ‘preserve’, and it causes the cp command to preserve things like ownership, permissions and creation dates.
The scp command can also preserve things like that, and the -p option enables this feature. The port specification should be done with the -P option. Therefore, the following command will work:
Also note that the -P option must be in front of the (remote) server.
The ssh command will still work if you put -p your port behind the host syntax, but scp won’t. Why? Because scp also supports copying between two servers and therefore needs to know which server the -P option applies to.
SSH command
SSH is some kind of an abbreviation of Secure SHell. It is a protocol that allows secure connections between computers.
In this tutorial, we’ll be dealing with the ssh command on Linux, the OpenSSH version. Most Linux distributions feature the OpenSSH client today, but if you want to be sure, have a look at the SSH manpage on your system. You can do this by typing:
Note: this should be done in a terminal.
If it displays something like this:
then you can be quite sure you’re running the OpenSSH version. For more background information about SSH, see https://en.wikipedia.org/wiki/SSH.
The most simple case
In the most simple case, you can connect to a server that supports ssh with a syntax as short as this:
Note: If you do not have any ssh server nearby that you can access, you can also try this command with your own computer as a server. To do this, replace “yourserver” with “localhost”. Of course, yourserver should be replaced by a hostname or an ip address of the server you want to connect to.
As you can see in the terminal snippet, I am logged in as pineehad. If you do not specify a username (I’ll explain how to do that later in this tutorial), SSH will assume that you want to login with the username you’re currently logged in with. So, in this case, SSH will try the username pineehad. Of course, you need to be sure that the server supports ssh connections.
The ssh client tries to connect to port 22 default. This means that, if you want to connect to a remote host with the default settings, you should make sure that, if applicable, port 22 is forwarded to the server you’re trying to connect to. You will find more regarding the SSH port further in this tutorial.
Now, back to the command we ran. If the server supports SSH connections and you can reach it by port 22, you should be prompted for a password (if this is the first time you try to connect to the server, ssh will first ask the question if you want to continue connecting, which can generally just be answered with a ‘yes’). If you type a password here, you won’t see asterisks appearing. Don’t panic, this is ssh’s normal behaviour. It makes connecting using ssh even more safe, because any accidental spectators won’t be able to see the length of the password.
After entering the password, if the username and the password were correct, you should be running a shell on the server. If not, make sure you are connecting to a server of which you know that you should be able to login with your username and the specified password. You could try connecting to your own computer (see the note beneath the terminal quote) or read on to learn how to specify an other username. Once you’re done trying the ssh shell, you can exit it by pressing Ctrl + D.
Specifying a username
It’s actually quite simple to specify a different username. You might even already be familiar with it. See the following example:
The above will make ssh try to connect with the username “yourusername” instead of (in my case) pineehad. This syntax is also used by a lot of other protocols, so it’ll always come in handy to know it.
By the way, you will still be asked for a password. For security reasons, it is not even possible to directly specify the password in the syntax. You will always be asked interactively, unless you start configuring the server in an advanced way (which is exactly why that topic is out of this tutorials scope: this tutorial documents how to use the clients, not how to configure the server).
Move the ssh service to an other port
There are many reasons to move the ssh service to an other port. One of them is avoiding brute-force login attempts. Certain hackers try to get access to ssh servers by trying a lot of common usernames with common passwords (think of a user “john” with password “doe”).
Although it is very unlikely that these hackers will ever get access to the system, there is an other aspect of the brute-force attacks that you’ll generally want to avoid: the system and connection load. The brute-force attacks usually are done with dozens or even thousands of tries a second, and this unnecessarily slows down the server and takes some bandwidth which could’ve been used a lot better.
By changing the port to a non-default one, the scripts of the hackers will just be refused and most of the bandwidth will be saved.As the ssh command can’t just guess the port, we will have to specify it if it’s not the default 22 one. You can do that this way:
Of course, you will have to replace “yourport” with the port number. These is an important difference between ssh and scp on this point. I’ll explain it further on.
Running a command on the remote server
Sometimes, especially in scripts, you’ll want to connect to the remote server, run a single command and then exit again. The ssh command has a nice feature for this. You can just specify the command after the options, username and hostname. Have a look at this:
This will make the server update its searching database. Of course, this is a very simple command without arguments. What if you’d want to tell someone about the latest news you read on the web? You might think that the following will give him/her that message:
However, bash will give an error if you run this command:
What happened? Bash (the program behind your shell) tried to interpret the command you wanted to give ssh. This fails because there are exclamation marks in the command, which bash will interpret as special characters that should initiate a bash function.
But we don’t want this, we just want bash to give the command to ssh! Well, there’s a very simple way to tell bash not to worry about the contents of the command but just pass it on to ssh already: wrapping it in single quotes. Have a look at this:
The single quotes prevent bash from trying to interpret the command, so ssh receives it unmodified and can send it to the server as it should. Don’t forget that the single quotes should be around the whole command, not anywhere else.
Differences between SCP and SSH
Unlike ssh, scp cannot be used to run a command on a (remote) server, as it already uses that feature of ssh to start the scp server on the host. The scp command does have an option that accepts a program (the -S option), but this program will then be used instead of ssh to establish the encrypted connection, and it will not be executed on the remote host.
Tips & tricks with SCP and SSH
Quite a handy thing about scp is that it supports asterisks. You can copy all files in a remote directory in a way like this:
And you can also just copy a whole directory by specifying the -r (recursive) option:
Both of these also work when copying to a (remote) server or copying between a (remote) server and another (remote) server.The ssh command can come in handy if you don’t know the exact location of the file you want to copy with scp. First, ssh to the (remote) server:
Then browse to the right directory with cd. This is essential Linux terminal knowledge, so I won’t explain it here. When you’re in the right directory, you can get the full path with this command:
Note: pwd is an abbreviation of Print Working Directory, which is a useful way to remember the command.
You can then copy this output, leave the ssh shell by pressing Ctrl + D, and then paste the full directory path in your scp command. This saves a lot of remembering and typing!
You can also limit the bandwidth scp may use when copying. This is very useful if you’re wanting to copy a huge amount of data without suffering from slow internet for a long time. Limiting bandwidth is done this way:
The bandwidth is specified in Kbit/sec. What does this mean? Eight bits is one byte. If you want to copy no faster than 10 Kbyte/sec, set the limit to 80. If you want to copy no faster than 80 Kbyte/sec, set the limit to 640. Get it? You should set the limit to eight times the maximum Kbyte/sec you want it to be.
I’d recommend to set the -l option with all scp’ing you do on a connection that other people need to use, too. A big amount of copying can virtually block a whole 10 Mbit network if you’re using hubs.
Level up your cloud career
A Cloud Guru makes it easy (and awesome) to get certified and master modern tech skills — whether you’re new to cloud or a pro. Check out our current free cloud courses or level up your cloud and it career path with a free trial.
Final Words
Well, that was it! I hope you learned a lot. Of course, you can always have a quick look at this tutorial again if you forgot something. Please tell other people who might be interested about this tutorial, you’ll help this blog to grow if you do =). Thank you for reading and have a lot of fun with your new knowledge!