CentOS 7设置静态IP地址

与世无争的帅哥 提交于 2019-12-07 04:13:09

本文将介绍如何在CentOS 7系统下设置静态IP地址

在CentOS 7系统中,默认的网卡名是ens33,配置文件是:/etc/sysconfig/network-scripts/ifcfg-ens33


第一步:使用编辑器对该文件进行编辑
vim /etc/sysconfig/network-scripts/ifcfg-eth0

第二步:在该文件中添加如下内容并保存

BOOTPROTO=static          =====此处原为dhcp,改为static
ONBOOT=yes                =====设置开机加载此配置
IPADDR=192.168.199.254    =====设置静态IP地址
NETMASK=255.255.255.0     =====设置网络子网掩码
GATEWAY=192.168.199.1     =====设置网关
DNS1=192.168.199.1         =====设置DNS


设置完成后总体效果如下:

TYPE=Ethernet
BOOTPROTO=static
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=ens33
UUID=cc2a07f2-4dac-4303-a2db-6b468551aae4
DEVICE=ens33
ONBOOT=yes
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPADDR=192.168.199.254
NETMASK=255.255.255.0
GATEWAY=192.168.199.1
DNS=192.168.199.1


第三步:重启网络服务

service network restart


第四步:查看IP修改效果

①使用ifconfig命令

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.199.254  netmask 255.255.255.0  broadcast 192.168.199.255
        inet6 fe80::3b18:474c:2cc6:ebab  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:34:75:ef  txqueuelen 1000  (Ethernet)
        RX packets 1117  bytes 163064 (159.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 641  bytes 130472 (127.4 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 1  (Local Loopback)
        RX packets 8  bytes 704 (704.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 704 (704.0 B)
        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:e7:4b:fc  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

②使用ip addr命令

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:34:75:ef brd ff:ff:ff:ff:ff:ff
    inet 192.168.199.254/24 brd 192.168.199.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::3b18:474c:2cc6:ebab/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:e7:4b:fc 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 1000
    link/ether 52:54:00:e7:4b:fc brd ff:ff:ff:ff:ff:ff

注意:如果BOOTPROTO那里不改成static且网络中有DHCP服务器的情况下,那么该网卡会通过DHCP获取到地址且静态地址同时生效。


以上内容为本人学习的笔记,如有任何错误,欢迎大神指正,在此先行谢过各位大神的帮助。


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!