背景
以前裝了虛擬機后,一開始設定的是動態IP, dhcp 的,好長時間沒用了;
最近重新開始使用虛擬機了,一開始IP是 192.168.169.128 ,然后就三天兩頭的變化,每次變化都增加1,這過了大概一星期,IP已經變到了 192.168.169.131 ,就想著看怎么把這個IP固定下來,
環境
[root@hadoop1 local]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@hadoop1 local]# uname -a
Linux hadoop1 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
動態IP
剛安裝好虛擬機,CentOS7作業系統,通過 ip a 或者 ifconfig 未查看到IP地址;
# 配置動態IP
[root@hadoop1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
bootproto=dhcp
onboot=yes
# 重啟
[root@hadoop1 ~]# reboot

這是一開始配置的動態IP,然后就出現了在幾天時間內,IP地址莫名其妙地發生變化,接著便想辦法把IP固定下來,
靜態IP
# 配置靜態IP
[root@hadoop1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO=static
IPADDR=192.168.169.130
# 重啟
[root@hadoop1 ~]# reboot

Note:根據作業系統版本的不同,在 /etc/sysconfig/network-scripts/ 目錄下不一定有 ifcfg-ens33 這個檔案,但應該有一個類似檔案名的組態檔,
驗證:通過 ip a 或者 ifconfig 可以看到配置的IP地址,并且之后IP地址固定不變,

域名ping不通了
- 問題
采用上述方式配置了靜態IP后,發現NTP服務無法正常同步,接著ping了下百度,
[root@hadoop1 ~]# ntpdate ntp1.aliyun.com
Error resolving ntp1.aliyun.com: Name or service not known (-2)
23 Jul 12:38:40 ntpdate[118769]: Can't find host ntp1.aliyun.com: Name or service not known (-2)
23 Jul 12:38:40 ntpdate[118769]: no servers can be used, exiting
[root@hadoop1 ~]# ping www.baidu.com
ping: www.baidu.com: 未知的名稱或服務
- 解決方法
# 配置靜態IP
[root@hadoop1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
DNS1=192.168.169.2
GATEWAY=192.168.169.2
# 重啟網路:
[root@hadoop1 ~]# service network restart
Restarting network (via systemctl): [ OK ]
Note:GATEWAY與DNS1必須都配置了,否則還會報錯:
# 不配GATEWAY
[root@hadoop1 ~]# ping www.baidu.com
connect: 網路不可達
# 不配DNS1
[root@hadoop1 ~]# ping www.baidu.com
ping: www.baidu.com: 未知的名稱或服務
總結
最終的配置如下,以下配置解決了我的虛擬機IP三天兩頭變化的問題;
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
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=6663e0ef-a2c8-48c7-b982-0569ba0bad8f
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.169.130
DNS1=192.168.169.2
GATEWAY=192.168.169.2

If you have any questions or any bugs are found, please feel free to contact me.
Your comments and suggestions are welcome!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/301461.html
標籤:其他
