How To Remotely Copy Files Over SSH Ubuntu Client

Using SSH protocol not only access your remote shell. But also use it for transfer files between computers securely through encrypted communication. You can use any one way out of following:

  1. scp (Secure Copy)
  2. sftp (Secure File Transfer Protocol)

SCP (Secure Copy):

scp stands for secure cp (copy), which means you can copy files across ssh connection. It is a very secure way to copy files between two computers because the connection securely encrypted.

scp uses by default the port 22, and connect via an encrypted connection or secure shell connection (SSH). Using scp you can copy files from your local computer to remote computer and remote computer to another remote computer.

Examples of scp:

Copy single file from local to a remote destination:

scp /path/to/source-file user@host:/path/to/destination-folder/

Assume that we have a file test.txt in local directory /home/technhit/test.txt and want to copy to the destination server root@192.168.182.50 in tmp directory.

scp

Copy single file from a remote server to your current local server:

 scp user@host:/path/to/source-file /path/to/destination-folder

Assume that we similarly have a test2.txt file on the remote server /tmp/test2.txt and copy to the local system.

scp

scp

Copy single file from a remote server to another remote server

 scp user1@server1:/path/to/file user2@server2:/path/to/folder/

Let’s assume that we have two remote server one is root@192.168.182.50  and another is technhit@192.168.182.51 and operating scp through our local system.

 scp technhit@192.168.182.51:/home/technhit/test3.txt root@192.168.182.50:/tmp/

Copy multiple files with one command

We can copy multiple files using scp, file names followed by space with each.

 scp file1.txt file2.txt file3.txt technhit@192.168.182.51:/home/technhit/

Copy files using wildcards:

 scp /path/to/folder/*.ext user@server:/path/to/folder/

For example,  we want to copy all .txt file from /home/technhit to /tmp/ directory of host 192.168.182.50

 scp /home/technhit/*.txt root@192.168.182.50:/tmp/

Copy a directory and all its contents to a remote server

 scp -rp /path/to/source-directoy user@server:/path/to/destination-directory/
  • -r means recursive
  • -p preserves modification times, access times, and modes from the original file.

The above syntax copy entire directory with its associative file information. One more time we’ll use an example.

 scp -rp /home/technhit/ root@192.168.182.50:/tmp/

scp

You can use scp on Linux, Mac and Windows (using WinSCP). Download WinSCP fro here 

winscp

winscp

SFTP Secure File Transfer Protocol:

The SSH File Transfer Protocol ( SFTP ) is a network protocol that provides file access, file transfer, and file management functionalities over the secure connection.

The SFTP server function is enabled by default in Ubuntu, but if it is not enabled, comment out or add the line [Subsystem sftp / usr / lib / openssh / sftp-server] to [/ etc / ssh / sshd_config] After that restart ssh service.

Login remote server for sftp file transfer:

sftp user@hostnmane

sftp

Remote current directory display:

sftp> pwd
Remote working directory: /root

Display local current directory:

sftp> !pwd
/home/technhit

Display file list of the remote current directory:

sftp> ls -l
drwx------ 3 root root 4096    Jan 25 21:20    systemd-private
drwxr-xr-x 4 root root 4096    Jan 26 21:52    technhit
-rw-r--r-- 1 root root 31      Jan 26 20:40    test.txt
-rw-r--r-- 1 root root 22      Jan 26 21:26    test2.txt
-rw-r--r-- 1 root root 0       Jan 26 21:58    test3.txt
drwx------ 2 root root 4096    Jan 25 21:20    vmware-root

Display the file list of the local current directory:

sftp> !ls -l
total 8
-rw-r--r-- 1 technhit technhit 22    Jan 26 21:28    test2.txt
-rw-rw-r-- 1 technhit technhit 0     Jan 26 21:52    test3.txt
-rw-rw-r-- 1 technhit technhit 31    Jan 26 20:27    test.txt

Directory move:

sftp> cd /tmp

sftp>pwd
Remote working directory: /tmp

Rename files remotely:

sftp> put test.txt ubuntutest.txt
Uploading test.txt to /tmp/ubuntutest.txt
test.txt 100% 31 0.0KB/s 00:00
sftp> ls -l
drwx------ 3 root root 4096    Jan 25 21:20    systemd-private
drwxr-xr-x 4 root root 4096    Jan 26 21:52    technhit
-rw-r--r-- 1 root root 31      Jan 26 20:40    test.txt
-rw-r--r-- 1 root root 22      Jan 26 21:26    test2.txt
-rw-r--r-- 1 root root 0       Jan 26 21:58    test3.txt
-rw-r--r-- 1 root root 31      Jan 26 23:43    ubuntutest.txt
drwx------ 2 root root 4096    Jan 25 21:20    vmware-root

Upload multiple local files remotely:

sftp> put *.txt
Uploading test.txt to /tmp/test.txt
test.txt            100%               31 0.0KB/s           00:00
Uploading test2.txt to /tmp/test2.txt
test2.txt           100%               22 0.0KB/s           00:00
Uploading test3.txt to /tmp/test3.txt
test3.txt           100%               0 0.0KB/s            00:00

Download the remote file locally:

sftp> get test.txt
Fetching /tmp/test.txt to test.txt
/tmp/test.txt            100%          31 0.0KB/s          00:00

Batch download of multiple remote files locally:

sftp> get *.txt
Fetching /tmp/test2.txt to test2.txt
/tmp/test2.txt           100%          22 0.0KB/s          00:00
Fetching /tmp/test3.txt to test3.txt
/tmp/test3.txt           100%          31 0.0KB/s          00:00
Fetching /tmp/ubuntutest.txt to ubuntutest.txt
/tmp/ubuntutest.txt      100%          31 0.0KB/s          00:00

Create a directory in the remote current directory:

sftp> mkdir testdirectory
sftp> ls -l
drwx------ 3 root root 4096    Jan 25 21:20    systemd-private
drwxr-xr-x 4 root root 4096    Jan 26 21:52    technhit
-rw-r--r-- 1 root root 31      Jan 26 23:45    test.txt
-rw-r--r-- 1 root root 22      Jan 26 23:45    test2.txt
-rw-r--r-- 1 root root 0       Jan 26 23:45    test3.txt
drwxr-xr-x 2 root root 4096    Jan 26 23:46    testdirectory
-rw-r--r-- 1 root root 31      Jan 26 23:43    ubuntutest.txt
drwx------ 2 root root 4096    Jan 25 21:20    vmware-root

Delete directory in remote current directory:

sftp> rmdir testdirectory
sftp> ls -l
drwx------ 3 root root 4096    Jan 25 21:20    systemd-private
drwxr-xr-x 4 root root 4096    Jan 26 21:52    technhit
-rw-r--r-- 1 root root 31      Jan 26 23:45    test.txt
-rw-r--r-- 1 root root 22      Jan 26 23:45    test2.txt
-rw-r--r-- 1 root root 0       Jan 26 23:45    test3.txt
-rw-r--r-- 1 root root 31      Jan 26 23:43    ubuntutest.txt
drwx------ 2 root root 4096    Jan 25 21:20    vmware-root

Delete file Remotely:

sftp> rm test.txt
Removing /tmp/test.txt
sftp> ls -l
drwx------ 3 root root 4096    Jan 25 21:20    systemd-private
drwxr-xr-x 4 root root 4096    Jan 26 21:52    technhit
-rw-r--r-- 1 root root 22      Jan 26 23:45    test2.txt
-rw-r--r-- 1 root root 0       Jan 26 23:45    test3.txt
-rw-r--r-- 1 root root 31      Jan 26 23:43    ubuntutest.txt
drwx------ 2 root root 4096    Jan 25 21:20    vmware-root

Exit Sftp Terminal:

sftp> quit
quit
technhit@ubuntu:~$

Conclusion :

So this entire article we learn how to use scp and sftp using ssh. hope this helpful for you. Thank you have a good day.

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.