If you own a web server and want to download a file from it, then this article may be helpful. You can see here which command to use to connect to the remote host and to get the desired file.
But before running the following commands, please ensure that you have the proper access to your web server. The SSH feature must be enabled on a remote instance. If it's not available, then you may need to contact your hosting provider and ask to enable the SSH access.
Finally, your user account should be able to read the desired file or folder. If you want to upload a file or folder to the remote server, then your account should also have the "write" access.
Table of Contents
- Using the scp command to transfer the files
- Copy file from remote server to the local machine
- Copy folder from remote server to the local machine
- Copy file from local machine to the remote server
- Copy folder from local machine to the remote server
- Additional useful scp command line options
- Copy file from a server using the SFTP command
- Copy directory from a server using the SFTP command
- Conclusion
Using the scp command to transfer the files
You can use the scp tool to easily copy files between various hosts. It's a special command that is built on top of the SSH protocol. This means that the same authentication rules apply to the SCP network connection. The file data may be transferred only if the server is properly configured and all necessary services are running.
If the remote server allows you to specify the username and password, then you can use them to connect to the host. If not, then another option is providing a valid SSH key. The following samples show how to do it correctly.
Copy file from remote server to the local machine
To copy some file from a server to a local machine, you have to run only a single command. No additional configuration is needed. Only the scp tool must be executed with the proper options.
For the simplest request, you need username, password (if applicable), hostname, path of the source file and path of the destination. Then you can combine all the parameters as it's shown below.
scp user@hostname:/remote/path/file /local/path/file
The above sample shows how to download a single file. But the scp tool can also download multiple files. To do that, you have to provide a list of source files. Also, the last parameters must be a path to the destination folder. The following sample shows how to correctly run such a command.
scp user@hostname:/remote/path/file1 user@hostname:/remote/path/file2 /local/directory/
There may be a situation when the SSH server is running on a non-standard port. Usually, web administrators apply such a configuration to make the server stronger. It may be harder for someone to get access to the server if the SSH service is enabled on some unusual port.
However, you still may be able to use the scp command in such a case. To solve that issue, you have to pass the -P flag with the port number of the running SSH service. You can get the port from your hosting provider or web administrator. See below how to correctly use that flag.
scp -P 2345 user@hostname:/remote/path/file /local/path/file
The port number may also be specified as a part of the web address. In that case, you should also explicitly specify that the scp protocol must be used in a remote connection. It can be done with the help of the "scp://" suffix. See below how to make such a connection.
scp scp://user@hostname:2345/remote/path/file /local/path/file
Please be aware that the above remote address resolves to the user's home directory. If you'd like to specify a full path of the source file, you have to add two forward slashes after the port number. Following is an example of how it has to be done.
scp scp://user@hostname:2345//root/remote/path/file /local/path/file
As we've already mentioned, you can connect to the server with the help of the SSH key file. To do that, you have to use the -i flag. The rest of the parameters remain the same. Example is shown below.
scp -i key_file.pem user@hostname:/remote/path/file /local/path/file
Copy folder from remote server to the local machine
If you have many files on a server, it may not be a convenient task to manually copy each file. Instead, you can copy the whole folder from the remote server to the local computer. But that requires specifying the -r flag. If it's set, the scp tool will recursively transfer all content of a specified folder.
See the following sample that shows how to pass that flag.
scp -r user@hostname:/remote/path/folder /local/path/folder/
Copy file from local machine to the remote server
Previously, we've described how to copy content from a remote server to a local machine. But you can also transfer content in the opposite direction.
If you'd like to send a local file to the remote destination, then you just have to swap the local path and a web address. Path to the source file must be specified as a first option. Then you have to provide the address of the destination. See the full example below.
scp /local/path/file user@hostname:/remote/path/file
It's also possible to send multiple files from the local computer to the remote server. To do that, you have to pass the list of files into the scp command. The last parameter must be a path to the remote folder. The following sample shows how to do it.
scp /local/path/file1 /local/path/file2 user@hostname:/remote/path/folder/
You may also use a key file to authenticate a user when sending a local file to the remote host. That can be done with the help of the -i flag. You have to provide a path to the private file. Example is shown below.
scp -i key_file.pem /local/path/file user@hostname:/remote/path/file
Copy folder from local machine to the remote server
To copy a folder from a local computer to the remote host, you should use the -r flag. If it's set, the scp tool will copy all nested content of a specific folder into the remote destination. Our example is given below.
scp -r /local/path/folder user@hostname:/remote/path/folder/
Additional useful scp command line options
The scp tool is a bit complex. If you are going to use it, it's recommended to get familiar with the official man pages for that command. However, just in case, we will describe here the most common options of the scp tool. This will help you to get a deeper knowledge of how this tool works.
- -3 - This option must be used if you'd like to transfer the data via the local computer. In most situations, this feature should not be enabled. It's sometimes needed when the data is transferred from one remote device to another remote device.
- -C - This flag enables data compression. It may be needed if you have a large amount of files.
- -l your_limit - This option allows you to set the maximum allowed speed for the network connection.
- -p - If this flag is set, the scp tool will keep the original timestamps and modes for each file.
- -q - This option disables all additional notifications or status messages that may appear in the Linux terminal.
- -v - This flag enables verbose mode. Linux terminal will contain additional information about the active network connection.
Copy file from a server using the SFTP command
There is another option available that allows you to copy files over the network. Linux has the SFTP command which is also using the SSH protocol.
It's very similar to the regular FTP connection. Once you are logged in, you can use the same commands, like cd, get, lcd, ln, ls and so on.
Following is the specification that shows how to initiate a connection.
sftp <user>@<hostname>
Below, you can see a small guide that shows how exactly to use this tool. There, we make a connection first. Then we try to download files with the help of the get command.
sftp user@192.168.0.1
sftp> get document.txt
If your SFTP/SSH server is running on a custom port, then you can specify it with help of the -P flag. The syntax is shown below.
sftp -P <custom_port> <user>@<hostname>
Below is an example that shows how you can open a custom connection in a private local network. You may replace the port number, username and IP address with the desired values.
sftp -P 2222 user@192.168.0.1
As we've shown it earlier, you can download the file with help of the get command.
sftp> get document.txt
Copy directory from a server using the SFTP command
The get command also allows you to copy the whole directory. For that purpose, you should use the -R flag. It will recursively download all the nested content. The syntax is given below.
sftp> get -R <directory_path>
Following is our example that shows how to copy a folder of a specific user from the remote server to the local computer.
sftp> get -R /home/user/folder
Conclusion
As you see, the SSH protocol is very popular. It's used in various Linux commands, such as SFTP or SCP. You can easily connect to the server and transfer the content in any direction.
It's worth noting that the SSH service is enabled by default in many web hosting services. However, there are some companies that disable the SSH connections because of security concerns.
If this feature is not enabled and you really need it, you may try to contact the support team. You can ask them to enable the SSH feature for your instances.
Comments