centos8 中 如何配置靜態IP

小鲨鱼2018發表於2024-04-04

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

相關文章