Ubuntu 18.04 使用 Netplan 來配置 IP 地址,Netplan 是一個新的命令行網(wǎng)絡(luò)配置工具。
新的 IP 配置方法不會再用到 /etc/network/interfaces 這個文件,取而代之的是一個 YAML 文件。
默認(rèn)的 Netplan 配置文件一般在 /etc/netplan 目錄下。
首先先找到 Netplan 默認(rèn)的網(wǎng)絡(luò)配置文件所在之處:
$ ls /etc/netplan/
00-installer-config.yaml
我們可以看到,默認(rèn)的網(wǎng)絡(luò)配置文件是 00-installer-config.yaml ,這是一個 YAML 文件。
然后我們再看一下這個文件的內(nèi)容是什么:
$ cat /etc/netplan/00-installer-config.yaml
network:
? ? ethernets:
? ? ? ? ens33:
? ? ? ? ? ? dhcp4: yes
? ? ? ? ? ? addresses: [] ?? ??
? ? version: 2
網(wǎng)卡默認(rèn)配置為從 DHCP 服務(wù)器中獲取 IP,如果是雙網(wǎng)卡,可能還會有一路配置,比如像下面這種:
network:
? ? ethernets:
? ? ? ? ens33:
? ? ? ? ? ? dhcp4: yes
? ? ? ? ? ? addresses: []?
?? ? ?ensxx:
? ? ? ? ? ? dhcp4: yes
? ? ? ? ? ? addresses: []
version: 2
現(xiàn)在給網(wǎng)卡配置為靜態(tài) IP 地址,打開配置文件。
$ sudo vim /etc/netplan/00-installer-config.yaml
接下來我們分別添加 IP 地址、子網(wǎng)掩碼、網(wǎng)關(guān)、DNS 服務(wù)器等配置。
分別用 192.168.0.4 作為網(wǎng)卡 enp33 的 IP 地址,
192.168.0.1 作為網(wǎng)關(guān)地址,
255.255.255.0 作為子網(wǎng)掩碼。
然后用 8.8.8.8 、 8.8.4.4 這兩個 DNS 服務(wù)器 IP。
# This is the network config written by 'subiquity'
network:
? ? ? ? ethernets:
? ? ? ? ? ? ? ? ens33:
? ? ? ? ? ? ? ? ? ? ? ? addresses: [192.168.0.4/24]
? ? ? ? ? ? ? ? ? ? ? ? #addresses: []
? ? ? ? ? ? ? ? ? ? ? ? gateway4: 192.168.0.1
? ? ? ? ? ? ? ? ? ? ? ? dhcp4: no
? ? ? ? ? ? ? ? ? ? ? ? #dhcp4: true
? ? ? ? ? ? ? ? ? ? ? ? nameservers:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?addresses: [8.8.8.8]
? ? ? ? ? ? ? ? ? ? ? ? optional: true
? ? ? ? renderer: networkd
? ? ? ? version: 2
要注意的一點(diǎn)是,在 Ubuntu 18.04 里,這個配置文件的每一行都必須靠空格來縮進(jìn),不能用 TAB 來代替,否則配置會不起作用。
同時,在 Ubuntu 18.04 中,我們定義子網(wǎng)掩碼的時候不是像舊版本的那樣把 IP 和子網(wǎng)掩碼分成兩項(xiàng)配置。
在舊版本的 Ubuntu 里,我們一般配置的 IP 和子網(wǎng)掩碼是這樣的:
address = 192.168.225.50
netmask = 255.255.255.0
而在 netplan 中,我們把這兩項(xiàng)合并成一項(xiàng),就像這樣:
addresses : [192.168.0.4/24]
配置完成之后保存并關(guān)閉配置文件。然后用下面這行命令來應(yīng)用剛才的配置:
$ sudo netplan apply
如果在應(yīng)用配置的時候有出現(xiàn)問題的話,可以通過如下的命令來查看剛才配置的內(nèi)容出了什么問題。
$ ip addr
在我的 Ubuntu 18.04 中配置完之后執(zhí)行命令輸出的信息如下:
到此為止,我們已經(jīng)成功地在 Ubuntu 18.04 LTS 中用 Netplan 完成了靜態(tài) IP 的配置。
更多關(guān)于 Netplan 的信息,可以在用 man 命令在手冊中查看:
$ man netplan
在 Ubuntu 18.04 LTS 中配置動態(tài) IP 地址
其實(shí)配置文件中的初始配置就是動態(tài) IP 的配置,所以你想要使用動態(tài) IP 的話不需要再去做任何的配置操作。如果你已經(jīng)配置了靜態(tài) IP 地址,想要恢復(fù)之前動態(tài) IP 的配置,就把在上面靜態(tài) IP 配置中所添加的相關(guān)配置項(xiàng)刪除,把整個配置文件恢復(fù)成上面的圖 1 所示的樣子就行了。
靈活的在不同IP之間切換
如果你用的電腦是公司配置的或者電腦總是在公司和家里兩邊帶的時候,而公司和家里的IP網(wǎng)段不一樣的話,就有可能會經(jīng)常要切換IP,
通常用ifconfig命令來修改IP地址,
[root@ubuntu_servers]# ifconfig eth0 192.168.2.10?
[root@ubuntu_servers]# ifconfig eth0 192.168.2.10 netmask 255.255.255.0?
[root@ubuntu_servers]# ifconfig eth0 192.168.2.10 netmask 255.255.255.0 broadcast 192.168.2.255?
但ifconfig只是臨時改一下,重新開機(jī)又會恢復(fù)原來的,像國慶長假在家,每次開機(jī)都要輸入指令也很麻煩。
如果每次修改靜態(tài)IP,則要打開并修改/etc/netplan/00-installer-config.yaml文件,然后 netplan apply一下,也很麻煩,
程序員都是很懶的,能少敲個命令絕不會多敲,有沒有一種辦法能簡化一下了?
當(dāng)然是有的,這里提供一個思路:寫一個shell腳本,每次要修改IP時,就執(zhí)行一下腳本就行了:
#!/bin/bash
ip=1
#:<< block
while [ 1 ]
do
echo -n "input ip field[0 3]:"
?? ?read -p "0 or 3 ? " ip
?? ?case $ip in
?? ?0) break ;;
?? ?3) break ;;
?? ?*) echo "input error, input 1 or 3."
?? ?esac
done
#block
#方法二
:<< block?
echo ?"input ip field[0 3]:"
ip1=0
ip2=3
select ip in $ip1 $ip2
do?
?? ?if [ $ip -eq $ip1 ] ; then
?? ??? ?break
?? ?fi
?? ?
?? ?if [ $ip -eq $ip2 ] ; then
?? ??? ?break
?? ?fi
?? ?echo -n "input 0 or 3 :"
?? ?#continue
?? ?
done
block
echo "set ip=192.168.$ip.4"
if [ $ip -eq 0 ?]
then
?? ?if [ ?-e 00-installer-config.yaml.0.x ]
?? ?then
?? ??? ?cp 00-installer-config.yaml.0.x ?00-installer-config.yaml
?? ??? ?netplan apply
?? ??? ?sleep 2s
?? ??? ?ip addr
?? ?else
?? ??? ?echo "ERR: 00-installer-config.yaml.0.x is not exist!!"
?? ?fi
fi
if [ $ip -eq 3 ?]
then
?? ?if [ ?-e 00-installer-config.yaml.3.x ]
?? ?then
?? ??? ?cp 00-installer-config.yaml.3.x ?00-installer-config.yaml
?? ??? ?netplan apply
?? ??? ?sleep 2s
?? ??? ?ip addr
?? ?else
?? ??? ?echo "ERR: 00-installer-config.yaml.3.x is not exist!!"
?? ?fi
fi
####################################################################
將00-installer-config.yaml 分別復(fù)制成 ?00-installer-config.yaml.0.x 和 00-installer-config.yaml.3.x ,并修改內(nèi)部IP地址,如下:
這樣只要輸入命令:
./net.sh ? ? #在/etc/netplan/
或者
./etc/netplan/net.sh?
?IP段輸入0,回車:
文章來源:http://www.zghlxwxcb.cn/news/detail-422920.html
程序會自動選擇目標(biāo)yaml文件,并修改當(dāng)前yaml文件,并打印修改后激活的IP。
?文章來源地址http://www.zghlxwxcb.cn/news/detail-422920.html
到了這里,關(guān)于在 Ubuntu 18.04 中配置靜態(tài) IP 地址及靈活切換IP的辦法的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!