(兩種獲取IP方式:DHCP自動獲取、靜態(tài)配置)
Debian
1 . 打開文件
vim /etc/network/interfaces
2 . 修改
dhcp自動獲取
修改為:
auto eth0 # eth0指的是自己機(jī)器上ifconfig 輸出對的網(wǎng)卡別名相符
iface eth0 inet dhcp # 表示獲取ip方式為dhcp
配置靜態(tài)IP
3 . 重啟網(wǎng)絡(luò)服務(wù)
/etc/init.d/networking restart
ContOS
1 . 打開
vi /etc/sysconfig/network-scripts/ifcfg-eth0(eth0為網(wǎng)卡設(shè)備別名)
2 . 修改
DHCP方式獲取
ONBOOT = yes 指系統(tǒng)啟動時激活此設(shè)備 BOOTPROTO=dhcp 指獲取ip方式為dhcp
靜態(tài)配置
修改
BOOTPROTO=static
換行添加
IPADDR=172.16.110.126
NETMASK=255.255.255.0
GATEWAY=172.16.110.1
DNS1=172.16.8.8
DNS2=8.8.8.8
3 . 重啟網(wǎng)絡(luò)服務(wù)
service network restart
Ubuntu
版本:ubuntu 12.04
1 . 打開
vim etc/network/interfaces
2 . 添加網(wǎng)卡配置(靜態(tài)獲取IP和DHCP獲取IP選一個配置即可)
靜態(tài)獲取IP
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.31.99
netmask 255.255.255.0
gateway 192.168.31.1
dns-nameservers 211.136.20.203
DHCP獲取IP
auto eth0
iface eth0 inet dhcp
3 . 重啟網(wǎng)絡(luò)設(shè)備
sudo /etc/init.d/networking restart
版本:ubuntu 14.04
(設(shè)置與12.04相同壕鹉,但還需要再修改一個文件)
1 . 修改managed參數(shù)
/etc/NetworkManager/NetworkManager.conf # 將參數(shù) managed 設(shè)置為 true
2 . 按照12.04 修改配置
版本:ubuntu 16.04
1 . 打開
vim /etc/network/inrerfaces
2 . 增加
靜態(tài)獲取IP
auto ens33 # ens33 為網(wǎng)卡別名
iface ens33 inet static # 設(shè)置為靜態(tài)配置
address 172.20.10.134 # 設(shè)置IP地址
netmask 255.255.255.0 # 設(shè)置子網(wǎng)掩碼
gateway 172.20.10.2 # 設(shè)置網(wǎng)關(guān)
提示(ubuntu 16 之后的網(wǎng)卡名字不再是eth0之類命名剃幌,根據(jù)本機(jī)器ifconfig查看到的為準(zhǔn))
DHCP獲取IP
auto ens33
iface ens33 inet dhcp
3 . 修改managed 參數(shù)
vim /etc/NetworkManager/NetworkManager.conf # 打開將參數(shù) managed 設(shè)置為 true
4 . 設(shè)置DNS
vim /etc/resolvconf/resolv.conf.d/base # 打開文件增加
-------------------------------------------------------------
nameserver 114.114.114.114
nameserver 8.8.8.8
驗(yàn)證
resolvconf -u
提示( 因?yàn)闄C(jī)器在重啟后,真正讀取dns 的配置是在 /etc/resolv.conf晾浴,但是 /etc/resolv.conf 的數(shù)據(jù)來源于 /etc/resolvconf/resolv.conf.d/base)
5 . 重啟網(wǎng)絡(luò)服務(wù)
/etc/init.d/networking restart
版本:ubunut 18.04
1 . 打開
/etc/netplan/01-network-manager-all.yaml
2 . 修改為
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
ethernets:
ens33: #配置的網(wǎng)卡名稱
dhcp4: no #dhcp4關(guān)閉
dhcp6: no #dhcp6關(guān)閉
addresses: [172.20.10.155/24] #設(shè)置本機(jī)IP及掩碼
gateway4: 192.20.10.2 #設(shè)置網(wǎng)關(guān)
nameservers:
addresses: [114.114.114.114,8.8.8.8] #設(shè)置DNS
3 . 執(zhí)行命令负乡,使配置文件生效
netplan apply