在 Ubuntu 上通过 DHCP 获取 IP 地址,通常不是“设置 IP”,而是让系统自动从 DHCP 服务器获取 IP。下面按不同 Ubuntu 版本和场景说明。
ip addr
例如:eth0、ens33
配置文件一般在:
/etc/netplan/*.yaml
示例(DHCP 自动获取 IP):
network:
version: 2
ethernets:
ens33:
dhcp4: true
sudo netplan apply
ip addr
或
hostname -I
编辑:
sudo nano /etc/network/interfaces
示例:
auto eth0
iface eth0 inet dhcp
重启网络:
sudo systemctl restart networking
sudo dhclient eth0
释放 IP:
sudo dhclient -r eth0
那需要在 DHCP 服务端(如 isc-dhcp-server)配置:
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.100 192.168.1.200;
}
host ubuntu-pc {
hardware ethernet 00:11:22:33:44:55;
fixed-address 192.168.1.150;
}
journalctl -u systemd-networkd
如果你告诉我:
我可以给你更精确的配置示例。