Centos7 静态IP设置

时间:2022-01-30 22:02:48


1、编辑 ifcfg-eth0 文件
[root@master ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno16777736

2、修改如下内容
BOOTPROTO="static" #dhcp改为static
ONBOOT="yes" #开机启用本配置
IPADDR=172.16.96.201 #静态IP
GATEWAY=172.16.96.1 #默认网关
NETMASK=255.255.224.0 #子网掩码
DNS1=119.29.29.29 #DNS 配置

3、修改后效果
[root@master ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=3fc7cf00-52d7-4828-80ba-97fc4dd82dae
DEVICE=eno16777736
ONBOOT=yes
IPADDR=172.16.96.201
GATEWAY=172.16.96.1
NETMASK=255.255.224.0
DNS1=119.29.29.29
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
[root@master ~]#

4、重启网络服务
[root@master ~]# service network restart

5、查看改动后的效果(Centos 7 不再使用 ifconfig 而是用 ip 命令查看网络信息)
[root@master ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:51:59:18 brd ff:ff:ff:ff:ff:ff
    inet 172.16.96.201/19 brd 172.16.127.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe51:5918/64 scope link
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
    link/ether 52:54:00:a4:9f:8f brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 500
    link/ether 52:54:00:a4:9f:8f brd ff:ff:ff:ff:ff:ff
[root@master ~]#

[root@master ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.96.201  netmask 255.255.224.0  broadcast 172.16.127.255
        inet6 fe80::20c:29ff:fe51:5918  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:51:59:18  txqueuelen 1000  (Ethernet)
        RX packets 22604  bytes 1980660 (1.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16374  bytes 11501428 (10.9 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 0  (Local Loopback)
        RX packets 21124  bytes 9292349 (8.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 21124  bytes 9292349 (8.8 MiB)
        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:a4:9f:8f  txqueuelen 0  (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

[root@master ~]#