CentOs设置静态IP

最近做数据库集群 需要配置CentOs的IP地址为静态IP 这里记录一下

查看网卡名称

输入 ip address 查看IP地址

1
2
3
4
5
6
7
8
9
10
11
12
13
[jtb@localhost ~]$ ip address
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: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 1c:1b:0d:00:73:98 brd ff:ff:ff:ff:ff:ff
inet 172.30.16.5/24 brd 172.30.16.255 scope global enp2s0
valid_lft forever preferred_lft forever
inet6 fe80::1e1b:dff:fe00:7398/64 scope link
valid_lft forever preferred_lft forever

可以看到除去环回地址外只有一个 enp2s0
我们编辑这个网卡的配置信息: vi /etc/sysconfig/network-scripts/ifcfg-enp2s0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
TYPE=Ethernet
BOOTPROTO=static #这里改成static
DEFROUTE=yes
IPADDR=172.30.16.5 #新增IP地址
NETMASK=255.255.255.0 #新增子网掩码
GATEWAY=172.30.16.1 #新增网关
NETWORK=172.30.16.0
DNS1=119.29.29.29 #新增DNS地址
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=enp2s0
UUID=b2693e73-4059-4c80-9641-7a8225dc12de
DEVICE=enp2s0
ONBOOT=yes
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_PRIVACY=no

保存配置文件 然后重启网络服务

1
2
[jtb@localhost ~]$ service network restart
Restarting network (via systemctl): [ 确定 ]