配置主機名和名稱解析
Linux 主機名稱解析過程
1.本機檔 /etc/hosts [設定]
172.25.250.9 workstation.lab.example.com workstation
172.25.250.10 servera.lab.example.com servera
172.25.250.11 serverb.lab.example.com serverb
172.25.250.12 serverc.lab.example.com serverc
172.25.250.13 serverd.lab.example.com serverd
172.25.250.14 servere.lab.example.com servere
ex.當解析本機名稱時不需要靠外網電腦,
2. Resoluer[解析器]
cat /etc/resolv.conf
ex.如本名稱無法解析會透過[名稱伺服器DNS]到外網尋找名稱解析
[root@worstation ~]# hostname
worstation.lab.example.com
[root@worstation ~]# hostnamectl set-hostname host.example.com
[root@worstation ~]# hostnamectl status
Static hostname: host.example.com
Pretty hostname: host.example.com
Icon name: computer-vm
Chassis: vm
Machine ID: 4a819b99cae847e5880de743e6b4a2f1
Boot ID: 3dde68d03d394159a96dd2d283b09a41
Virtualization: oracle
Operating System: CentOS Stream 8
CPE OS Name: cpe:/o:centos:centos:8
Kernel: Linux 4.18.0-408.el8.x86_64
Architecture: x86-64
[root@worstation ~]# eit
bash: eit: command not found...
[root@worstation ~]# exit
logout
[student@worstation ~]$ sudo su -
[root@hostexample ~]# cat /etc/hostname
hostexample.com
改回 workstation.lab.example.com
[root@hostexample ~]# hostnamectl set-hostname workstation.lab.example.com
[root@hostexample ~]# exit
logout
[student@worstation ~]$ sudo su -
[root@workstation ~]# hostname
workstation.lab.example.com
[root@workstation ~]#
Network Manager TUI
考試可用[設定如下]
[root@workstation ~]# nmtui
當使用 host 會使用外部的名稱伺服器解析
getent hosts xxx [先找內部/etc/hosts 登記解析 ,找不到再找host 外部解析]
[student@workstation ~]$ ssh student@servera
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Sun Oct 23 18:43:42 2022 from 172.25.250.9
[student@servera ~]$ hostname
servera.lab.example.com
[student@servera ~]$ hostnamectl status
Static hostname: servera.lab.example.com
Icon name: computer-vm
Chassis: vm
Machine ID: 4a819b99cae847e5880de743e6b4a2f1
Boot ID: c239c9652ebe474bb72f6fd7efe80add
Virtualization: oracle
Operating System: CentOS Stream 8
CPE OS Name: cpe:/o:centos:centos:8
Kernel: Linux 4.18.0-408.el8.x86_64
Architecture: x86-64
[student@servera ~]$ sudo hostnamectl set-hostname servera.lab.example.com
[sudo] password for student:
[student@servera ~]$ cat /etc/hostname
servera.lab.example.com
[student@servera ~]$ hostnamectl status
Static hostname: servera.lab.example.com
Icon name: computer-vm
Chassis: vm
Machine ID: 4a819b99cae847e5880de743e6b4a2f1
Boot ID: c239c9652ebe474bb72f6fd7efe80add
Virtualization: oracle
Operating System: CentOS Stream 8
CPE OS Name: cpe:/o:centos:centos:8
Kernel: Linux 4.18.0-408.el8.x86_64
Architecture: x86-64
[student@servera ~]$ sudo hostname
hostname hostnamectl
[student@servera ~]$ sudo hostname testname
[student@servera ~]$ hosetname
bash: hosetname: command not found...
[student@servera ~]$ hostname
testname
[student@servera ~]$ cat /etc/resolv.conf
# Generated by NetworkManager
search lab.example.com
nameserver 1.1.1.1
[student@servera ~]$ cat /etc/hostname
servera.lab.example.com
[student@servera ~]$ sudo systemctl reboot
Connection to servera closed by remote host.
Connection to servera closed.
[student@workstation ~]$ ssh student@servera
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Sun Oct 23 18:54:15 2022 from 172.25.250.9
[student@servera ~]$ hostname
servera.lab.example.com
[student@servera ~]$ host serverb.lab.example.com
Host serverb.lab.example.com not found: 3(NXDOMAIN)
[student@servera ~]$ getent hosts serverb
172.25.250.11 serverb.lab.example.com serverb
[student@servera ~]$ ping -c3 serverb
PING serverb.lab.example.com (172.25.250.11) 56(84) bytes of data.
64 bytes from serverb.lab.example.com (172.25.250.11): icmp_seq=1 ttl=64 time=0.441 ms
64 bytes from serverb.lab.example.com (172.25.250.11): icmp_seq=2 ttl=64 time=0.356 ms
64 bytes from serverb.lab.example.com (172.25.250.11): icmp_seq=3 ttl=64 time=0.342 ms
--- serverb.lab.example.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2037ms
rtt min/avg/max/mdev = 0.342/0.379/0.441/0.049 ms
[student@servera ~]$ exit
logout
Connection to servera closed.