筆記:CentOS/RedHat/Debian/Rocky linux 修改主機名稱 hostname

2019110223:30

顯示目前的主機名稱 /其它資訊
$ hostnamectl

$ hostnamectl   
   Static hostname: picaa     這是一台VM
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 153a217486fe4be8a8db123db67ed581
           Boot ID: a8f3c1328e154cf6b169641238054b97
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 4.14.12-x86_64-linode92
      Architecture: x86-64

$ hostnamectl
   Static hostname: box203     這是一台實體主機
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: b18ae35b56a14ff68ff3ce08f2b62394
           Boot ID: ab99ab06bc194a3eb4f0c220c46b1355
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-862.9.1.el7.x86_64
      Architecture: x86-64


修改主機名稱
$ hostnamectl set-hostname 新的名稱 
   例如:
    hostnamectl set-hostname myserver


檢查是否修改成功  (「目前的值」)
$ cat /proc/sys/kernel/hostname 


hostname 資料存放在
/etc/hostname
 ( CentOS 6 以前是 /etc/sysconfig/network )
手動修改這個檔案也可以,但無法馬上生效,得重開機

----

另外, CentOS 也可以用
圖形介面來修改 IP/Hostname 資料:
$ nmtui

nmcli 也可查詢、設定網路介面
 $ nmcli
enp2s0: connected to eth0
        "Broadcom Limited NetXtreme BCM5723 Gigabit Ethernet PCIe (NC107i Integrated PCI Express Gigabit Server
        ethernet (tg3), B4:99:BA:B5:34:B8, hw, mtu 1500
        ip4 default
        inet4 192.168.0.3/24
        route4 192.168.0.0/24
        route4 0.0.0.0/0

lo: unmanaged
        "lo"
        loopback (unknown), 00:00:00:00:00:00, sw, mtu 65536

DNS configuration:
        servers: 8.8.8.8
        interface: enp2s0

Use "nmcli device show" to get complete information about known devices and
"nmcli connection show" to get an overview on active connection profiles.

Consult nmcli(1) and nmcli-examples(5) manual pages for complete usage details.


改 IP

檔案在 /etc/sysconfig/network-scripts/
$ ls -l
-rw-r--r--. 1 root root   358 Apr  6  2021 ifcfg-eth0
-rw-r--r--. 1 root root   359 Sep  1  2020 ifcfg-eth0.20200901
-rw-r--r--. 1 root root   254 May 22  2020 ifcfg-lo
lrwxrwxrwx. 1 root root    24 Nov 29  2021 ifdown -> ../../../usr/sbin/ifdown
-rwxr-xr-x. 1 root root   654 May 22  2020 ifdown-bnep
-rwxr-xr-x. 1 root root  6532 May 22  2020 ifdown-eth

$ cat  ifcfg-eth0
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
UUID=2253034d-d7b7-43df-986e-851f1c67e110
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.0.211
PREFIX=24
GATEWAY=192.168.0.1
DNS1=8.8.8.8
IPV6_PRIVACY=no