LoginSignup
0
0

More than 1 year has passed since last update.

Hyper-Vに立てたLinuxマシーンにhostnameでsshするためにmDNSを設定しよう

Last updated at Posted at 2023-05-09

前提

意図した名前の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を導入する必要がある。

  1. avahi-daemonのinstall

    • ubuntu系の場合: sudo apt install -y avahi-daemon
    • RedHat系の場合: sudo dnf install -y epel-release && sudo dnf install -y avahi nss-mdns
  2. avahi-daemonを起動する。

    1. sudo systemctl status avahi-daemonActive: acitve(running)になっているか確認する。なってれば起動してる。
    2. 立ち上がっていなければsudo systemctl start avahi-daemonで起動できる
  3. /etc/nsswitch.confを確認する。mdns4_minimal [NOTFOUND=return]ないしmdns_minimal [NOTFOUND=return]resolvednsの前に書かれていればいい。書かれてなければ追記する。

    $ cat /etc/nsswitch.conf | grep hosts
    hosts:          files mdns4_minimal [NOTFOUND=return] dns
    
  4. ファイアウォールを確認する。

    • 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で状態を見れる

ここまですると、host側でping <host name>などとすると次のようなパケットが確認でき、応答が帰るはずである。

image.png

CentOS6

色々頑張ると今からでもCentOS6でmDNSすることはできる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0