PDF download Download Article
Use scp to securely move files between servers
PDF download Download Article

In a multi-server Linux environment, many tasks involve moving one or more files from one server to another. Depending on the number of files you need to move, there are several commands that may help you out. This wikiHow article will teach you how to use the scp command to securely transfer files and directories from one Linux system to another.

Things You Should Know

  • The syntax for the scp command is scp filename username@hostname:filename.
  • To transfer a directory, use scp -r /path/to/local_directory username@hostname:/path/to/remote_directory.
  • To copy multiple individual files at once, add them to a tarball first.
  1. You can use this as a "push" or a "pull" command, but let's start with pushing the file to the other server. Let's assume for these discussions that our servers are Alice and Madhat, and that our user on Alice is rabbit, and our user on madhat is Fieldmouse.
    • While on Alice, use the command scp localfile fieldmouse@madhat:remotefilename. This will copy the file over to the other system, into the fieldmouse userid, with the name "remotefilename".
    • If you were logged in on Madhat, you could just as easily pull the file with the command scp rabbit@alice:localfilename remotefilename and get the same results.
    • You can omit the remote file name if you want the file to keep the same name once transferred.
  2. This time we'll add the -r switch, to cause the copy to act recursively." scp -r /path/to/local_directory username@hostname:/path/to/remote_directory will copy the entire directory "/path/to/local_directory" over to the other system in its /path/to/remote_directory directory, including all of its subdirectories.
    Advertisement
  3. You could use the "tar" command to create a single file, and then copy that file as above, then use tar to expand it on the other server. But that would seem so un-Unix-like. There has to be a way to do it in a single step, right? Well of course!
    • Enter your favorite shell's pipes. We can still use tar to package up the files we want, then use ssh to get it to the other system (which is what scp has been using under the covers), and tar on the other side to expand the files back out.
    • But why waste time and space creating an actual tar file, when we could create a pipe that spans the two systems and transfer the tar data through it? Using the same directory as in the previous example, try tar -cf - mydir/*
  4. Advertisement


Expert Q&A

Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Tips

  • Using the above commands, You should replace the user name/hostname/file name/directory name according to your network configuration and environment. The commands shown above are only examples of executing commands for copying files on the server.
  • Be sure that UIDs and GIDs on the various systems you're using match up (Not just the user names). If they don't, interesting security problems will ensue.
Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement

About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 18 people, some anonymous, worked to edit and improve it over time. This article has been viewed 197,499 times.
How helpful is this?
Co-authors: 18
Updated: January 29, 2025
Views: 197,499
Categories: Linux
Thanks to all authors for creating a page that has been read 197,499 times.

Is this article up to date?

Advertisement