CentOS7 设置静态 IP

测试环境:

1
2
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)

查找网卡信息

找到要修改的网卡,比如我这里的是 eno16777736

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.204.128 netmask 255.255.255.0 broadcast 192.168.204.255
inet6 fe80::20c:29ff:fe43:2fd1 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:43:2f:d1 txqueuelen 1000 (Ethernet)
RX packets 93 bytes 10339 (10.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 102 bytes 12391 (12.1 KiB)
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 15 bytes 1329 (1.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 15 bytes 1329 (1.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

修改配置

找到 eno16777736 这个网卡对应的配置文件,编辑

1
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno16777736

文件默认配置是这样的(后面的注释是我加上去的):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
HWADDR=00:0C:29:43:2F:D1
TYPE=Ethernet
BOOTPROTO=dhcp ## IP的分配模式,这里是DHCP
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=0c9115ee-a2b3-47bc-b2b9-efecb596e108
ONBOOT=yes ## 是否默认开机启动

现在要做的就是更改为如下配置,后面的几个项根据自己实际情况配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
HWADDR=00:0C:29:43:2F:D1
TYPE=Ethernet
## BOOTPROTO=dhcp ## 把DHCP注释掉
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=0c9115ee-a2b3-47bc-b2b9-efecb596e108
ONBOOT=yes ## 是否开机启动

## Broly: static
BOOTPROTO=static ## DHCP改为static
IPADDR=192.168.204.136 ## 静态IP地址
GATEWAY=192.168.204.1 ## 网关
NETMASK=255.255.255.0 ## 子网掩码
##DNS1=192.168.204.1 ## DNS配置,看着配

重启网络服务

别忘了重启网络服务

1
[root@localhost ~]# systemctl restart network.service