Copy file to remote linux server

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

From legacy scp to modern rsync. From the unconventional sshfs to the GUI convenience of FileZilla. Learn different ways for copying files 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

Copying files from the remote Linux system over SSH

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.

scp [email protected]_address:/home/username/filename .

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.

Copy files from the remote Linux system over SSH using scp

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.

Читайте также:  Arch linux installation wifi

Copy files from your local machine to the remote machine

Copying files to the remote Linux system over SSH

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.

Copying files to remote Linux system over SSH using scp command

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.

scp -r source_dir [email protected]_address:/home/username/target_dir

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.

rsync [email protected]_address:/home/username/filename .

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 the remote Linux system over SSH using rsync

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.

Copying files to remote Linux system over SSH using rsync command

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.

rsync -r source_dir [email protected]_address:/home/username/target_dir

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

Copy directory from the remote Linux system over SSH using rsync

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.

Читайте также:  System map linux boot

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

sshfs [email protected]_address:path_to_dir mount_dir

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.

Using sshfs to transfer files over ssh

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.

Connecting to remote server using SFTP in Filezilla

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.

Using Filezilla to transfer files between remote and local Linux systems over sftp

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.

Источник

Classic SysAdmin: How to Securely Transfer Files Between Servers with scp

This is a classic article written by Swapnil Bhartiya from the Linux.com archives. For more great SysAdmin tips and techniques check out our free intro to Linux course.

If you run a live or home server, moving files between local machines or two remote machines is a basic requirement. There are many ways to achieve that. In this article, we talk about scp (secure copy command) that encrypts the transferred file and password so no one can snoop. With scp you don’t have to start an FTP session or log into the system.

The scp tool relies on SSH (Secure Shell) to transfer files, so all you need is the username and password for the source and target systems. Another advantage is that with SCP you can move files between two remote servers, from your local machine in addition to transferring data between local and remote machines. In that case you need usernames and passwords for both servers. Unlike Rsync, you don’t have to log into any of the servers to transfer data from one machine to another.

This tutorial is aimed at new Linux users, so I will keep things as simple as possible. Let’s get started.

Copy a single file from the local machine to a remote machine:

The scp command needs a source and destination to copy files from one location to another location. This is the pattern that we use:

scp localmachine/path_to_the_file username@server_ip:/path_to_remote_directory

In the following example I am copying a local file from my macOS system to my Linux server (Mac OS, being a UNIX operating system has native support for all UNIX/Linux tools).

scp /Volumes/MacDrive/Distros/fedora.iso swapnil@10.0.0.75:/media/prim_5/media_server/

Here, ‘swapnil’ is the user on the server and 10.0.0.75 is the server IP. It will ask you to provide the password for that user, and then copy the file securely.

Читайте также:  User activity logs in linux

I can do the same from my local Linux machine:

scp /home/swapnil/Downloads/fedora.iso swapnil@10.0.0.75:/media/prim_5/media_server/

If you are running Windows 10, then you can use Ubuntu bash on Windows to copy files from the Windows system to Linux server:

scp /mnt/c/Users/swapnil/Downloads/fedora.iso swapnil@10.0.0.75:/media/prim_5/ media_server/

Copy a local directory to a remote server:

If you want to copy the entire local directory to the server, then you can add the -r flag to the command:

scp -r localmachine/path_to_the_directory username@server_ip:/path_to_remote_directory/

Make sure that the source directory doesn’t have a forward slash at the end of the path, at the same time the destination path *must* have a forward slash.

Copy all files in a local directory to a remote directory

What if you only want to copy all the files inside a local directory to a remote directory? It’s simply, just add a forward slash and * at the end of source directory and give the path of destination directory. Don’t forget to add the -r flag to the command:

scp -r localmachine/path_to_the_directory/* username@server_ip:/path_to_remote_directory/

Copying files from remote server to local machine

If you want to make a copy of a single file, a directory or all files on the server to the local machine, just follow the same example above, just exchange the place of source and destination.

Copy a single file:

scp username@server_ip:/path_to_remote_directory local_machine/path_to_the_file

Copy a remote directory to a local machine:

scp -r username@server_ip:/path_to_remote_directory local-machine/path_to_the_directory/

Make sure that the source directory doesn’t have a forward slash at the end of the path, at the same time the destination path *must* have a forward slash.

Copy all files in a remote directory to a local directory:

scp -r username@server_ip:/path_to_remote_directory/* local-machine/path_to_the_directory/

Copy files from one directory of the same server to another directory securely from local machine

Usually I ssh into that machine and then use rsync command to perform the job, but with SCP, I can do it easily without having to log into the remote server.

Copy a single file:

scp username@server_ip:/path_to_the_remote_file username@server_ip:/ path_to_destination_directory/

Copy a directory from one location on remote server to different location on the same server:

scp username@server_ip:/path_to_the_remote_file username@server_ip:/ path_to_destination_directory/

Copy all files in a remote directory to a local directory

scp -r username@server_ip:/path_to_source_directory/* usdername@server_ip:/ path_to_the_destination_directory/

Copy files from one remote server to another remote server from a local machine

Currently I have to ssh into one server in order to use rsync command to copy files to another server. I can use SCP command to move files between two remote servers:

Usually I ssh into that machine and then use rsync command to perform the job, but with SCP, I can do it easily without having to log into the remote server.

Copy a single file:

scp username@server1_ip:/path_to_the_remote_file username@server2_ip:/ path_to_destination_directory/

Copy a directory from one location on a remote server to different location on the same server:

scp username@server1_ip:/path_to_the_remote_file username@server2_ip:/ path_to_destination_directory/

Copy all files in a remote directory to a local directory

scp -r username@server1_ip:/path_to_source_directory/* username@server2_ip:/ path_to_the_destination_directory/

Conclusion

As you can see, once you understand how things work, it will be quite easy to move your files around. That’s what Linux is all about, just invest your time in understanding some basics, then it’s a breeze!

Ready to continue your Linux journey? Check out our free intro to Linux course!

Источник

Оцените статью
Adblock
detector