Copy file from local to server ssh

SSH or Secure Shell is a protocol that allows secure access to remote computers. SSH also comes with scp utility for transferring file between remote computers. It uses the SSH protocol and has the syntax almost similar to the cp command.

Other file transfer applications such as sftp and rsync can also utilize SSH to secure their file transfers. These applications allow us to copy our files from local to remote servers and to copy files from remote servers to our local machine.

Examples for the following file transfer methods are based on the above setup.

Make sure you have SSH access to the remote server with adequate permission to the remote files and folders.

Methods for remote file transfer using SSH:

Transfer file using scp

The easiest of these is scp or secure copy. While cp is for copying local files, scp is for remote file transfer. The main difference between cp and scp is that, you'll have to specify the remote host's DNS name or IP address and provide a login credential for the command to work when using scp. You can scp files from local to remote and from remote to local.

  1. Copy single file from local to remote using scp.

    $ scp myfile.txt remoteuser@remoteserver:/remote/folder/

    If the target folder (/remote/folder/) is not specified, it will copy the file to the remote user's home directory.

  2. scp from remote to local using a single file .

    $ scp remoteuser@remoteserver:/remote/folder/remotefile.txt localfile.txt

    Using . as the copy target (replacing localfile.txt will copy the remote file to the current working directory using the same filename (remotefile.txt)

  3. Copy multiple files from local to remote using scp.

    $ scp myfile.txt myfile2.txt remoteuser@remoteserver:/remote/folder/

  4. Copy all files from local to remote using scp.

    $ scp * remoteuser@remoteserver:/remote/folder/

  5. Copy all files and folders recursively from local to remote using scp.

    $ scp -r * remoteuser@remoteserver:/remote/folder/

    remoteuser need to exist and have write permission to /remote/folder/ in the remote system.

    GUI programs such WinSCP can also be used to transfer files between local and remote host using scp methods.

Transfer file using sftp

sftp or Secure FTP, on the other hand works almost exactly like ftp but with a secure connection. Most of the commands are similar and can be used interchangeably. The following sftp example will work exactly as ftp would.

$ sftp Connected to 192.168.1.10. sftp> dir file1 file2 file3 sftp> pwd Remote working directory: /home/user sftp> get file2 Fetching /home/user/file2 to file2 /home/user/file2 100% 3740KB 747.9KB/s 00:05 sftp> bye $

WinSCP can also be used for file transfer using SFTP protocol, but with graphical interface. The other popular tool is FileZilla.

Transfer file using rsync

You can also use ssh to secure your rsync session. To do this, use --rsh=ssh or -e “ssh” with your normal rsync commands. The following 2 commands will work exactly the same;

$ rsync -av --delete --rsh=ssh /path/to/source remote:/remote/folder/ $ rsync -av --delete -e "ssh" /path/to/source remote:/remote/folder/

If these options are not specified, rsync will first try to connect to rsyncd but will automatically fall back to SSH if rsyncd is not running in the remote system.

Mount remote filesystem locally

Remote filesystems could be mounted to the local host and accessed as a local filesystem. Mounting remote filesystem requires SSH access to the remote host and using sshfs.

Discuss the article:

Comment anonymously. Login not required.

I'm using Linux (centos) machine, I already connected to the other system using ssh. Now my question is how can I copy files from one system to another system?

Suppose, in my environment, I have two system like System A and System B. I'm using System A machine and some other using System B machine.

How can I copy a file from System B to System A? And, copy a file from System A to System B?

asked Dec 24, 2013 at 9:43

6

Syntax:

scp <source> <destination>

To copy a file from B to A while logged into B:

scp /path/to/file username@a:/path/to/destination

To copy a file from B to A while logged into A:

scp username@b:/path/to/file /path/to/destination

HalosGhost

4,62410 gold badges31 silver badges40 bronze badges

answered Dec 24, 2013 at 9:48

DopeGhotiDopeGhoti

69.5k8 gold badges93 silver badges130 bronze badges

22

In case if you need an alternate approach.

Install sshfs. if you use ubuntu/debian:

sudo apt-get install sshfs

or, if you use centos/rhel:

sudo yum install fuse-sshfs

or, in macOS

brew install sshfs

Create an empty dir

mkdir /home/user/testdir

"link" or "mount" the two directories

sshfs :/remote/dir /home/user/testdir

"unlink" the dirs

fusermount -u /home/user/testdir

On BSD and macOS, to unmount the filesystem:

umount mountpoint

or

diskutil unmount mountpoint

For more see here, linuxjournal.com libfuse/sshfs

Rakib Fiha

5623 gold badges9 silver badges23 bronze badges

answered Dec 24, 2013 at 9:58

Ruban SavvyRuban Savvy

8,0817 gold badges28 silver badges43 bronze badges

8

Sometimes you need to get fancy with tar:

tar -C / -cf - \ opt/widget etc/widget etc/cron.d/widget etc/init.d/widget \ --exclude=opt/widget/local.conf | ssh otherhost tar -C / -xvf -

answered Dec 24, 2013 at 15:17

Dan GarthwaiteDan Garthwaite

7,3061 gold badge14 silver badges9 bronze badges

7

If you want to keep the files on both systems in sync then have a look at the rsync program:

(see tutorial here)

answered Dec 24, 2013 at 21:30

KiffinKiffin

4683 silver badges5 bronze badges

1

If they are running SCP / SSH on a different port, make sure you specify the uppercase -P port option.

Example:

scp -P 7121 /users/admin/downloads/* :/home/

answered Feb 6, 2021 at 20:13

CarterCarter

3012 silver badges3 bronze badges

2

A simpler method that works with via SSH controlled NVIDIA Jetson computers is to connect with SFTP (SSH File Transfer Protocol).

Suppose I wish to move the document UFO_blueprint.odt from NASA's remote servers and move it into my Documents.

  1. cd to where you want the file saved

    $ cd Documents
  2. Connect

    $ sftp sammy@your_server_ip_or_remote_hostname
  3. Go the directory that contains the file to be transferred.

    $ cd NASA/secret_files/
  4. Transfer

    $ get UFO_blueprint.odt

To get the complete directory, instead use

$ get -r secret_files/

AdminBee

19.4k16 gold badges43 silver badges67 bronze badges

answered Oct 1, 2020 at 3:53

Pe DroPe Dro

1,1501 gold badge6 silver badges13 bronze badges

2

Unix/Linux recursive copy of files and folders over a secure shell using the code following:

scp -r <host@machine_name:from_remote_directory_path> <to_local_directory_path>

A full example might look like:

scp -r sizwe@hprobook:./home/sizwe/PycharmProjects ./home/immaculate/OtherPycharmProjects

Note if you do not have a DNS server to resolve hostnames, use your IP address instead. The example above might look like this

scp -r 192.168.43.167:./home/sizwe/PycharmProjects ./home/immaculate/OtherPycharmProjects

AdminBee

19.4k16 gold badges43 silver badges67 bronze badges

answered Nov 9, 2020 at 11:32

SizweSizwe

711 silver badge1 bronze badge

If speed (and not security) is your priority, then check out netcat. Before I start, let me warn you that this should be used only in a trusted network because netcat transfers are not encrypted.

First, on the receiving side, run this:

nc -lp 22222 >FileName

Then on the sending side:

nc -w3 <receiver IP or hostname> 22222 <FileName

Notes:

  • -l: Listen mode
  • -p: Port number to listen on (I picked an arbitrary port)
  • -w: Connect timeout

answered Apr 28, 2021 at 7:14

If you have one of the common OSes, you can install pigz that is the same as gzip except that it will use multiple cores and be quicker over a fast network. My command is a bit different.

tar cf - -C /opt -S <dir> | pigz | ssh <target> "pigz -d | tar xf - -C /opt -S"

or the other way to get files

ssh <target> "tar cf - -C /opt -S <dir> | pigz" | pigz -d | tar xf - -C /opt -S

or backup the root fs with

ssh <target> "tar cf - -C / --one-file-system -S --numeric-owner . | pigz" > root.tgz

answered Aug 4, 2020 at 19:09

1

How do I transfer files from local to SSH?

Transferring Files Via SSH Across Remote Servers.
Use your remote servers' web console to log in. Access their control panels and check that SSH is enabled. ... .
Start Putty and set up your SSH remote server connection. ... .
Find the destination folder you're looking for on the remote server (through Putty). ... .
4 Comments..

How do I copy a local file to a remote server?

How to Copy a File from/to a Remote Server.
scp username@example.com:/remote/path/to/file /local/path..
scp -r username@example.com:/remote/path/to/directory /local/path..
scp /local/file/path username@example:/remote/path..
scp -r /local/directory/path username@example:/remote/directory/path..

How do I copy a folder from local to remote SSH?

You can use either scp or rsync to copy folder and files from local to ssh or copy folder and files from ssh to local within in the same or different directory. By default copy files and folders happen sequentially. If you wish to copy directory and contents in parallel then you must use pscp or pssh tool.

Chủ đề