Copy file Linux from one server to another
Show
In my last article I shared the steps to encrypt a file using gpg key in Linux. Now in this article I will share various commands and tools which you can use to securely copy file from one server to another in Linux. There are additionally other ways to transfer files which I cannot cover here for example you can also use HTTPS to upload and download files. I may write another article with detail list of steps to use HTTPS and curl for secure file upload and download.If you wish to copy files between Windows and Linux then you can always use Samba but since here we are targeting file transfer between two Linux machines, I will not share any steps related to Samba configuration. Some more articles on related topic you may be interested in
In computing, the SSH File Transfer Protocol (also Secure File Transfer Protocol, or SFTP) is a network protocol that provides file access, file transfer, and file management over any reliable data stream. SFTP is easy to work with: You enter You can also automate the file transfer using SFTP in shell script, or you can also use one liner SFTP commands to perform file transfer rather than interactive sessions. [deepak@Ban17-inst01-a ~]$ sftp -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no Password: Once you give the password of sftp> ls -l -rw-r----- 1 root root 401 Dec 7 11:53 new_key.pub -rw-r----- 1 deepak users 9 Dec 10 14:13 pwd.txt drwxr-xr-x 2 root root 4096 Nov 28 11:38 scripts -rw-r--r-- 1 root root 0 Dec 10 14:07 test_file Next to copy file from host to client (i.e. upload a file from host to client) sftp> put /home/deepak/pwd.txt .
Uploading /home/deepak/pwd.txt to /home/deepak/./pwd.txt
/home/deepak/pwd.txt 100% 9 26.6KB/s 00:00 To copy a directory and all it's content use (-r). Here /home/deepak/mydir is available on my host machine which I am copying to the connected client node under current working directory. sftp> put -r /home/deepak/mydir .
Entering /home/deepak/mydir/ So the file was successfully uploaded. You can verify the same sftp> ls
new_key.pub pwd.txt scripts test_file Next copy a file from client node to your host server. I have a file 'test_file' on my client node under '/home/deepak/test_file' sftp> get test_file /tmp/
Fetching /home/deepak/test_file to /tmp/test_file Validate the same on your host server # ls -l /tmp/test_file -rw-r----- 1 deepak deepak 0 Dec 10 14:09 /tmp/test_file You can get more supported options from the man page of Using RSYNC to copy file from one server to anotherrsync is a utility that you can use to copy file from one server to another very easily, and there are many options available to allow you to be very specific about how you want the data transferred. Another aspect that makes rsync flexible is the many ways you can manipulate the source and target directories. However, you don't even have to use the network; you can even copy data from one directory to another on the same server. Copying a file within the same server from one location to another # rsync -r /home/deepak/mydir/test /tmp/ Here we are using NOTE: To copy files between two servers # rsync -av test :/tmp/ Password: sending incremental file list sent 44 bytes received 12 bytes 22.40 bytes/sec total size is 5 speedup is 0.09 Using SCP to copy file from one server to anotherA useful alternative to rsync is the Secure Copy (SCP) utility to copy file from one server to another, which comes bundled with OpenSSH. It allows you to quickly copy files from one node to another. If your goal is to send a single file or a small number of files to another machine, SCP is a great tool you can use to get the job done. To utilize SCP, we'll use the scp command. Since you most likely already have OpenSSH installed, you should already have the scp command available Using SCP is very similar in nature to rsync. The command requires a source, a target, and a filename. To transfer a single file from your local machine to another, the resulting command would look similar to the following: # scp -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null host_list :/tmp/ Password: host_list 100% 30 83.2KB/s 00:00 If you do not specifiy the target directory while doing scp, then the home directory of the target user will be used are destination. # scp -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null host_list : Password: IMPORTANT NOTE: With our previous # scp -r -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null /home/deepak/mydir : Password: Using NFS to share file from one server to anotherA Network File System (NFS) is a great method of sharing files between Linux or UNIX servers. I have written another article with detailed steps to setup NFSv4 and NFSv3 with examples in RHEL/CentOS 7 and 8 Linux On my RHEL node I have installed # cat /etc/exports /share *(rw,no_root_squash) /share 10.0.2.0/255.255.255.0(rw,no_root_squash)
NOTE: Next restart your # systemctl restart nfs-server.service To check the list of shares currently exported # exportfs -v /share 10.0.2.0/255.255.255.0(rw,sync,wdelay,hide,no_subtree_check,sec=sys,secure,no_root_squash,no_all_squash) /share Now try to mount the directory [root@node1 ~]# mount -t nfs node2:/share /mnt So our directory mount is successful, Next validate the content [root@node1 ~]# cd /mnt/ [root@node1 mnt]# ls test1 test2 We can validate the same on our client node using below command [root@node1 mnt]# mount | grep share node2:/share on /mnt type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=10.0.2.20,local_lock=none,addr=10.0.2.21) NOTE: After successfully mounting the share on the client node, you can copy the file locally to your node. # cp -av /mnt/* /tmp/ ‘/mnt/test1’ -> ‘/tmp/test1’ ‘/mnt/test2’ -> ‘/tmp/test2’ Using SSHFS to copy file from one server to anotherSSH Filesystem (SSHFS) is a file sharing solution similar to NFS and Samba. NFS and Samba are great solutions for designating file shares but these technologies may be more complex than necessary if you want to set up a temporary file-sharing service to use for a specific period of time. SSHFS allows you to mount a remote directory on your local machine, and have it treated just like any other directory. The mounted SSHFS directory will be available for the life of the SSH connection and can be used to copy file from one server to another. Drawbacks of using SSHFS
SSHFS is part of EPEL repository, which you can install using yum # yum -y install sshfs For SSHFS to work, we'll need a directory on both your local Linux machine as well as a remote Linux server. SSHFS can mount any directory from the remote server where you have SSH access. Here I am mounting [root@node1 ~]# sshfs root@node2:/share /mnt root@node2's password: Now validate the content of [root@node1 ~]# cd /mnt/ [root@node1 mnt]# ls test1 test2 [root@node1 mnt]# mount | grep share root@node2:/share on /mnt type fuse.sshfs (rw,nosuid,nodev,relatime,user_id=0,group_id=0) Now you can copy the files from [root@node1 mnt]# cp -av * /tmp/ ‘test1’ -> ‘/tmp/test1’ ‘test2’ -> ‘/tmp/test2’ Once your copying is complete, manually unmount the
respective directory. There are two ways to do so. First, we can use the [root@node1 ~]# umount /mnt/ You can also use HTTPS for file sharing, although it would be more like of file uploading and downloading via GET and PUT using curl command. Lastly I hope the commands from this article to copy file from one server to another in Linux or Unix was helpful. So, let me know your suggestions and feedback using the comment section. Can we copy file from one server to another in Linux?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.
What are the two ways to copy files from one server to another?FTP and SCP are helpful way if you want to transfer files from local to remote and vice versa. If you use SCP, it still can transfer files between two remote servers.
|