Upload a File From One Http Server to Another
In my last article I shared the steps to encrypt a file using gpg key in Linux. Now in this article I volition share various commands and tools which you can utilize to securely copy file from one server to some other in Linux. There are additionally other means to transfer files which I cannot cover hither for example y'all tin besides use HTTPS to upload and download files.
I may write another commodity with item list of steps to use HTTPS and ringlet for secure file upload and download.If you lot wish to re-create files between Windows and Linux then you tin always use Samba but since here we are targeting file transfer between ii Linux machines, I volition not share any steps related to Samba configuration.
Some more manufactures on related topic y'all may be interested in
- ii commands to copy folder from local to remote server or vice versa in Linux with examples
- Step-by-Pace Guide to setup SFTP chroot Jail to restrict user to a specfic directory when copying files in Linux
- How to securely transfer files betwixt ii hosts using HTTPS in Linux
Using SFTP to copy file from one server to another
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 lot enter sftp
along with the proper name of the remote system on the command line. You are prompted for the account countersign
; then you are dropped into SFTP with the connection open and waiting.
You can also automate the file transfer using SFTP in beat out script, or you lot can as well 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 deepak@x.43.138.2 Password:
In one case y'all give the countersign of deepak
user on target node, y'all will get sftp
crush
sftp> ls -fifty -rw-r----- 1 root root 401 Dec 7 eleven:53 new_key.pub -rw-r----- ane deepak users ix Dec 10 14:xiii pwd.txt drwxr-xr-x two root root 4096 November 28 11:38 scripts -rw-r--r-- 1 root root 0 Dec x 14:07 test_file
Next to copy file from host to client (i.due east. upload a file from host to customer)
Here I take a file 'pwd.txt
' on my host server nether '/home/deepak/pwd.txt
' which I wish to re-create to my client'due south current working directory
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). Hither /habitation/deepak/mydir is available on my host auto which I am copying to the connected customer node under current working directory.
sftp> put -r /abode/deepak/mydir . Entering /domicile/deepak/mydir/
So the file was successfully uploaded. You can verify the same
sftp> ls new_key.pub pwd.txt scripts test_file
Side by side copy a file from customer node to your host server. I have a file 'test_file' on my client node under '/dwelling/deepak/test_file'
sftp> go test_file /tmp/ Fetching /abode/deepak/test_file to /tmp/test_file
Validate the same on your host server
# ls -50 /tmp/test_file -rw-r----- 1 deepak deepak 0 Dec 10 14:09 /tmp/test_file
You tin get more supported options from the man page of sftp
.
Using RSYNC to copy file from one server to some other
rsync is a utility that you can use to copy file from i server to some other very easily, and in that location are many options bachelor to let yous to be very specific about how you want the data transferred. Another aspect that makes rsync flexible is the many ways you lot tin manipulate the source and target directories. However, you don't even have to use the network; you lot tin 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/
Hither we are using -r
to re-create recursively, yous can besides use (-a
) i.e. for archive which retains as much metadata as possible (in most cases, it should make everything an exact copy).
Annotation:
This works considering whenever rsync
runs, information technology will copy what's different from the last time information technology ran. The files from our first fill-in were already at that place, but the permissions were wrong. When we ran the second control, rsync
but needed to copy what was different, then it applied the correct permissions to the files. If any new files were added to the source directory since we last ran the command, the new or updated files would be copied over also.
To re-create files between 2 servers
# rsync -av test deepak@10.43.138.2:/tmp/ Password: sending incremental file list sent 44 bytes received 12 bytes 22.xl bytes/sec total size is v speedup is 0.09
Using SCP to re-create file from i server to another
A useful alternative to rsync is the Secure Copy (SCP) utility to copy file from one server to some other, which comes bundled with OpenSSH. It allows you to rapidly copy files from one node to another. If your goal is to send a single file or a modest number of files to another car, SCP is a great tool you lot can use to get the job done. To utilize SCP, we'll use the scp command. Since y'all nearly likely already have OpenSSH installed, y'all should already have the scp command available
Using SCP is very like in nature to rsync. The command requires a source, a target, and a filename. To transfer a single file from your local machine to some other, the resulting control would look like to the following:
# scp -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/zilch host_list deepak@10.43.138.2:/tmp/ Password: host_list 100% 30 83.2KB/south 00:00
If you practice not specifiy the target directory while doing scp, then the home directory of the target user volition exist used are destination.
# scp -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/goose egg host_list deepak@ten.43.138.ii: Password:
IMPORTANT Annotation:
Brand certain y'all e'er include at least the colon when copying a file, since if you don't include information technology, yous'll end upward copying the file to your current working directory instead of the target.
With our previous scp
examples, we've merely been copying a single file. If we want to transfer or download an entire directory and its contents, we volition need to use the -r pick, which allows u.s. to do a recursive copy:
# scp -r -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/nothing /domicile/deepak/mydir deepak@ten.43.138.two: Countersign:
Using NFS to share file from one server to another
A Network File Organization (NFS) is a great method of sharing files between Linux or UNIX servers. I take written another commodity with detailed steps to setup NFSv4 and NFSv3 with examples in RHEL/CentOS 7 and 8 Linux
On my RHEL node I have installed nfs-utils
and at present will setup my exports. To set up NFS, let'due south starting time create some directories that we will share to other users. Each share in NFS is known as an Export
.
# cat /etc/exports /share *(rw,no_root_squash) /share 10.0.2.0/255.255.255.0(rw,no_root_squash)
- In the first line I have given share admission to globe for
/share
directory. - In the second line, later on the directory is called out in a line, we're also setting which network is able to access them (
ten.0.2.0/255.255.255.0
in our instance). This means that if y'all're connecting from a different network, your admission volition be denied. - As far as what these options do, the first (
rw
) is rather self-explanatory. - One choice you'll see quite frequently in the wild is
no_root_squash
. Normally, the root user on one arrangement will get map to nobody on the other for security reasons. In near cases, one system having root access to another is a bad thought. Theno_root_squash
choice disables this, and it allows the root user on i end to be treated as the root user on the other. .
Note:
Cheque the man pages for export for more information on additional options you can pass to your exports.
Next restart your nfs-server
services on the server
# systemctl restart nfs-server.service
To cheque the list of shares currently exported
# exportfs -v /share ten.0.2.0/255.255.255.0(rw,sync,wdelay,hide,no_subtree_check,sec=sys,secure,no_root_squash,no_all_squash) /share <world>(rw,sync,wdelay,hide,no_subtree_check,sec=sys,secure,no_root_squash,no_all_squash)
Now endeavor to mount the directory /share
from the client side
[root@node1 ~]# mount -t nfs node2:/share /mnt
And 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 customer node using below control
[root@node1 mnt]# mount | grep share node2:/share on /mnt type nfs4 (rw,relatime,vers=four.i,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=x.0.2.20,local_lock=none,addr=10.0.2.21)
NOTE:
For our examples we take disabled the firewall and selinux
. So if y'all face whatever issues, check your iptables
and selinux
rules and add iptables rules to allow ssh connexion.
Later on successfully mounting the share on the customer node, you tin re-create the file locally to your node.
# cp -av /mnt/* /tmp/ '/mnt/test1' -> '/tmp/test1' '/mnt/test2' -> '/tmp/test2'
Using SSHFS to re-create file from one server to another
SSH Filesystem (SSHFS) is a file sharing solution similar to NFS and Samba. NFS and Samba are great solutions for designating file shares only these technologies may be more circuitous than necessary if you want to fix a temporary file-sharing service to apply for a specific period of fourth dimension. SSHFS allows you to mount a remote directory on your local machine, and accept 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 re-create file from one server to another.
Drawbacks of using SSHFS
- Performance of file transfers won't be as fast as with an NFS mount, since in that location's encryption that needs to be taken into consideration too
- Some other downside is that you'd desire to salvage your work regularly as you piece of work on files within an SSHFS mountain, considering if the SSH connection drops for any reason, you may lose data.
SSHFS is part of EPEL repository, which y'all tin can install using yum
# yum -y install sshfs
For SSHFS to work, we'll need a directory on both your local Linux machine too as a remote Linux server. SSHFS tin can mountain any directory from the remote server where you take SSH admission.
Here I am mounting /share
from node2
on node1
[root@node1 ~]# sshfs root@node2:/share /mnt root@node2's password:
At present validate the content of /mnt
and brand sure the path is properly mounted
[root@node1 ~]# cd /mnt/ [root@node1 mnt]# ls test1 test2 [root@node1 mnt]# mount | grep share root@node2:/share on /mnt blazon fuse.sshfs (rw,nosuid,nodev,relatime,user_id=0,group_id=0)
Now you can copy the files from /mnt
locally to any other directory
[root@node1 mnt]# cp -av * /tmp/ 'test1' -> '/tmp/test1' 'test2' -> '/tmp/test2'
Once your copying is consummate, manually unmount the respective directory. There are ii ways to exercise so. First, we can use the umount
command every bit the root
(just like we commonly would):
[root@node1 ~]# umount /mnt/
You tin also use HTTPS for file sharing, although it would be more similar of file uploading and downloading via Get and PUT using curl command.
Lastly I hope the commands from this commodity to copy file from 1 server to another in Linux or Unix was helpful. So, permit me know your suggestions and feedback using the annotate department.
Source: https://www.golinuxcloud.com/commands-copy-file-from-one-server-to-another-linux-unix/