Linux send files ssh

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.

Читайте также:  Сервер управления компьютерами linux

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.

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.

Читайте также:  Установка pip kali linux

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.

Источник

SSH / TransferFiles

Another important function of SSH is allowing secure file transfer using SCP and SFTP.

Secure Copy (scp)

Just as all modern Unix-like systems have an SSH client, they also have SCP and SFTP clients. To copy a file from your computer to another computer with ssh, go to a command-line and type:

For example, to copy your TPS Reports to Joe’s Desktop:

scp "TPS Reports.odw" joe@laptop:Desktop/

This will copy TPS Reports.odw to /home/joe/Desktop, because SCP uses your home folder as the destination unless the destination folder begins with a ‘/’.

To copy the pictures from your holiday to your website, you could do:

scp -r /media/disk/summer_pics/ mike@192.168.1.1:"/var/www/Summer 2008/"

The -r (recursive) option means to copy the whole folder and any sub-folders. You can also copy files the other way:

scp -r catbert@192.168.1.103:/home/catbert/evil_plans/ .

The ‘.’ means to copy the file to the current directory. Alternatively, you could use secret_plans instead of ‘.’, and the folder would be renamed.

Secure FTP (sftp)

Finally, if you want to look around the remote machine and copy files interactively, you can use SFTP:

This will start an SFTP session that you can use to interactively move files between computers.

SSHFS

SSHFS is a recent addition to Linux that allows you to make a remote filesystem available over SSH act as if it was inside a folder on your own system. See SSHFS for details.

GNOME

Click File -> Connect to Server. Select SSH for Service Type, write the name or IP address of the computer you’re connecting to in Server. Click Add Bookmark if you want to make the connection available later in the Places sidebar. There are options to login as a different User Name, on a different Port number, and use a different default Folder.

Files can be copied by dragging and dropping between this window and other windows.

KDE

Open Konqueror, and in the address bar type:

fish://username@server_address

Files can be copied by dragging and dropping them between this window or tab and to other windows or tabs.

Читайте также:  Ssh in linux example

Using other programs

SecPanel and PuTTY also have file transfer utilities, although they’re generally not as easy to use as the ones discussed above.

SSH/TransferFiles (последним исправлял пользователь c-71-237-198-100 2015-01-16 00:19:38)

The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details

Источник

Копирование файлов через SSH

В статье мы расскажем, как копировать файлы в Windows и Linux-системах, и покажем основные команды, с помощью которых происходит передача файлов по SSH.

Для копирования файлов по SSH в Linux-системах и Windows используют разные инструменты:

  • scp (Secure CoPy) — утилита для безопасного копирования данных между Linux-системами по протоколу SSH. Она входит в состав OpenSSH, поэтому для работы с утилитой не нужно устанавливать дополнительное ПО;
  • pscp.exe — утилита для загрузки файлов по SSH в ОС Windows. Она обладает теми же возможностями, что и scp. Утилита входит в состав программы Putty — SSH-клиента для Windows. Скачать программу можно по ссылке.

Если файл, который вы хотите скопировать, уже существует на целевом хосте, при копировании он будет перезаписан.

Копирование файлов по SSH на Linux

Для Linux копирование файлов по SSH происходит с использованием команды scp. С её помощью можно копировать файлы:

  • с локального компьютера на удалённый сервер,
  • с удалённого сервера на локальный компьютер,
  • с одного удалённого сервера на другой.
scp [опция] [источник] [получатель]

Как скопировать файл по SSH с локальной машины на удалённый сервер

Как загрузить файл на сервер по SSH? Для этого используйте команду вида:

scp [путь к файлу] [имя пользователя]@[имя сервера/ip-адрес]:[путь к файлу]
scp /home/test.txt root@123.123.123.123:/directory

Файл test.txt будет скопирован на хост 123.123.123.123 в директорию «/directory».

Как скопировать файлы с удалённого сервера на локальный компьютер

При подключённом SSH скачать файл на локальный компьютер с удалённого сервера можно с помощью команды:

scp [имя пользователя]@[имя сервера/ip-адрес]:[путь к файлу] [путь к файлу]
scp root@123.123.123.123:/home/test.txt /directory

Файл test.txt будет загружен с сервера 123.123.123.123 на локальный компьютер в папку «/directory».

Как скопировать файл по SSH с одного удалённого сервера на другой

Подключитесь по SSH к серверу, на котором расположен файл. Затем выполните команду:

scp [путь к файлу] [имя пользователя]@[имя сервера/ip-адрес]:[путь к файлу]
scp /home/test.txt root@123.123.123.123:/directory

Файл test.txt будет скопирован на хост 123.123.123.123 в директорию «/directory».

Как скачать папку со всеми файлами и подпапками

Если вы хотите скачать папку со всеми файлами и подпапками, используйте ключ -r:

scp -r [источник] [получатель]

Как подключиться к серверу по нестандартному порту

Бывает, что для подключения по SSH нужно указать нестандартный порт. Без указания порта команда подключается к серверу по стандартному 22 порту. Чтобы указать нестандартный порт, введите команду с ключом -P:

scp -P [источник] [получатель]
scp -P 12345 /home/test.txt root@123.123.123.123:/directory

Эта команда подключается по порту 12345 к серверу 123.123.123.123 и копирует на него файл «test.txt» с локального компьютера в директорию «/directory».

Как передать и скачать файлы по SSH на Windows

Скопировать файл по SSH на сервер можно командой:

pscp [путь к файлу] [имя пользователя]@[имя сервера/ip-адрес]:[путь к файлу]

Скачать файл по SSH с сервера командой:

pscp [имя пользователя]@[имя сервера/ip-адрес]:[путь к файлу] [путь к файлу]

Увидеть список папок и файлов на сервере можно через pscp.exe. Для этого введите:

pscp -ls [имя пользователя]@[имя сервера/ip-адрес]:[путь]

Если в пути или в названии файла есть пробелы, используйте кавычки:

pscp “C:\files or docs\file name” root@123.123.123.123:/home

Источник

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