Tag Archives: Linux Commands

SCP Command use tips and tricks

scp command

What is SCP

scp stands for secure cp (copy), which means you can copy files across ssh connection(securely encrypted). It is a very secure way to copy files between computers.

It uses by default the port 22, and connect via an encrypted connection using SSH, in short it using all security setting applied for an ssh connections, for transferring data over network during copy of file from one host to another.

When copying a source file to a target file which already exists, scp will replace the contents of the target file (keeping the inode).

scp have various options, which can be specified over command line.

Few Examples of Scp Commands

Copy one single local file to a remote destination

scp [options [-r Recursively copy entire directories]] /path/to/source-file username@hostname:/path/to/destination-folder/

So, if you want to copy the file /home/user/table.csv to a remote host named host.example.com and copy there to abhishek’s home folder, use this command.

scp /home/user/table.csv username@host.example.com:/home/abhishek/

Copy one single file from a remote server to your current local server

scp username@hostname:/path/to/source-file /path/to/destination-folder

example body could be similar to .
scp username@host.deliverchoices.co.in:/home/abhishek/table.csv /home/abhishek/

You can copy multiple file from host to remote by just replacing file name with ‘directory-name/*’ options.

Copy one single file from a remote server to another remote server

With scp you can copy files between remote servers from a third server without the need to ssh into any of them, all weight lifting will be done by scp itself.

scp user1@server1:/path/to/file user2@server2:/path/to/folder/

Copy multiple files with one command

You can copy multiple files at once without having to copy all the files in a folder, or copy multiple files from different folders putting them in a space separated list.

scp file1.txt file2.txt file3.txt username@host.deliverchoices.co.in:/home/abhishek/

Copy all files in a folder recursively to a remote server

scp -r /home/abhishek/html/* username@host.deliverchoices.co.in:/home/abhishek/newfolder/

SSH Command Tips and Tricks

ssh man

SSH is some kind of an abbreviation of Secure SHell. It is a protocol that allows secure connections between computers.

In more detail we can summarize it as, ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote machine. It is intended to replace rlogin and rsh, and provide secure encrypted communications between two untrusted hosts over an insecure network.

ssh connects and logs into the specified hostname (with optional user name). The user must prove his/her identity to the remote machine using one of several methods depending on the protocol version used.

Few Use Case Examples

Assuming you are using any Linux(Like Ubuntu etc.) You can perform ssh command over terminal.

The simplest pattern would be of SSH command as below with minimum option.

ssh yourusername@yourserverip

You will still be asked for a password. For security reasons, it is not even possible to directly specify the password in the syntax. You will always be asked interactively, unless you start configuring the server in an advanced way like (You may have added your system public key to remote system from which you are connecting, in this way you will be only asked a few question during first time login, not the password for your ssh username using which you are logging to system).

You can specify your port too while issuing ssh command, if you do not wish to use default port(22) setting, due to any of reasons.

ssh -p portno yourusername@yourserverip

Sometimes, especially in scripts, you may want to connect to the remote server, and run a single command and then exit again.

ssh yourusername@yourserverip checkinfo

Authentication Methods

The methods available for authentication are: GSSAPI-based authentication, host-based authentication, public key authentication, challenge-response authentication, and password authentication. Authentication methods are tried in the order specified above, though protocol 2 has a configuration option to change the default order: PreferredAuthentications.

Public Key based Authentication

In most of situation you may find you are using public key based Authentication.
Public key authentication works as follows: The scheme is based on public-key cryptography, using cryptosystems where encryption and decryption are done using separate keys, and it is unfeasible to derive the decryption key from the encryption key. The idea is that each user creates a public/private key pair for authentication purposes. The server knows the public key, and only the user knows the private key. ssh implements public key authentication protocol automatically, using either the RSA or DSA algorithms.

Creating of public keys can be done using ssh-keygen command.