内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
本文档详细介绍hostnamectl命令的使用方法,包括查看主机名、修改主机名、设置静态主机名等操作。
风哥提示:
Part01-查看主机名
1.1 查看当前主机名
$ hostnamectl
Static hostname: rhel10
Icon name: computer-vm
Chassis: vm 🖥
Machine ID: 1234567890abcdef1234567890abcdef
Boot ID: abcdef1234567890abcdef1234567890
Virtualization: vmware
Operating System: Red Hat Enterprise Linux 10 (Plow)
CPE OS Name: cpe:/o:redhat:enterprise_linux:10
Kernel: Linux 6.6.0-0.rc0.20241025git3a8c3a5.x86_64
Architecture: x86-64
Hardware Vendor: VMware, Inc.
Hardware Model: VMware Virtual Platform
# 只查看静态主机名
$ hostnamectl –static
rhel10
# 查看瞬态主机名
$ hostnamectl –transient
rhel10
# 查看漂亮主机名
$ hostnamectl –pretty
Red Hat Enterprise Linux 10
# 使用hostname命令查看
$ hostname
rhel10
# 查看主机名和域名
$ hostname -f
rhel10.fgedu.net.cn
# 查看短主机名
$ hostname -s
rhel10
Part02-修改主机名
2.1 设置静态主机名
$ sudo hostnamectl set-hostname webserver01
# 验证主机名已修改
$ hostnamectl –static
webserver01
# 查看完整主机名信息
$ hostnamectl
Static hostname: webserver01
Icon name: computer-vm
Chassis: vm 🖥
Machine ID: 1234567890abcdef1234567890abcdef
Boot ID: abcdef1234567890abcdef1234567890
Virtualization: vmware
Operating System: Red Hat Enterprise Linux 10 (Plow)
CPE OS Name: cpe:/o:redhat:enterprise_linux:10
Kernel: Linux 6.6.0-0.rc0.20241025git3a8c3a5.x86_64
Architecture: x86-64
# 设置带域名的静态主机名
$ sudo hostnamectl set-hostname webserver01.fgedu.net.cn
# 验证主机名
$ hostnamectl –static
webserver01.fgedu.net.cn
$ hostname -f
webserver01.fgedu.net.cn
2.2 设置漂亮主机名
$ sudo hostnamectl set-hostname –pretty “Web Server 01”
# 验证漂亮主机名
$ hostnamectl –pretty
Web Server 01
# 查看所有主机名
$ hostnamectl status
Static hostname: webserver01.fgedu.net.cn
Transient hostname: webserver01.fgedu.net.cn
Icon name: computer-vm
Chassis: vm 🖥
Machine ID: 1234567890abcdef1234567890abcdef
Boot ID: abcdef1234567890abcdef1234567890
Virtualization: vmware
Operating System: Red Hat Enterprise Linux 10 (Plow)
CPE OS Name: cpe:/o:redhat:enterprise_linux:10
Kernel: Linux 6.6.0-0.rc0.20241025git3a8c3a5.x86_64
Architecture: x86-64
Part03-主机名配置文件
3.1 查看和修改配置文件
$ cat /etc/hostname
学习交流加群风哥QQ113257174webserver01.fgedu.net.cn
# 查看hosts文件
$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.100 webserver01 webserver01.fgedu.net.cn
# 编辑hosts文件添加主机名解析
from PG视频:www.itpux.com$ sudo vi /etc/hosts
# 添加以下内容
192.168.1.100 webserver01 webserver01.fgedu.net.cn
192.168.1.101 webserver02 webserver02.fgedu.net.cn
192.168.1.102 webserver03 webserver03.fgedu.net.cn
# 验证hosts文件
$ cat /etc/hosts
127.0.0.1 学习交流加群风哥微信: itpux-com localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomai更多学习教程公众号风哥教程itpux_comn6
192.168.1.100 webserver01 webserver01.fgedu.net.cn
192.168.1.101 webserver02 webserver02.fgedu.net.cn
192.168.1.102 webserver03 webserver03.fgedu.net.cn
# 测试主机名解析
$ ping -c 2 webserver01
PING webserver01 (192.168.1.100) 56(84) bytes of data.
64 bytes from webserver01 (192.168.1.100): icmp_seq=1 ttl=64 time=0.123 ms
64 bytes from webserver01 (192.168.1.100): icmp_seq=2 ttl=64 time=0.124 ms
— webserver01 ping statistics —
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.123/0.123/0.124/0.000 ms
Part04-批量修改主机名
4.1 批量修改多台服务器主机名
# 创建脚本
$ cat > change_hostname.sh << 'EOF' #!/bin/bash # 服务器列表 servers=( "192.168.1.100:webserver01" "192.168.1.101:webserver02" "192.168.1.102:webserver03" ) # 批量修改主机名 for server in "${servers[@]}"; do ip=$(echo $server | cut -d: -f1) hostname=$(echo $server | cut -d: -f2) echo "Changing hostname for $ip to $hostname" ssh root@$ip "hostnamectl set-hostname $hostname" if [ $? -eq 0 ]; then echo "Hostname changed successfully for $ip" else echo "Failed to change hostname for $ip" fi done echo "All hostnames changed" EOF # 赋予执行权限 $ chmod +x change_hostname.sh # 执行脚本 $ ./change_hostname.sh Changing hostname for 192.168.1.100 to webserver01 Hostname changed successfully for 192.168.1.100 Changing hostname for 192.168.1.101 to webserver02 Hostname changed successfully for 192.168.1.101 Changing hostname for 192.168.1.102 to webserver03 Hostname changed successfully for 192.168.1.102 All hostnames changed # 验证主机名修改 $ ssh root@192.168.1.100 "hostname" webserver01 $ ssh root@192.168.1.101 "hostname" webserver02 $ ssh root@192.168.1.102 "hostname" webserver03
Part05-故障排查
5.1 主机名修改问题排查
# 1. 检查主机名状态
$ hostnamectl
Static hostname: webserver01.fgedu.net.cn
Transient hostname: rhel10
# 2. 重新设置主机名
$ sudo hostnamectl set-hostname webserver01.fgedu.net.cn
# 3. 重启系统
$ sudo reboot
# 4. 验证主机名
$ hostnamectl
Static hostname: webserver01.fgedu.net.cn
Transient hostname: webserver01.fgedu.net.cn
# 场景:主机名解析失败
# 1. 检查hosts文件
$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
# 2. 添加主机名解析
$ sudo sh -c ‘echo “192.168.1.100 webserver01 webserver01.fgedu.net.cn” >> /etc/hosts’
# 3. 测试解析
$ ping -c 1 webserver01
PING webserver01 (192.168.1.100) 56(84) bytes of data.
64 bytes from webserver01 (192.168.1.100): icmp_seq=1 ttl=64 time=0.123 ms
— webserver01 ping statistics —
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.123/0.123/0.123/0.000 ms
1. 使用hostnamectl命令修改主机名
2. 修改主机名后更新/etc/hosts文件
3. 主机名建议使用小写字母和数字
4. 避免使用特殊字符和空格
5. 修改主机名后建议重启系统
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
