centos7配置hostname和ip地址(永久有效)

时间:2024-03-09 07:33:32

一、修改hostname

1、查看当前hostname

[root@localhost~]# hostname
localhost

2、配置新主机名

[root@localhost~]# hostnamectl set-hostname  test88

3、修改/etc/hosts文件并保存

[root@localhost~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 test88
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
~

:wq

二、修改ip地址

1、查看本机网卡信息

[root@test00 ~]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.253  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::dfec:d75c:a23c:244d  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:13:b7:01  txqueuelen 1000  (Ethernet)
        RX packets 184780  bytes 266278597 (253.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30875  bytes 1943437 (1.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 405  bytes 29144 (28.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 405  bytes 29144 (28.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:74:a9:ad  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

2、编辑对应的网卡文件

 [root@test00 ~]#vim /etc/sysconfig/network-scripts/ifcfg-enp0s3

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=7f12b8fc-f9d4-4908-8192-3e0f26a7c718
ONBOOT=yes
IPADDR=192.168.3.253           !!!!!!!!!!!
PREFIX=24
GATEWAY=192.168.3.1
DNS1=8.8.8.8
~

修改对应的ip地址,保存退出

 

3、重启网络服务即可生效

[root@test00 ~]# systemctl restart  network