前提
意図した名前のhostnameになっていること。なってない場合はhostnamectl set-hostname fooとかで設定する。
$ hostnamectl
Static hostname: yumetodo-vm-ubuntu2204-rbenv
Icon name: computer-vm
Chassis: vm
Machine ID: 29c64690abb84668836a03dc707fed75
Boot ID: cfdd3f3a5132490694a711f12b6f3ff6
Virtualization: microsoft
Operating System: Ubuntu 22.04.2 LTS
Kernel: Linux 5.15.0-70-generic
Architecture: x86-64
Hardware Vendor: Microsoft Corporation
Hardware Model: Virtual Machine
hostnameでsshしたい
Hyper-Vでdefault switchでマシーンを立てると毎回IPが変わりがちなので、hostnameでsshしたくなる。
そのためには名前解決が必要で、昔だったらSMBのセットアップをするところだが、今の時代はmDNSがあるのでこれを使う。
Avahi導入
というわけで、Avahi - ArchWikiを導入する必要がある。
-
avahi-daemonのinstall
- ubuntu系の場合:
sudo apt install -y avahi-daemon - RedHat系の場合:
sudo dnf install -y epel-release && sudo dnf install -y avahi nss-mdns - Arch系の場合:
sudo pacman -S avahi nss-mdns
- ubuntu系の場合:
-
avahi-daemonを起動する。
-
sudo systemctl status avahi-daemonでActive: acitve(running)になっているか確認する。なってれば起動してる。 - 立ち上がっていなければ
sudo systemctl start avahi-daemonで起動できる
-
-
/etc/nsswitch.confを確認する。mdns4_minimal [NOTFOUND=return]ないしmdns_minimal [NOTFOUND=return]がresolveやdnsの前に書かれていればいい。書かれてなければ追記する。$ cat /etc/nsswitch.conf | grep hosts hosts: files mdns4_minimal [NOTFOUND=return] dns -
ファイアウォールを確認する。
- ubuntu系の場合:
sudo ufw status,状態: 非アクティブか、5353/udp ALLOW Anywhereがあれば問題なし。それ以外はsudo ufw allow mdnsする - RedHat系の場合:
sudo firewall-cmd --add-service=mdns --zone=public --permanent && sudo firewall-cmd --reloadする。sudo firewall-cmd --list-allで状態を見れる
- ubuntu系の場合:
ここまですると、host側でping <host name>などとすると次のようなパケットが確認でき、応答が帰るはずである。
CentOS6
色々頑張ると今からでもCentOS6でmDNSすることはできる。
