Transfer files with SCP over SSH

Guide to the SCP command, which allows you to transfer files to servers using SSH. How to upload or download files from a local computer to a remote server, using a secure transfer.

SCP Command Guide

This is the table of contents of this SCP command guide:

What is the SCP command for?

When we manage servers we usually have to transfer files from our computer to the remote server and vice versa. When we start with simple hosting services we have FTP to upload files to the directory where the websites are hosted on that server, but this situation is not the usual one when you work with more complex servers such as dedicated servers or cloud servers. In fact, it is normal that this type of server does not offer an FTP service. Then what do we do?

The solution is to use the SCP command that allows us to transfer files between servers, or from the local computer to the server. It is quite easy to use and very secure since it uses encrypted connections.

Depending on your particular situation, the command to execute may be different. For the impatient, this is the SCP command we would need to upload files to the server.

scp file-to-upload.html user@www.example.com:path/destination/server/remote

In this manual you will learn how to transfer files to a server that you can only connect to by ssh, by command line, not a strange situation. You already know that SSH allows you to connect to the server and access the terminal to execute any console command, but that SSH connection does not allow you to bring any files to your computer, or to deposit files on the remote server. For this there is a different command, totally independent of ssh that does the job for you, called scp.

Note: First of all, clarify that with FTP you can transfer files with an FTP client that makes your life much easier. By command line with scp the truth is that it is much more cumbersome, but sometimes there is no other remedy. With FTP you can only download or send files in the folders enabled for it. Sometimes you want to put the files in another location, or you have to download files that are not in a path accessible by FTP. There would be no problem, since accessing by SSH you could copy the files you want to download in the path reachable by FTP. Or if what you need is to upload files, you simply upload them where FTP allows you and then move them from SSH to the folder where you really need them. This is what I usually do in my day to day, as long as FTP can be done on the server. This article offers you an alternative path for when you don’t have FTP.

See also  Markdown

How to get the scp

The scp command is available on both Linux and Mac. If you are using Windows, surely there is a way to use that command as well. I don’t know it right now, but I know that there is software (I’ve never tried it) called WinSCP that can do this task for you too, even with a graphical interface.

The command is very easy to use, we simply indicate the source file, with its path, and the destination file, with its path as well. The issue is dealing with the connection to the remote server, which makes the syntax a bit complicated.

Internally, scp connections are made through an SSH tunnel, so to reference the remote server, we will use the same connection data that we have been using to connect via SSH.

SCP Command Examples

We can see through some examples how this command works.

Note: As we have been told in the comments (thanks dolphinziyo) there is a Windows program called WinSCP, a client that uses SSH to send files as if it were by traditional FTP. It can be very good for someone who is working on this system and does not like to operate from the command line.

Upload a file from local to remote server

Suppose you have a file on your local machine and you want to transfer it to a remote server. You use the command with the following syntax.

scp file_name user@server:server_path_where_to_put_file

Remember that the connection is made by ssh, imagine that your server is called “example.com” and the user with which you connect to the machine is “root”. Imagine that the file you are trying to upload is called “file_to_upload.zip” and that you want to put it in the /var/www/example.com folder on the remote server.

scp file_to_upload.zip root@example.com:/var/www/example.com/

The server name could also be an IP address and of course the user does not have to be root.

scp other_file_to_upload.js myuser@112.223.4.215:/other/target/folder/

Upload a folder and all its content from local to remote with scp

The SCP command also supports sending an entire folder, with all its contents, recursively, to send any number of files to the server. To do this we use the “-r” option, the same as in other commands such as copy or delete.

scp -r csv-data root@2.1.3.0:/var/www/example.com/storage/

This command will copy the “csv-data” folder with all its contents to the server and place it in /var/www/example.com/storage/. That is, once the transfer is done, there will be a folder on the server in the path /var/www/example.com/storage/csv-data with all the contents of the local folder with the same name.

See also  Animated Gif search and creation services

But sometimes you don’t want to upload a folder, but all the content inside. This command allows you to upload all the files from the folder in which we are located in the terminal through SCP and place them in a specific folder on the server.

scp * user@7.4.4.1:/home/forge/tomasalvarez.com/storage/app/public

Remember that if you wanted it to be recursive, to also upload all subdirectories, you would have to use the -r flag

This command does the same thing, but using an ssh key, thanks to the -i flag (see below for other command examples with private keys).

scp -i /Users/midesweb/.ssh/socrates_cloud * forge@8.7.5.3:/home/forge/tomasalvarez.com/storage/app/public

What to do when my user does not have write permissions when using it in scp

If you are not using the root user on your server, which is very common because many servers have root disabled, remember that not all system paths are eligible to write data to them, simply because your user does not have permission to write to that folder. . Since you can’t use “sudo” directly in the scp command you can’t gain root permissions.

In these cases, the operation would be to upload the files by SCP to the personal folder, using a command like this:

scp file-upload-scp.sh root@82.223.24.123:~/

As you have seen, by placing “~/” we indicate that the upload path of the file is the personal folder. Therefore, you do not need to know the physical path of that folder.

You then move the files with the “mv” command to the location where they are needed, using “sudo” if you need root permissions to write to that folder.

Download a file from a remote server to our local computer

This second case is almost identical, only that we interchange the source and destination. The explanations given so far are basically the same.

scp user@server:server_path_where_the_file_is local_computer_path_where_to_put_the_file

With connection data similar to that of before, the command would come out like this:

scp root@example.com:/var/www/example.com/file_to_download.html file_to_download_name_in_local.html

Using SCP to fetch an entire folder from the remote server

There are some useful options for working with the scp command, you can access help with the “man scp” command. I am sharing with you an option that I use a lot to copy all the files in a folder, recursively, also copying the content of the subfolders. The option is -r.

scp -r root@161.0.0.1:/var/www//folder/ destination_path_in_local/

This will copy all the files that you find in the path “/var/www//carta/” on the remote server (even with the folders that you can find in that path). It will download them and they will be placed on your local computer in the “ruta_destino_en_local” folder.

Using the SCP command against a server with a private key

Many servers require a private key to log in via SSH. If this is the case in your case, you will need the same private key for the SCP command.

The parameter to indicate the private key is “-i”, . Follow with a space and the path where you have the key on your local computer. We show you with an example command.

scp -i /Users/midesweb/.ssh/key_file local_file.txt user@0.0.0.3:/var/www/example.com/remote_file.xml

This command would allow an upload by SCP, of a file called local_archive.txt, to a server with ip 0.0.0.3, accessing with the user “user” and the private key that we have in the document with full path /Users/midesweb/ .ssh/key_file. As you can see, everything you’ve learned about SCP in this article applies exactly the same. You simply have to use the -i option to indicate the path where you have the private ssh access key that you intend to use for the SCP transfer.

scp -i /Users/midesweb/.ssh/ssh_key user@1.2.3.4:/var/www/example.com/public/download.compressed.tgz .

This second SCP command that makes use of an ssh key, would allow the transfer of the file from remote to local, that is, the download of a file. The file is called download.compressed.tgz and would be saved on the local computer with the same name.

If you needed to get all the contents of a folder recursively, with all its subdirectories, accessing the server with the ssh key would launch the following command:

scp -i /Users/my_user/.ssh/my_key -r user@1.6.2.21:folder/other_folder_to_download .

This would get you everything in the server folder that is inside the user user and folder/other_folder_to_download and put it in the directory where you were positioned in the terminal.

To do the transfer in the opposite direction of the entire folder you have locally and send it to the server, you would run this command.

scp -i /Users/my_user/.ssh/my_key -r . server_user@1.2.2.1:folder/destination

scp command conclusion

I hope that with these notes you can easily use your command line to transfer files to the server securely, via scp, using an SSH tunnel, something that you will surely have to incorporate into your web development workflows sooner or later. Remember that you have many other articles dedicated to working with servers in the .

Loading Facebook Comments ...
Loading Disqus Comments ...