- 5 commands to copy file from one server to another in Linux or Unix
- Using SFTP to copy file from one server to another
- Using RSYNC to copy file from one server to another
- Using SCP to copy file from one server to another
- Using NFS to share file from one server to another
- Using SSHFS to copy file from one server to another
- Drawbacks of using SSHFS
- Related Posts
- 10 thoughts on “5 commands to copy file from one server to another in Linux or Unix”
- How to copy a file from remote server to local machine? [closed]
- 4 Answers 4
- Linux copy file from network
- Command Line SCP/SFTP Clients
- Graphical SCP/SFTP Clients
- FTP Command Line Utility
- Examples
5 commands to copy file from one server to another in Linux or Unix
Table of Contents
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
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 enter sftp along with the name of the remote system on the command line. You are prompted for the account password ; then you are dropped into SFTP with the connection open and waiting.
Once you give the password of deepak user on target node, you will get sftp shell
Next to copy file from host to client (i.e. upload a file from host to client)
Here I have a file ‘ pwd.txt ‘ on my host server under ‘ /home/deepak/pwd.txt ‘ which I wish to copy to my client’s current working directory
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.
So the file was successfully uploaded. You can verify the same
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’
Validate the same on your host server
You can get more supported options from the man page of sftp .
Using RSYNC to copy file from one server to another
rsync 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
Here we are using -r to copy recursively, you can also use ( -a ) i.e. for archive which retains as much metadata as possible (in most cases, it should make everything an exact copy).
To copy files between two servers
Using SCP to copy file from one server to another
A 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:
If you do not specifiy the target directory while doing scp, then the home directory of the target user will be used are destination.
With our previous scp examples, we’ve only been copying a single file. If we want to transfer or download an entire directory and its contents, we will need to use the -r option, which allows us to do a recursive copy:
Using NFS to share file from one server to another
A 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 nfs-utils and now will setup my exports. To set up NFS, let’s first create some directories that we will share to other users. Each share in NFS is known as an Export .
- In the first line I have given share access to world for /share directory.
- In the second line, after the directory is called out in a line, we’re also setting which network is able to access them ( 10.0.2.0/255.255.255.0 in our case). This means that if you’re connecting from a different network, your access will be denied.
- As far as what these options do, the first ( rw ) is rather self-explanatory.
- One option you’ll see quite often in the wild is no_root_squash . Normally, the root user on one system will get map to nobody on the other for security reasons. In most cases, one system having root access to another is a bad idea. The no_root_squash option disables this, and it allows the root user on one end to be treated as the root user on the other. .
Next restart your nfs-server services on the server
To check the list of shares currently exported
Now try to mount the directory /share from the client side
So our directory mount is successful, Next validate the content
We can validate the same on our client node using below command
After successfully mounting the share on the client node, you can copy the file locally to your node.
Using SSHFS to copy 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 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
- Performance of file transfers won’t be as fast as with an NFS mount, since there’s encryption that needs to be taken into consideration as well
- Another downside is that you’d want to save your work regularly as you work on files within an SSHFS mount, because if the SSH connection drops for any reason, you may lose data.
SSHFS is part of EPEL repository, which you can install using yum
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 /share from node2 on node1
Now validate the content of /mnt and make sure the path is properly mounted
Now you can copy the files from /mnt locally to any other directory
Once your copying is complete, manually unmount the respective directory. There are two ways to do so. First, we can use the umount command as the root (just like we normally would):
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.
Related Posts
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
10 thoughts on “5 commands to copy file from one server to another in Linux or Unix”
Lovely just what I was searching for.Thanks to the author for taking his clock time on this one.
Excellent post. Keep writing such kind of info on your
blog. Im really impressed by your site.
This is the right site for everyone who hopes to understand this topic.
You know so much its almost tough to argue with you (not that I really would want to…HaHa).
You certainly put a brand new spin on a subject that’s been written about for decades.
Great stuff, just wonderful!
Can we transfer 20tb data of file from one system to anothersystem
Is it possible to transfer 20TB data of file from one system to another system using scp
20TB is a big data, theoretically I don’t see any problem. I would recommend using rsync as it will perform incremental copy. But copying such large chunk of data then you need heave bandwidth, memory resources in your system. With rsync atleast you can be safe to pick up from where you lost in case the transfer fails during the transaction.
Can we copy one public key for many systems for passwordless login to another systems
Hello, congratulations on the blog.
To big data, such as 500Gb or 1Tb, rysnc is better than scp. Have you used it to perform backups throughout the day? How to improve throughput, or bandwidth? Thanks
Hello, thank you for the feedback
In our production environment also we perform transfer of multiple file each 500GB+
Now generally when you attempt to copy such big file, the copy tool (rsync or any other tool) will eat up all the available bandwidth so it is very important that you assign a BW limit. In our network we have pre-defined a restriction of 1Gb so that other applications using BW don’t get impacted
You can use —bwlimit to limit the BW with rsync, also —compare-dest to make sure only newer data is copied (assuming the transfer was stopped due to some reason)
Источник
How to copy a file from remote server to local machine? [closed]
Want to improve this question? Update the question so it’s on-topic for Stack Overflow.
Closed 6 years ago .
In my terminal shell, I ssh’ed into a remote server, and I cd to the directory I want. Now in this directory, there is a file called table that I want to copy to my local machine /home/me/Desktop . How can I do this?
I tried scp table /home/me/Desktop but it gave an error about no such file or directory. Does anyone know how to do this?
4 Answers 4
For example, your remote host is example.com and remote login name is user1:
The scp operation is separate from your ssh login. You will need to issue an ssh command similar to the following one assuming jdoe is account with which you log into the remote system and that the remote system is example.com:
The scp command issued from the system where /home/me/Desktop resides is followed by the userid for the account on the remote server. You then add a «:» followed by the directory path and file name on the remote server, e.g., /somedir/table. Then add a space and the location to which you want to copy the file. If you want the file to have the same name on the client system, you can indicate that with a period, i.e. «.» at the end of the directory path; if you want a different name you could use /home/me/Desktop/newname, instead. If you were using a nonstandard port for SSH connections, you would need to specify that port with a «-P n» (capital P), where «n» is the port number. The standard port is 22 and if you aren’t specifying it for the SSH connection then you won’t need that.
When you use scp you have to tell the host name and ip address from where you want to copy the file. For instance, if you are at the remote host and you want to transfer the file to your pc you may use something like this:
Example:
scp -P22 table fake_user@111.111.111.11:/home/me/Desktop/
On the other hand, if you are at your are actually on your machine you may use something like this:
Example:
scp -P22 [fake_user]@222.222.222.222:/remote/path/table /home/me/Desktop/
Источник
Linux copy file from network
This lesson will introduce how to copy files over the network. You already know how to copy files from one location to another on the same system using cp command. But if you want to copy files from your local work station to a Linux server or between Linux servers you need to use SCP or SFTP.
- SCP is Secure copy.
- SFTP is SSH file transfer protocol.
SCP and SFTP are both extensions of the secure shell protocol.
Command Line SCP/SFTP Clients
In order to use SCP or SFTP you need a client.
- Mac and Linux come with scp or sftp command line utilities.
- If you are running Windows, you can use the PuTTY Secure Copy client — pscp.exe and the PuTTY Secure File Transfer client — psftp.exe.
With scp you need to know what files you want to transfer before running the command. The scp command format is
If you are looking for a more interactive experience where you can examine the local files and remote files, use sftp . The sftp command format is
For example: connect to reliawind server with username admin
Graphical SCP/SFTP Clients
There are graphical clients as well.
- Cyberduck is popular for Mac and Windows.
- FileZilla runs on Mac, Windows, and Linux.
- WinSCP is a Windows only SCP and SFTP client.
FTP Command Line Utility
SCP and SFTP are not the only way to transfer files to remote system. Sometimes FTP, called file transfer protocol, is enabled. In such cases you can use the built-in FTP command line client on Linux, Mac, or a graphical client like WinSCP for Windows.
The ftp command format is:
Important: Just be aware that FTP is not a secure transfer protocol like SCP and SFTP. This means that your login credentials are sent in plain text over the network. The files that you upload and download are not encrypted either. If given the choice between SCP, SFTP or FTP, pick SCP or SFTP.
Examples
After use sftp connected to the remote server servername, you can use pwd to check your current working directory on the server, and ls to show all the files & directories in the current remote directory. If you precede those commands with an l , that’s for local, lpwd display your local working directory, and lls will show files & directories in your current local directory.
Let’s put a file test.txt onto the server. Then you can type ls to check whether test.txt has been uploaded.
To quit a SFTP client, type quit .
With scp you can copy from your local system to the remote system, but you need to know the source and destination. So in this example, our source file is test.txt and the destination is servername. You can append a colon ( : ) at the end of the servername followed by a path name. For example: put this file in the tmp directory on the remote server.
You could put the file in home directory using the home directory shortcut tilda (
To check whether file has been transfered, you can do this with SSH or SFTP connection.
Источник