HowTo : Scp A File / Directory In Linux

November 13, 2012 | By
| Reply More

SCP, the secure copy program in Linux copies files between hosts on the network. It uses SSH to transfer the data. It provides the same security and uses same authentication as required by SSH. SSH is the secure shell program, which can be used for remote system access. SSH sends encrypted data over the network. A system can be accessed using SSH as if it were present on the local machine. This article provides necessary information for transferring files and directories using this protocol (with scp tool).

SCP

scp command can be used to copy directories to and from a remote machine, or between two remote machines. Simply copying a file can have following cases:

Copy a file FROM remote host

The following command will copy the file named ‘file_for_copy’.txt from 192.168.1.10 to the current directory.

$ scp 192.168.1.10:/home/raghu/file_for_copy.txt .
raghu@192.168.1.10's password:
file_for_copy.txt 100% 0 0.0KB/s 00:00

raghu@Inspiron-1440:~$ ls
file_for_copy.txt

Copy a directory FROM the remote host

The directories can be copied with -r option (like most commands like cp), i.e. recursively.

$ scp -r 192.168.1.10:/home/raghu/dir_for_copy .
raghu@192.168.1.10's password:
file1.txt 100% 0 0.0KB/s 00:00
file3.txt 100% 0 0.0KB/s 00:00
file2.txt 100% 0 0.0KB/s 00:00

$ ls -ld dir_for_copy
drwxrwxr-x 2 raghu raghu 4096 2012-11-07 15:11 dir_for_copy

Copy a file/directory TO the remote host

The -v option of scp prints verbose output. It causes scp and to print debugging messages about their progress. In the following example, we use -v option. Here, a local file is copied to the remote host.

$ scp -v examples.desktop 192.168.1.10:/home/raghu
Executing: program /usr/bin/ssh host 192.168.1.10, user (unspecified), command scp -v -t -- /home/raghu
OpenSSH_5.8p1 Debian-1ubuntu3, OpenSSL 0.9.8o 01 Jun 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 192.168.1.10 [192.168.1.10] port 22.
debug1: Connection established.
debug1: identity file /home/raghu/.ssh/id_rsa type -1
debug1: identity file /home/raghu/.ssh/id_rsa-cert type -1
debug1: identity file /home/raghu/.ssh/id_dsa type -1
debug1: identity file /home/raghu/.ssh/id_dsa-cert type -1
debug1: identity file /home/raghu/.ssh/id_ecdsa type -1
debug1: identity file /home/raghu/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_4.3
debug1: match: OpenSSH_4.3 pat OpenSSH_4*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.8p1 Debian-1ubuntu3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA d1:10:58:ed:bc:d5:0d:d2:56:8c:db:96:7a:8d:fd:28
debug1: Host '192.168.1.10' is known and matches the RSA host key.
debug1: Found key in /home/raghu/.ssh/known_hosts:5
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-with-mic,password
debug1: Next authentication method: gssapi-with-mic
debug1: An invalid name was supplied
Cannot determine realm for numeric host address

debug1: An invalid name was supplied
Cannot determine realm for numeric host address

debug1: An invalid name was supplied

debug1: Next authentication method: publickey
debug1: Trying private key: /home/raghu/.ssh/id_rsa
debug1: Trying private key: /home/raghu/.ssh/id_dsa
debug1: Trying private key: /home/raghu/.ssh/id_ecdsa
debug1: Next authentication method: password
raghu@192.168.1.10's password:
debug1: Authentication succeeded (password).
Authenticated to 192.168.1.10 ([192.168.1.10]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: scp -v -t -- /home/raghu
Sending file modes: C0644 179 examples.desktop
Sink: C0644 179 examples.desktop
examples.desktop 100% 179 0.2KB/s 00:00
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 2032, received 2008 bytes, in 0.5 seconds
Bytes per second: sent 4252.4, received 4202.2
debug1: Exit status 0

Similarly a directory can be copied with -r option. In following example, a directory named jvm is copied to the remote host.

$ scp -r jvm 192.168.1.10:/tmp
raghu@192.168.1.10's password:
file2.txt 100% 42 0.0KB/s 00:00
file.txt 100% 39 0.0KB/s 00:01

Copying files/directories between two hosts

To copy a file between two hosts, both the machines must be running ssh service. The -3 option will transfer between two remote hosts are through the local host. The following command copies the file /home/raghu/myfile1.txt from the machine 192.168.1.10 to /tmp directory on 192.168.1.20 machine, with verbose output.

$ scp -3 -v 192.168.1.10:/home/raghu/myfile1.txt 192.168.1.20:/tmp
Executing: program /usr/bin/ssh host 192.168.1.10, user (unspecified), command scp -v -f -- /home/raghu/myfile1.txt
Executing: 2nd program /usr/bin/ssh host 192.168.1.20, user (unspecified), command scp -v -t -- /tmp
OpenSSH_5.8p1 Debian-1ubuntu3, OpenSSL 0.9.8o 01 Jun 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 192.168.1.10 [192.168.1.10] port 22.
debug1: Connection established.
debug1: identity file /home/raghu/.ssh/id_rsa type -1
debug1: identity file /home/raghu/.ssh/id_rsa-cert type -1
debug1: identity file /home/raghu/.ssh/id_dsa type -1
debug1: identity file /home/raghu/.ssh/id_dsa-cert type -1
debug1: identity file /home/raghu/.ssh/id_ecdsa type -1
debug1: identity file /home/raghu/.ssh/id_ecdsa-cert type -1
OpenSSH_5.8p1 Debian-1ubuntu3, OpenSSL 0.9.8o 01 Jun 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 192.168.1.20 [192.168.1.20] port 22.
debug1: Connection established.
debug1: identity file /home/raghu/.ssh/id_rsa type -1
debug1: identity file /home/raghu/.ssh/id_rsa-cert type -1
debug1: identity file /home/raghu/.ssh/id_dsa type -1
debug1: identity file /home/raghu/.ssh/id_dsa-cert type -1
debug1: identity file /home/raghu/.ssh/id_ecdsa type -1
debug1: identity file /home/raghu/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.8p1 Debian-7ubuntu1
debug1: match: OpenSSH_5.8p1 Debian-7ubuntu1 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.8p1 Debian-1ubuntu3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Remote protocol version 2.0, remote software version OpenSSH_4.3
debug1: match: OpenSSH_4.3 pat OpenSSH_4*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.8p1 Debian-1ubuntu3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: ECDSA 00:d1:8a:b5:79:53:fd:72:43:2f:7d:68:28:e0:d1:21
debug1: Host '192.168.1.20' is known and matches the ECDSA host key.
debug1: Found key in /home/raghu/.ssh/known_hosts:8
debug1: Server host key: RSA d1:10:58:ed:bc:d5:0d:d2:56:8c:db:96:7a:8d:fd:28
debug1: Host '192.168.1.10' is known and matches the RSA host key.
debug1: Found key in /home/raghu/.ssh/known_hosts:5
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-with-mic,password
debug1: Next authentication method: gssapi-with-mic
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: /home/raghu/.ssh/id_rsa
debug1: Trying private key: /home/raghu/.ssh/id_dsa
debug1: Trying private key: /home/raghu/.ssh/id_ecdsa
debug1: Next authentication method: password
raghu@192.168.1.20's password:
debug1: Authentication succeeded (password).
Authenticated to 192.168.1.20 ([192.168.1.20]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: scp -v -t -- /tmp
Could not chdir to home directory /home/raghu: No such file or directory
debug1: An invalid name was supplied
Cannot determine realm for numeric host address

debug1: An invalid name was supplied
Cannot determine realm for numeric host address

debug1: An invalid name was supplied

debug1: Next authentication method: publickey
debug1: Trying private key: /home/raghu/.ssh/id_rsa
debug1: Trying private key: /home/raghu/.ssh/id_dsa
debug1: Trying private key: /home/raghu/.ssh/id_ecdsa
debug1: Next authentication method: password
raghu@192.168.1.10's password:
debug1: Authentication succeeded (password).
Authenticated to 192.168.1.10 ([192.168.1.10]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: scp -v -f -- /home/raghu/myfile1.txt
Sending file modes: C0664 0 myfile1.txt
Sink: C0664 0 myfile1.txt
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 1856, received 1992 bytes, in 0.3 seconds
Bytes per second: sent 5482.1, received 5883.8
debug1: Exit status 0
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 1800, received 1664 bytes, in 15.5 seconds
Bytes per second: sent 116.4, received 107.6
debug1: Exit status 0

Similarly directories can be copied between two remote machines with -r option.

$ scp -3 -r 192.168.1.10:/home/raghu/mydir 192.168.1.20:/tmp
raghu@192.168.1.20's password:
Could not chdir to home directory /home/raghu: No such file or directory
raghu@192.168.1.10's password:

SCP With A Port Number

Suppose, the SSH port of the remote machine is 2200 (rather than the default SSH port 22), you need to execute it as follows.

[test@clientserver ~]$ scp –P 2200 -r test_folder test@192.168.1.26:/home/test/

Filed Under : HOWTOS, LINUX HOWTO

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.