24.6 C
Jaipur
Saturday, July 31, 2021

How to use SSH config file – Linux Hint

Must read

When the user needs to access the remote servers frequently using SSH protocol, then the user will require to remember the IP addresses, usernames, different port numbers, and command-line options. But it is not an efficient way to do the tasks. This problem can be solved in multiple ways. The user can create the alias command of the bash for the remote connection that is easier to remember. Another solution is to create an SSH config file for each user to save the different SSH options for the different remote systems. The use of SSH config files for accessing the remote servers regularly have shown in this tutorial.

Prerequisites

Before starting the steps of this tutorial, the following steps will be required to complete.

Enable the SSH service on Ubuntu if it is not enabled before.

Generate the SSH Key pairs to execute the commands in the remote server. Run the following command to create the public key and the private key. The private key will be stored in the remote server, and the public keys will be stored in the client securely.

Run the following command to open the sshd_config file using nano editor to add some necessary configurations.

$ sudo nano /etc/ssh/sshd_config

Add the following lines in the file to enable the root login and password-based authentication.

PasswordAuthentication yes
PermitRootLogin yes

Run the following command to restart the SSH service.

$ sudo service ssh restart

Common SSH configuration options

Different configuration options can be set in the config file of the client machine for different purposes. Some of the useful options have explained in the following table.

Option Purpose
HostName It is used to define the hostname or IP address of your remote server. If the host identifier defines the original hostname, then it is not required to set.
User It is used to set the username of the remote server.
Port It is used to define the port number that is used for listening to the server connection. The default port number is 22.
Compression The compression will be used if it is set to yes. The default value of this option is no.
ForwardX11 It is used to redirect the X11 connection automatically over the secure channel and the DISPLAY set. The value of this option can be yes or no. The default value is no.
IdentityFile It is used to specify the public key path that the SSH client will use for authentication.
LogLevel It is used to define the verbosity level that is used when logging messages from ssh. The values of this option can be QUIET, FATAL, ERROR, INFO, VERBOSE, DEBUG, DEBUG1, DEBUG2, and DEBUG3. The default value is INFO.
ServerAliveCountMax It is used to set the number of server alive messages.
ServerAliveInterval It is used to set the timeout interval in seconds, after which if no response has been received from the server. The default value of this option is 0, which means no message will be sent to the server.

SSH Client Config Files

You can use the default client-side configuration file to set up config values, and the file path is /etc/ssh/ssh_config. It contains the settings that are applicable for all users of the SSH client. But if you want to apply the setting for the particular user, it is better to use the custom configuration file applicable to that user. How to use a custom client-side config file has been shown in the next part of this tutorial.

Create User Specific SSH Configuration File

Create a folder named .ssh inside the home directory of the client user and create a configuration file named config with the following content inside this folder. Here, three types of hosts have been defined. The first host is ‘fahmida,’ and the hostname is ‘Yasmin. It will connect to the SSH server using the default port, 22. The second host is ‘fahmida.com.bd,’ and the hostname is an IP address. The value of the ForwardX11 is set to yes for the second host, which means it will automatically redirect the X11 connection over the secure channel. The parameters of the third host are defined for all hosts. The IP address values, port number, IndentityFile, compression, ServerAliveInterval, and ServerAliveCountMax parameters have been defined in the third host. The IdentifyFile parameter has defined the location of the public key. The compression parameter has been defined to compress the data. ServerAliveInterval and ServerAliveCountMax parameters have been defined to increase the SSH connection time.

Host fahmida
     HostName Yasmin
   
Host fahmida.com.bd
     HostName 10.0.2.15
     ForwardX11 yes

Host *
     User Ubuntu
     HostName 10.0.2.15
     Port 22
     IdentityFile ~/.ssh/id_rsa
     Compression yes
     ServerAliveInterval 60
     ServerAliveCountMax 20

Run the SSH command for different hosts

Run the following ssh command to connect with the host, ‘fahmida,’ and the hostname, ‘Yasmin.  Here, the -i option has been used with the ssh command to mention the path of the public key.

$ ssh -i ~/.ssh/id_rsa [email protected]

The following output will appear if the SSH connection is established properly with the server.

Run the following ssh command to connect with the host, ‘fahmida,’ and the IP address.  Here, the -i option has been used with the ssh command to mention the path of the public key, and the -p option has been used to define the port number.

$ ssh -i ~/.ssh/id_rsa -p 22 [email protected]

The following output will appear if the SSH connection is established properly with the server.

Run the following ssh command without any option to connect with the host, ‘fahmida,’ and the hostname, ‘fahmida.com.bd.’

The following output will appear if the SSH connection is established properly with the server.

Conclusion

Using the custom SSH config file for making an SSH connection with the server has been shown in this tutorial by using the local host of the two accounts. You can follow the same process to make an SSH connection with the host of the remote network.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article