How to connect through SSH without typing the access parameters: The config file

stats con chris
2023-08-09
0

...

We show the steps to follow in Linux to connect through SSH using the config file; in this way, we avoid continually typing the access parameters.

We show the steps to follow in Linux to connect through SSH using the config file; in this way, we avoid continually typing the access parameters.

In the article: "How to connect through SSH without passwords: The public key authentication.", we manage to have an SSH connection without using passwords. For this, in the terminal we type:

ssh -i ~/.ssh/nepy.key ortiz@87.123.243.210 -p 1132

As you can see, despite having passwordless access, we still have to specify the path to the encrypted SSH key, i.e., -i ~/.ssh/nepy.key, but not only that, we also have to specify the username and the IP. To simplify this cumbersome command, in this article, we will make use of the config file, which will give similar results to the bash alias, explained in the article: "How to connect through SSH without typing the access parameters: The bash alias." For this, we will consider the devices office and hp, given in Fig. 1, and our goal will be to connect from office to ortiz@hp using the config file.

Fig. 1: Dispositivos en redes distintas.

A summary of this article with some extra info appears in the following video:

The steps to follow are:


1. Create the config file

Open the config file. By default, this file is located in the /home/<username>/.ssh directory. However, this may change depending on the system configuration; therefore, the most practical way to access it is by considering cd ~/.ssh. For editing, we use the vi (or vim) editor:

vi ~/.ssh/config

If the file does not exist, we create it with the appropriate permissions:

touch ~/.ssh/config && chmod 600 ~/.ssh/config

In the file, we type the following:

# connect to hp
Host hp   
  Hostname 87.123.243.210   
  User ortiz   
  Port 1132   
  IdentityFile ~/.ssh/nepy.key

Note that if you use a dynamic IP, e.g., your home IP, then it seems that using the config file would not be suitable for you because you would have to continually change the IP. However, one way to fix this is by considering a DNS (Domain Name System), which will remain fixed even when the IP changes over time. The steps to set a DNS are described in the article: "How to configure a free domain name for a dynamic IP." In this example, having configured the DNS, in the config file it would be enough to type:

# connect to hp
Host hp   
  Hostname nepy.ddns.net   
  User ortiz   
  Port 1132   
  IdentityFile ~/.ssh/nepy.key

Once this is done, we save it using the vi (or vim) commands.


2. Access to the remote device

To connect to ortiz@hp, we type the following in the terminal:

ssh hp

As you can see, thanks to the config file, now the command to type is very simple. If you want to change any parameter or create another SSH access, you just have to repeat the steps given here. Note that all your SSH accesses are stored in the config file.

Views: 1

stats con chris

A writer who learned to add

A writer who learned to add

Notifications

Receive the new articles in your email

2022 © nepy

One single breath

Configure

Choose your own style

Color

Choose the perfect color mode for you


Navigation Position

Select a suitable navigation system


Vertical Navbar Style

Switch between styles for your vertical navbar

Customize