centos8 中 如何配置静态IP
001、 以centos8.4为例
[root@localhost test01]# cat /etc/redhat-release ## 查看系统centos8.4 CentOS Linux release 8.4.2105
002、查看网络接口名称(网卡名称)
[root@localhost test01]# nmcli d ## 查看网络接口名称 DEVICE TYPE STATE CONNECTION ens160 ethernet connected ens160 virbr0 bridge connected (externally) virbr0 lo loopback unmanaged -- virbr0-nic tun unmanaged --
003、进入网卡配置文件所在的目录
[root@localhost test01]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# ls ifcfg-ens160
004、修改网卡配置文件
[root@localhost network-scripts]# vim ifcfg-ens160 PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static ## 此处由dhcp改为static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no NAME=ens160 UUID=024ee419-b660-4150-94f3-67fffb1ae8a2 DEVICE=ens160 ONBOOT=yes ## 改为yes,开机自启 IPADDR=192.168.174.130 ## 设置IP NETMASK=255.255.255.0 ## 设置子网掩码 GATEWAY=192.168.174.2 ## 设置网关 DNS1=119.29.29.29 ## DNS服务器1 DNS2=114.114.114.114 ## DNS服务器2 ~ ~ ~ ~ ~ :wq!
005、重启网络服务
[root@localhost network-scripts]# nmcli connection reload ## 重新加载网卡(centos7中激活网卡命令systemctl restart network不起作用) [root@localhost network-scripts]# nmcli connection up ens160 ## 激活网卡 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
006、查看IP是否生效
[root@localhost network-scripts]# ifconfig | head -n 3 ## 查看当前IP ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.174.130 netmask 255.255.255.0 broadcast 192.168.174.255 inet6 fe80::20c:29ff:fe9b:1211 prefixlen 64 scopeid 0x20<link> [root@localhost network-scripts]# ping -c 3 www.baidu.com ## 测试网络连通性 PING www.a.shifen.com (110.242.68.4) 56(84) bytes of data. 64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=1 ttl=128 time=11.7 ms 64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=2 ttl=128 time=11.4 ms 64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=3 ttl=128 time=12.3 ms --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 11.417/11.792/12.281/0.382 ms
。