Connect Wifi use static IP — Mi 20 November 2024

你好,世界!

通常我们使用电脑连接网络都是让电脑直接从路由器获得 IP,而路由器分配 IP 的方式也基本上都是动态分配,比如使用 DHCP。不过,有些时候,我们会给电脑分配静态 IP。

Debian Edu 的要求

在安装 Debian Edu GNU/Linux 系统的时候,它的网关要求是

IP: 10.0.0.1
mask: 255.0.0.0
DHCP: disable
SSID: no broadcast

而它的主服务器连接在网关上,需要有两个网卡,都是静态 IP。系统的 /etc/network/interfaces 文件内容如下:

auto eth0 eth1
iface eth0 inet static
        address 10.0.2.2
        gateway 10.0.0.1
        dns-search intern
        dns-nameserver 127.0.0.1

iface eth1 inet static
        address 192.168.0.254

我的电脑有两个网卡,一个有线网卡 eth0,一个无线网卡 wlan0。安装完成之后,当没有用网线连接网关时。我们用 ip address 命令查看会输出:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute
       valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_code1 state DOWN group default qlen 1000
    link/ether 00:f5:f0:40:71:fe brd ff:ff:ff:ff:ff:ff
    altname enp0s1
    inet 10.0.2.2/8 brd 10.255.255.255 scope global eth0
       valid_lft forever preferred_lft forever
3: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether fa:95:7b:1e:af:a7 brd ff:ff:ff:ff:ff:ff

我们看到两个网卡都没有连接网络(state DOWN)。我的目标是让无线网卡 wlan0 的 IP=10.0.2.2,而让有线网卡 eth0 的 IP=192.168.0.254。怎么办?

首先,我们修改文件 /etc/network/interfaces:

auto eth0 wlan0
iface wlan0 inet static
        address 10.0.2.2
        gateway 10.0.0.1
        dns-search intern
        dns-nameserver 127.0.0.1

iface eth0 inet static
        address 192.168.0.254

重启电脑之后,我们再用 ip address 命令查看一下。

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute
       valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_code1 state DOWN group default qlen 1000
    link/ether 00:f5:f0:40:71:fe brd ff:ff:ff:ff:ff:ff
    altname enp0s1
    inet 192.168.0.254/24 brd 192.168.0.255 scope global eth0
       valid_lft forever preferred_lft forever
3: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether fa:95:7b:1e:af:a7 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.2/8 brd 10.255.255.255 scope global wlan0
       valid_lft forever preferred_lft forever

可以看到两个网卡的静态 IP 都正确分配了。

使用静态 IP 连接无线网关

接下来就是把无线网卡和无线网关连接起来。我们使用 wpa_supplicant 来完成这件事。

首先,要新建一个文件 /etc/wpa_supplicant/wpa_supplicant.conf,其内容如下

ctrl_interface=DIR=/run/wpa_supplicant GROUP=netdev
update_config=1

network={
        ssid="myssid"
        scan_ssid=1
        #psk="my_very_secret_passphrase"
        #psk=hash-value-by-wpa_passphrase
}

其次,我们再修改文件 /etc/network/interfaces 让它把无线网卡和我们指定的网关连接起来。

auto eth0 wlan0
iface wlan0 inet static
        address 10.0.2.2
        gateway 10.0.0.1
        dns-search intern
        dns-nameserver 127.0.0.1
        pre-up wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B -f /var/log/wpa_supplicant.log

iface eth0 inet static
        address 192.168.0.254

然后,我们重启电脑,再看一下 ip address

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute
       valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_code1 state DOWN group default qlen 1000
    link/ether 00:f5:f0:40:71:fe brd ff:ff:ff:ff:ff:ff
    altname enp0s1
    inet 192.168.0.254/24 brd 192.168.0.255 scope global eth0
       valid_lft forever preferred_lft forever
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether fa:95:7b:1e:af:a7 brd ff:ff:ff:ff:ff:ff
    altname wlp0s1
    inet 10.0.2.2/8 brd 10.255.255.255 scope global wlan0
       valid_lft forever preferred_lft forever

无线网卡 wlan0 的状态是 UP。试着打开网络:https://www.gnu.org。可以打开。大功告成!

如果你对 GNU 系统的网络设置还有问题,立伯乐 或许可以帮你。

让 GNU 带给你自由上网的感觉!