Copy files to linux via ssh

How to copy files via SSH

Do you have a file on your Linux PC that needs to be transferred to your Linux server and you are not sure how to do this? This article teaches you how to copy files via SSH to your remote Linux server. It presents two methods for achieving this file transfer in a secure way. One based on the scp program and one based on the rsync program.

Background

Once you have your own Linux server up and running, you typically access it through SSH. SSH stands for Secure Socket Shell. SSH enables you to securely log in and access your Linux server over an unsecured network. Through SSH you can install, configure and update software on your Linux server, to name just a few common Linux server administration tasks.

While administering you Linux server, sooner or later you run into a situation where you have a file on your own Linux PC and you need to transfer this file to your Linux server. So you SSH-ed into your server and you are staring at your terminal screen, wondering how to go about this task. Unfortunately, you cannot directly transfer a file from your own PC to your remote Linux server through this active SSH terminal session. Luckily though, several methods exist that enable you to copy files via SSH. This article presents you with two of these methods. Namely, by using the scp and rsync programs.

System setup

A typical system setup consists of your Linux desktop PC, connected to your local network router, and a remote Linux server somewhere in the cloud. Instead of setting up a cloud server somewhere for this article (think Digital Ocean or Linode for example) , I decided on running a Linux server as a virtual machine (VM) on my laptop. Below you can find an illustration of the system setup:

My trusty Lenovo Thinkpad T450s serves as the Desktop PC. I run Debian 10 on this PC and its hostname is set to tinka . The Linux server VM also runs Debian 10 and its hostname is set to debianvm . I configured the same username on both the PC and the server. It is set to pragmalin . Refer to this article in case you would like to setup a similar Debian server as a virtual machine with VirtualBox.

Connecting to your server via SSH

While explaining the steps for copying files to the Debian server via SSH, I’ll occasionally SSH into the Debian server to verify that the files actually got transferred. Here follows a quick refresher that explains how you can log into your server via SSH.

The command from a Linux terminal on your PC to connect to your server is: ssh @ip-address or ssh @hostname . In my case the hostname of the Debian server VM is debianvm . My username on this server is set to pragmalin . This means that I can log into this server via SSH with the command:

To close the SSH connection, simply type the exit command:

SCP versus RSYNC

Before diving into the actual file copying via SSH, we should discuss the two commonly used programs for this, namely scp and rsync .

The SCP program

The scp program is a secure copy program. So basically a secure and remote version of the cp program that you locally use for copying files. Pretty much all Linux server distributions install the scp program by default, including Debian. Now, if the already installed scp program does all we need then why would we ever need another program for the same task? Read on and you’ll see that rsync does offer some benefits.

Читайте также:  Linux delete partition disk

The RSYNC program

The rsync program is labeled as a fast, versatile and remote file-copying tool. But it is not just a plain file-copying tool. The rsync program features build-in synchronization functionality. This means that it only copies a file to the remote server if it is not already present. In contrast, the scp program blatantly overwrites the file. Furthermore, rsync can compress the files during the transfer. In other words, rsync is faster and uses less network bandwidth.

By default rsync does not communicate in a secure way. Luckily an easy fix exists for this. You can force rsync to use the SSH protocol by specifying the -e «ssh» option when calling the program. Another minor disadvantage is that rsync is not installed by default on all Linux server distributions. Of course this is merely a one time inconvenience. You can simply install it with sudo apt install rsync . Just keep in mind that the rsync program needs to be installed on both sides. So both on your PC and your server.

When should you use scp and when rsync ? They both work, so it partially comes down to personal preference. Personally, I use scp for small quick file transfers as its syntax strikes me as more intuitive. For large file transfers, I opt for rsync , because it is faster and uses less network bandwidth. For example when I need to restore a complete backup to one of my servers.

WordPress archive

For file copy via SSH testing purposes, this article uses the latest WordPress archive. WordPress is a hugely popular website content management system and runs on millions of websites, including the PragmaticLinux blog. We are not actually going to install WordPress, but just use the WordPress files for file copy example purposes.

Go ahead and download the latest WordPress archive from https://wordpress.org/latest.tar.gz. On my PC the file wordpress-5.4.2.tar.gz is now present in directory /home/pragmalin/Downloads/

Copy a single file

Let’s start out with copying just a single file to the server via SSH. Open your terminal and go to the directory that holds to previously downloaded WordPress archive. Next, run either one of the following commands to copy the file to your remote server. Just replace the /home/pragmalin directory name with the name of your home directory on the server and replace the [email protected] part with your username on the server and the hostname of the server, respectively:

scp wordpress-5.4.2.tar.gz [email protected]:/home/pragmalin

rsync -e «ssh» -avz wordpress-5.4.2.tar.gz [email protected]:/home/pragmalin

If you now SSH into your server, you can verify the presence of the wordpress-5.4.2.tar.gz file in your user’s home directory. Both the scp and rsync commands have a similar structure. It is:

[COMMAND] [OPTIONAL ARGUMENTS] [SOURCE] [DESTINATION]

As you can see in this example, the scp program does not require any arguments. However, the rsync program does: -e «ssh» -avz . For detailed information on the command options, you can refer to the program’s man-page. Alternatively, you can make use of the excellent explainshell.com website. Here are the links for an explanation of the previous two commands: scp and rsync.

Permissions

Note that you can only copy files to a directory where the username you specified has write permissions. That is the reason why I specified the home directory in this example. If you need to store the file in a directory where your user does not have write permissions, then you would have to connect to the server via SSH afterwards and move the file with the help of sudo mv .

Reverse transfer direction

You can copy the files via SSH in the other direction too. So from the server to your PC. You just need to swap the [SOURCE] and [DESTINATION] in the command. For example:

scp [email protected]:/home/pragmalin/wordpress-5.4.2.tar.gz /home/pragmalin/Downloads

rsync -e «ssh» -avz [email protected]:/home/pragmalin/wordpress-5.4.2.tar.gz /home/pragmalin/Downloads

Copy all files in a directory

Another common operation is to copy all the files in a specific directory via SSH. We need a few files to try this out. Since we already downloaded the WordPress archive, we might all well extract its contents to get a bunch of files for testing purposes:

tar -xvf wordpress-5.4.2.tar.gz

For details on how to create and extract TAR GZ archives, refer to this tutorial. The newly created wordpress subdirectory now holds the archive contents. To copy all the files in this directory to your remote server, run either one of the following commands. Just replace the /home/pragmalin directory name with the name of your home directory on the server and replace the [email protected] part with your username on the server and the hostname of the server, respectively:

Читайте также:  Как через принтер перенести документ на компьютер

rsync -e «ssh» -avz —no-recursive * [email protected]:/home/pragmalin

If you now SSH into your server, you can verify the presence of the files such as index.php , wp-config-sample.php , etc. in your user’s home directory.

Copy all files in a directory recursively

In the previous section just the files in a specific directory were copied. This did not include subdirectories. If you want to copy everything, so files and subdirectories, run either one of the following commands. Just replace the /home/pragmalin directory name with the name of your home directory on the server and replace the [email protected] part with your username on the server and the hostname of the server, respectively:

rsync -e «ssh» -avz * [email protected]:/home/pragmalin

The output of the command is a bit too long for a screenshot. However the following screenshot from the directory contents listing on the server show proof that the copy operation worked. You can verify the presence of the files such as index.php and wp-config-sample.php , but also all the directories such as wp-admin , wp-contents , etc. in your user’s home directory:

Wrap up

After working through this article, you now know about two programs ( scp and rsync ) that enable you to copy files via SSH. Both commands get the job done. The syntax of the rsync command is a bit more complicated so you might prefer scp . Keep in mind though that rsync uses less network bandwidth. As a result rsync is faster especially when transferring a large amount of data.

The syntax for both commands is not hard to understand. Nevertheless, it is complex enough that you probably won’t memorize them, unless used frequently. For this reason I recommend bookmarking this article. That way you can quickly reference this information when needed.

report this ad

PragmaticLinux

Long term Linux enthusiast, open source software developer and technical writer.

Источник

4 Ways to Transfer Files Between Remote and Local Systems Over SSH

Sooner or later, you’ll find yourself in a situation where you have to upload the file to the remote server over SSH or copy a file from it.

There are various ways you can transfer files over SSH. I am going to discuss the following methods here:

  1. scp: Legacy command which is being deprecated
  2. rsync: Popular command for file synchronization
  3. sshfs: Mounting remote directory over SSH
  4. sftp clients: GUI tool for accessing file over SFTP

For a successful file transfer over SSH, you need to

  • have SSH access between the two machines
  • know the username and password on the remote machine
  • know the IP address or hostname (on the same subnet) of the remote machine

With that aside, let’s see the methods for copying files between remote systems via SSH.

Method 1: Use scp command to copy files over SSH

I have read that scp is going to be deprecated. Still, it is my favorite tool for transferring files between systems over SSH. Why? Because its syntax is similar to the cp command.

Let’s see how to use the scp command.

Copy files from the remote machine to your local machine

Here’s the scenario. You want to copy files from the remote Linux system to the currently logged in system.

Here’s a generic syntax that copies the file from the home directory of the user on the remote system to the current directory of your locally logged in system.

Do you see the similarity with the cp command? It’s almost the same except that you have to specify username and ip address with colon (:).

Now, let me show you a real-world example of this command.

In the example above, I copied the file remote.txt from the /home/abhishek/my_file directory on the remote system to the current directory of the local machine.

This should give you a hint that you should know the exact location of the file on the remote system. The tab completion does not work on remote systems.

Copy files from your local machine to the remote machine

The scenario is slightly changed here. In this one, you are sending a local file to the remote system over SSH using scp.

This is a generic syntax which will copy the filename to the home directory of username on the remote system.

Читайте также:  Куртка женская цветной принтер

In the above example, I copied local.txt file from the current directory to the home directory of the user abhishek on the remote system.

Then I logged into the remote system to show that the file has actually been copied.

You can copy directories too

Remember I told you I like scp because of its similarity with the cp command?

Like cp command, you can also use scp to copy directory over SSH. The syntax is similar to the cp command too. You just have to use the -r option.

You can do a lot more with it. Read some more examples of scp command in this tutorial:

Method 2: Use rsync to copy files and directories over SSH

Since scp is being deprecated, rsync is the next best tool for copying files between remote system over SSH. Actually, it is better than scp in many terms.

The command syntax is the same as scp. Older versions of rsync had to use rsync -e ssh but that’s not the case anymore.

Copy files from the remote machine to your local machine

Let’s say you want to copy a file from the home directory of the user on the remote system to the current directory of your locally logged in system.

Let’s take the same example you saw with scp. I am copying the file remote.txt from the /home/abhishek/my_file directory on the remote system to the current directory of the local machine.

Copy files from your local machine to the remote machine

Here is a generic syntax which will copy the file to the home directory of username on the remote system.

Time to see the real world example. I am copying local.txt file from the current directory to the home directory of the user abhishek on the remote system.

How about copying directories with rsync?

It’s the same. Just use -r option with rsync to copy entire directory over SSH between remote systems.

Take a look at this example. I copy the entire my_file directory from the remote system to the local system.

rsync is a versatile tool. It is essentially a tool for ‘recursively syncing’ the contents between two directories and quite popular for making automated backups.

Method 3: Using SSHFS to access files from remote system over SSH

There is also SSHFS (SSH Filesystem) that can be used to access remote files and directories. However, this is not very convenient just for copying files.

In this method, you mount the remote directory on your local system. Once mounted, you can copy files between the mounted directory and the local system.

You may need to install sshfs on your local system first using your distribution’s package manager.

On Debian and Ubuntu, you may use the following command:

Once you have sshfs installed on your system, you can use it to mount the remote directory. It would be better to create a dedicated directory for the mount point.

Now mount the desired directory on the remote machine in this fashion:

Once it is mounted, you can copy files into this directory or from this directory as if it is on your local machine itself.

Remember that you have mounted this file. Once your work is done, you should also unmount it:

Here’s an example where I mounted the my_file directory from the remote system to the remote_dir directory on the local system. I copied the remote.txt file to the local system and then unmounted the directory.

Method 4: Use a GUI-based SFTP client for transferring files between remote systems

As the last resort, you can use an FTP client for transferring files between remote and local systems.

FileZilla is one of the most popular cross-platform FTP client. You can easily install on your local system.

Once installed, go to File->Site Manager and add the remote system details like IP address, SSH port number, username and password.

Once you connect, you can see a split window view that shows the local filesystem on the left and the remote filesystem on the right.

To transfer the file, drag and drop files from left to right or right to left. A progress bar appears at the bottom.

Which method do you prefer?

Alright! I showed various command line and GUI methods that can be used for copying files over SSH.

Now it is up to you to decide which method to use here. Do comment your preferred method for transferring files over SSH.

Источник

Поделиться с друзьями
КомпСовет
Adblock
detector