How Can We Help?

SSH and SCP command in Linux

Table of Content

SSH and SCP 

1. SSH

Secure shell (SSH) is used to connect Linux servers remotely. By default, SSH uses port number 22. The SSH protocol uses encryption to secure the connection.

By default SSH service on linux server is installed and enabled. Installation of SSH daemon on Centos server.

#sudo yum –y install openssh-server openssh-clients

Installation in Ubuntu

#apt-get install openssh-server

Now after the installation we need to start and enable the SSH service on the server.

#systemctl enable sshd 
#systemctl start sshd

How to connect SSH 

For windows machines we are using Putty tool to connect SSH. For Linux machines we are using terminal to run SSH command to connect. Syntax for SSH command is 

Syntax: # ssh username@hostname

Example:

ssh root@103.127.30.21

If we are not using the username in the following command then by default SSH command connects to the currently logged in user in the remote server, so mentioning user is very important. There are two authentication methods available for SSH.

  1. Using Password
  2. Using Public and private key

Using Password Authentication

Use the following command to connect server.

# ssh root@103.209.147.217

If you connects to the server for the first time, then a message comes to add the server in known_hosts file. Now write yes and then press enter and then type your password for authentication.

Using Private and Public key

This is the most secure method of accessing a server. So for this we have to generate the key, so for generating the key we use ssh-keygen command 

#ssh-keygen

By default public and private keys are stored under the root directory of the user. We can also change the path of keys.After generating the keys we need to copy those keys to the remote server by using ssh-copy-id command.

#ssh-copy-id user@hostname

Note: Once you run the above command, it will ask the destination's password of the user and then you can access the server without using any password. -i option is used when you are not using the default path of ssh public and private keys.

Here the same concept will apply if you are not specifying the user name the ssh-copy-id command will search for the same user from which you are logged in your base machine.

After that you can access the server without using the password.

For more options of ssh command please read the manual page of ssh command.

 man ssh 

2. SCP

Secured copy command is used to copy the files from one server to another. This command uses ssh utility to share files remotely.scp also uses the same authentication and same level of security like ssh command.While copying one thing always keep in mind that scp will replace the content of file while copying the data from source to destination.So the basic syntax of SCP command is  Please Note that, it is compulsory to have scp command on both the servers

Syntax: #scp source_file destination_file

Example:
From current server to another server

#scp file1 root@103.127.30.21:/root/

From one remote server to another remote server

 #scp root@103.127.30.35:/home/user/   root@103.127.30.21:/root/dir/ 

If you are using ssh service on different port so you need to define the port number for that we need to use -P option

#scp -P 2230 /home/user/file2 root@103.127.30.21:/root/dir/

-p (small p) option is used to preserve the file modification time and access time

-r option is used to copy the data recursively

Table of Contents