我面临这样一个场景,直接连接wifi后获得的IP是192.168.1.x结构,但我需要将IP变为另外一个网段比如28.x.x.x。这篇文章就介绍这种方法。
本方法使用到了netplan这个工具,这个工具是需要自行安装的,安装命令如下:
sudo apt-get install netplan.io
安装netplan后,需要进行文件配置,到/etc/netplan/下写入一个yaml文件,文件命名为50-XXXX-XXX.yaml(名字其实应该可以自己随便改,但我一直用这个),内容如下:
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
eth0:
dhcp4: true
optional: true
wifis:
wlan0:
dhcp4: no
addresses: [目标IP/24]
optional: true
gateway4: 28.11.84.1
nameservers:
addresses: [114.114.114.114,8.8.8.8]
access-points:
wifi的名称:
password: "密码"
version: 2
注意:这个时候一定要保证已经通过wifi连接到目标网络了
设置好了以后,执行如下两条命令:
sudo netplan generate
sudo netplan apply
然后就发现ip已经自动更改了~
但是在使用的时候发现一个事情,重启一下之后就没办法连接wifi了。这个时候必须要把之前那个yaml文件删除掉,再重启一下,为了方便,我写了两个shell文件,留在这里供参考:
delete.sh
sudo rm /etc/netplan/50-XXXX-XXX.yaml
sudo netplan generate
sudo netplan apply
reboot