我正在使用使用网络管理器的 Ubuntu 现代版本,并且我想通过命令行发布和更新我的网络设置。
在 Ubuntu 使用该文件的旧时代interfaces
,我只需执行:
sudo /etc/init.d/networking restart
但现在不再起作用了。
我正在寻找与 Windows 类似的ipconfig /release
功能ipconfig /renew
。
如何从命令行界面发布和更新网络设置?
答案1
要释放和更新 IP 地址,请执行以下操作:
sudo dhclient -r <NIC>
sudo dhclient <NIC>
或者您可以尝试从中获取默认以太网名称的单行代码netstat
(使用 -v 开关显示详细信息):
NIC=$(netstat -r | awk '/default/ {print $NF}'); sudo dhclient -r -v $NIC && sudo dhclient -v $NIC
来自dhclient
手册页:
-r Release the current lease and stop the running DHCP client as
previously recorded in the PID file. When shutdown via this
method dhclient-script will be executed with the specific reason
for calling the script set. The client normally doesn't release
the current lease as this is not required by the DHCP protocol
but some cable ISPs require their clients to notify the server
if they wish to release an assigned IP address.
例子:
terrance@terrance-ubuntu:~$ NIC=$(netstat -r | awk '/default/ {print $NF}'); sudo dhclient -r -v $NIC && sudo dhclient -v $NIC
[sudo] password for terrance:
Internet Systems Consortium DHCP Client 4.4.1
Copyright 2004-2018 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/eth0/40:8d:5c:4f:12:03
Sending on LPF/eth0/40:8d:5c:4f:12:03
Sending on Socket/fallback
DHCPRELEASE of 10.0.0.100 on eth0 to 10.0.0.1 port 67 (xid=0x7eae9da6)
Internet Systems Consortium DHCP Client 4.4.1
Copyright 2004-2018 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/eth0/40:8d:5c:4f:12:03
Sending on LPF/eth0/40:8d:5c:4f:12:03
Sending on Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 3 (xid=0x57fdb020)
DHCPOFFER of 10.0.0.100 from 10.0.0.1
DHCPREQUEST for 10.0.0.100 on eth0 to 255.255.255.255 port 67 (xid=0x20b0fd57)
DHCPACK of 10.0.0.100 from 10.0.0.1 (xid=0x57fdb020)
bound to 10.0.0.100 -- renewal in 41481 seconds.
希望这可以帮助!
答案2
实现此目的的一种方法是告诉网络管理器暂时断开设备连接然后再次连接:
nmcli device disconnect wlan0; nmcli device connect wlan0
(wlan0
用系统上正确的设备名称替换)