Linux Administration Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

Within your config file, create the start of four blocks.

One should be a wildcard block (using *) and the other should be variations on the name CentOS2 (note the capitals):

Host * !CentOS2-V6
IdentityFile ~/.ssh/id_ed25519
Port 22

Host CentOS2-V4
Hostname 192.168.33.11
User vagrant

Host CentOS2-V6
Hostname fe80::a00:27ff:fe56:c5a7%%eth1
IdentityFile ~/.ssh/id_rsa
Port 22
User vagrant

Host CentOS2-Hostname
Hostname centos2
User vagrant
Note that in the V6 entry, we actually use two percentage signs, instead of the single one we used on the command line. This is so that SSH doesn't misinterpret what we mean and try to read the entry with a %e value.

Inside these blocks, we've set a few basic options based on what we did previously on the command line.

With these settings in place, we can save and exit our configuration file, and try to connect to our specified hosts.

First, we're going to connect to our other VM on its IPv4 address:

[vagrant@centos1 ~]$ ssh CentOS2-V4 
Enter passphrase for key '/home/vagrant/.ssh/id_ed25519':
Last login: Wed Aug 8 13:31:41 2018 from fe80::a00:27ff:fe2a:1652%eth1
[vagrant@centos2 ~]$

Next, we're going to use our IPv6 address:

[vagrant@centos1 ~]$ ssh CentOS2-V6
Enter passphrase for key '/home/vagrant/.ssh/id_rsa':
Last login: Wed Aug 8 13:34:26 2018 from 192.168.33.10
[vagrant@centos2 ~]$

Finally, we're going to resolve the hostname of the host:

[vagrant@centos1 ~]$ ssh CentOS2-Hostname 
Enter passphrase for key '/home/vagrant/.ssh/id_ed25519':
Last login: Wed Aug 8 13:34:04 2018 from fe80::a00:27ff:fe2a:1652%eth1
[vagrant@centos2 ~]$
Most systems will also auto complete entries in the SSH config file. Try it out yourself by typing ssh C and hitting Tab three times.