在 CentOS 中从命令行设置静态IP。
首先找一下需要静态IP的网卡是哪个,比如我这里需要静态的网卡是ens33
1 2 3 4 5 6 7 8 9
| [root@itp-A network-scripts] ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.16.138.129 netmask 255.255.255.0 broadcast 172.16.138.255 inet6 ----::----:----:----:---- prefixlen 64 scopeid 0x20<link> ether 00:00:00:00:00:00 txqueuelen 1000 (Ethernet) RX packets 24406 bytes 34264994 (32.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 10661 bytes 746079 (728.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
切换到这个文件夹
1
| cd /etc/sysconfig/network-scripts/
|
可见该文件下有如下文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| [root@itp-A network-scripts] total 240 drwxr-xr-x. 2 root root 4096 Sep 18 23:45 . drwxr-xr-x. 6 root root 4096 Sep 18 23:46 .. -rw-r--r--. 1 root root 310 Sep 18 23:34 ifcfg-ens33 -rw-r--r--. 1 root root 254 May 22 2020 ifcfg-lo lrwxrwxrwx. 1 root root 24 Sep 18 23:45 ifdown -> ../../../usr/sbin/ifdown -rwxr-xr-x. 1 root root 654 May 22 2020 ifdown-bnep -rwxr-xr-x. 1 root root 6532 May 22 2020 ifdown-eth -rwxr-xr-x. 1 root root 781 May 22 2020 ifdown-ippp -rwxr-xr-x. 1 root root 4540 May 22 2020 ifdown-ipv6 lrwxrwxrwx. 1 root root 11 Sep 18 23:45 ifdown-isdn -> ifdown-ippp -rwxr-xr-x. 1 root root 2130 May 22 2020 ifdown-post -rwxr-xr-x. 1 root root 1068 May 22 2020 ifdown-ppp -rwxr-xr-x. 1 root root 870 May 22 2020 ifdown-routes -rwxr-xr-x. 1 root root 1456 May 22 2020 ifdown-sit -rwxr-xr-x. 1 root root 1621 Dec 9 2018 ifdown-Team -rwxr-xr-x. 1 root root 1556 Dec 9 2018 ifdown-TeamPort -rwxr-xr-x. 1 root root 1462 May 22 2020 ifdown-tunnel lrwxrwxrwx. 1 root root 22 Sep 18 23:45 ifup -> ../../../usr/sbin/ifup -rwxr-xr-x. 1 root root 12415 May 22 2020 ifup-aliases -rwxr-xr-x. 1 root root 910 May 22 2020 ifup-bnep -rwxr-xr-x. 1 root root 13758 May 22 2020 ifup-eth -rwxr-xr-x. 1 root root 12075 May 22 2020 ifup-ippp -rwxr-xr-x. 1 root root 11893 May 22 2020 ifup-ipv6 lrwxrwxrwx. 1 root root 9 Sep 18 23:45 ifup-isdn -> ifup-ippp -rwxr-xr-x. 1 root root 650 May 22 2020 ifup-plip -rwxr-xr-x. 1 root root 1064 May 22 2020 ifup-plusb -rwxr-xr-x. 1 root root 4997 May 22 2020 ifup-post -rwxr-xr-x. 1 root root 4154 May 22 2020 ifup-ppp -rwxr-xr-x. 1 root root 2001 May 22 2020 ifup-routes -rwxr-xr-x. 1 root root 3303 May 22 2020 ifup-sit -rwxr-xr-x. 1 root root 1755 Dec 9 2018 ifup-Team -rwxr-xr-x. 1 root root 1876 Dec 9 2018 ifup-TeamPort -rwxr-xr-x. 1 root root 2780 May 22 2020 ifup-tunnel -rwxr-xr-x. 1 root root 1836 May 22 2020 ifup-wireless -rwxr-xr-x. 1 root root 5419 May 22 2020 init.ipv6-global -rw-r--r--. 1 root root 20678 May 22 2020 network-functions -rw-r--r--. 1 root root 30988 May 22 2020 network-functions-ipv6
|
在其中找到ifcfg-
+你的网卡名的文件,打开该文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| TYPE="Ethernet" PROXY_METHOD="none" BROWSER_ONLY="no" # BOOTPROTO参数指定IP设置方式 # 改为static即为静态IP BOOTPROTO="dhcp" 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="30c7f540-670d-4074-b1db-9789682f4adc" DEVICE="ens33" ONBOOT="yes"
|
同时在文件最下方加入
1 2 3 4
| IPADDR=192.168.10.10 #静态IP GATEWAY=192.168.10.1 #默认网关 NETMASK=255.255.255.0 #子网掩码 DNS1=192.168.10.1 #DNS 配置
|
最后重启网卡即可。
1
| systemctl restart network
|