Bog'liq Mastering Ubuntu Server Gain expertise in the art of deploying, configuring, managing, and troubleshooting Ubuntu Server by Jay LaCroix (z-lib.org)
[ 320 ] With this example, we're assuming that
myfile.txt
is located in the
home
directory
for the user
jdoe
. This command will copy that file to the current working directory
of our local machine, since I designated the local path as a
single period (which
corresponds to our current working directory). Using
scp
in reverse isn't always
practical, since you have to already know where the desired file is stored on the
target before transferring it.
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:
scp -r /home/jdoe/downloads/linux_iso jdoe@192.168.1.50:downloads
With this example, we're copying the local folder
/home/jdoe/downloads/linux_iso
to remote machine
192.168.1.50
. Since we used the
-r
option,
scp
will transfer the
linux_iso
folder and all of its contents. On the remote end, we're again connecting
via the user
jdoe
. Notice that the target path is simply
downloads
. Since
scp
defaults
to the user's
home
directory, this will copy the
linux_iso
directory from the source
machine to the target machine under the
/home/jdoe/downloads
directory. The
following command would've had the exact same result:
scp -r /home/jdoe/downloads/linux_iso jdoe@192.168.1.50:/home/jdoe/
downloads
The
home
directory is not the only assumption the
scp
command makes. It also
assumes that SSH is listening on port
22
on the remote machine. Since it's possible to
change the SSH port on a server to something else, port
22
may or may not be what's
in use. If you need to designate a different port for
scp
to use, use the
-P
option:
scp -P 2222 -r /home/jdoe/downloads/linux_iso
jdoe@192.168.1.50:downloads
With that example, we're connecting to the remote machine via port
2222
. If you've
configured SSH to listen on a different port, change the number accordingly.
Although port
22
is always the default for OpenSSH, it's common
for some administrators to change it to something else. While
changing the SSH port doesn't add a great deal of benefit in regard
to security (an intensive port scan will still find your SSH daemon),
it's a relatively easy change to make, and making it even just a little
bit harder to find is beneficial. We'll discuss this further in
Chapter 21 ,
Securing Your Server .