實(shí)驗(yàn)環(huán)境:WSL的ubuntu18.04 LTS
ip命令方式
# 查看所有網(wǎng)卡及配置
ip a
# 刪除指定網(wǎng)卡eth0的當(dāng)前ip配置(ifconfig只顯示第一條,不刪除直接添加會(huì)導(dǎo)致顯示不完整)
ip addr del 192.168.23.1/24 broadcast 192.168.23.255 dev eth0
# 在指定網(wǎng)卡添加ip
ip addr add 192.168.23.13/24 broadcast 192.168.23.255 dev eth0
# 只添加ip,子網(wǎng)掩碼默認(rèn)為32,廣播地址不填充(結(jié)尾類型:scope global eth0)
ip addr add 192.168.23.13 dev eth0
# 只添加ip/24,但不會(huì)自動(dòng)填充(結(jié)尾類型:scope global secondary eth0)
ip addr add 192.168.23.13/24 dev eth0
# 查看路由(包括default默認(rèn)路由,如果有設(shè)置的話)
ip route
# 刪除默認(rèn)路由
ip route delete default
# 設(shè)置默認(rèn)路由的網(wǎng)關(guān)
ip route add default via 192.168.23.254 dev eth0
# 上一句有時(shí)會(huì)報(bào)錯(cuò),則添加onlink
ip route add default via 192.168.23.254 dev eth0 onlink
ifconfig命令
# 刪除并新建網(wǎng)絡(luò)配置(遠(yuǎn)程連接可能會(huì)中斷)
# 實(shí)際過程:先檢查第一條ip配置與參數(shù)是否一致,一致則不做操作。不一致則刪除第一條,在ip列表結(jié)尾添加新一條(最后一條ip與參數(shù)相同時(shí)會(huì)提示已存在,但只是子網(wǎng)掩碼不同時(shí)有異?,F(xiàn)象)(有scope global eth0和scope global secondary eth0兩個(gè)列表。),但顯示的只是第一條ip,已使用ip命令測(cè)試驗(yàn)證)
ifconfig eth0 192.168.23.130 netmask 255.255.255.0 broadcast 192.168.23.255
# 簡(jiǎn)寫:刪除并新建網(wǎng)絡(luò)配置(默認(rèn)值填充后同上)
ifconfig eth0 192.168.23.130
# 查看指定網(wǎng)卡配置
ifconfig eth0
原本順序?yàn)?31,132,133。執(zhí)行ifconfig eth0 192.168.23.133 netmask 255.255.0.0 broadcast 192.168.23.255
后,提示“文件已存在”,因子網(wǎng)掩碼與最后一條不同產(chǎn)生錯(cuò)續(xù)與錯(cuò)改。
修改DNS
測(cè)試環(huán)境:Ubuntu18.04文章來源:http://www.zghlxwxcb.cn/news/detail-562691.html
# 查看systemd-resolved服務(wù)中的dns配置
systemd-resolve --status | grep 'DNS Servers' -A2
# 編輯配置文件(在文件中添加:DNS=114.114.114.114 223.5.5.5)
vim /etc/systemd/resolved.conf
# 鎖定dns配置文件
chattr +i /etc/systemd/resolved.conf
# 重啟systemd-resolved服務(wù)
systemctl restart systemd-resolved
相關(guān)異常
- Ubuntu18.04下
ip
命令報(bào)錯(cuò):RTNETLINK answers: no such process,且執(zhí)行:systemctl status networking
后正常(開機(jī)腳本里加沒用)
原因及解決:在/etc/network/interfaces
文件中配置對(duì)應(yīng)的網(wǎng)卡
相關(guān)參考:
Ubuntu18配置dns
Error:Nexthop has invalid gateway文章來源地址http://www.zghlxwxcb.cn/news/detail-562691.html
到了這里,關(guān)于Linux 修改網(wǎng)卡ip的兩種方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!