Ubuntu 设置无线 wifi 的静态 IP

文章讲述了如何在Linux系统中安装net-tools来查看网络IP信息,通过`ifconfig`命令获取当前网络接口详情。接着,它指导用户手动设置固定IP地址,包括IP、子网掩码和网关,并提供了两种方法来修改DNS设置,以确保能连接到公网。文章还讨论了自动分配IP地址后DNS设置的重置问题,以及如何通过编辑`/etc/systemd/resolved.conf`文件来持久化DNS设置。
摘要由CSDN通过智能技术生成

安装 net-tools

sudo apt install net-tools

输入ifconfig查看当前网络ip地址:

pulsar@pulsar:~$ ifconfig 
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:02:8c:87:a4  txqueuelen 0  (以太网)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (本地环回)
        RX packets 1458  bytes 113031 (113.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1458  bytes 113031 (113.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp0s20f3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.111  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::c1e9:cb75:e21a:5125  prefixlen 64  scopeid 0x20<link>
        ether d4:54:8b:13:06:28  txqueuelen 1000  (以太网)
        RX packets 53789  bytes 54126215 (54.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 35118  bytes 10016278 (10.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

找到wifi网口的配置,一般来说eth0是有线网口,wlan0是wifi网口,我的电脑wifi网口是wlp0s20f3,如下:

wlp0s20f3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.111  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::c1e9:cb75:e21a:5125  prefixlen 64  scopeid 0x20<link>
        ether d4:54:8b:13:06:28  txqueuelen 1000  (以太网)
        RX packets 53789  bytes 54126215 (54.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 35118  bytes 10016278 (10.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

注意看第二行,其中192.168.1.111是当前的IP地址,255.255.255.0是子网掩码,192.168.1.255是广播地址(不用管)。

打开设置中的Wi-Fi

 点击所连接的wifi右边的设置按钮:

 选择IPv4进入

选择手动,并根据前面ifconfig得到的IP地址输入,前面我得到的是192.168.1.111(需要根据自己得到的ip地址进行修改),然后子网掩码填255.255.255.0,网关填192.1681.1,即

点击关闭网络然后重新打开,这个时候ip地址就固定了。

但是这个时候可能会出现连不上网的情况,或者说能连上局域网,但是连不上公网 (比如www.baidu.com),这时候需要修改DNS,修改方式有两种:

方法一:

将取消DNS自动并填入8.8.8.8

关闭网络然后重新打开即可。

方法二:

终端输入

sudo gedit /etc/resolv.conf 

将nameserver的值改成8.8.8.8,

sudo systemctl restart NetworkManager

关闭网络然后重新打开,即可连接公网。

但是自动分配ip地址后,每次重新开机后,ip地址是不变的,因此每次关机后,此文件中的nameserver会重置,还需要再次修改,才能使用dns服务。解决办法如下:

注意 resolv.conf 文件说明

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

这个文档说 Do not edit,他自动生成的文档。另外在终端输入

man systemd-resolved

注意到

The DNS servers contacted are determined from the global settings in
       /etc/systemd/resolved.conf, the per-link static settings in
       /etc/systemd/network/*.network files (in case systemd-
       networkd.service(8) is used), the per-link dynamic settings received
       over DHCP, user request made via resolvectl(1), and any DNS server
       information made available by other system services. See
       resolved.conf(5) and systemd.network(5) for details about systemd's own
       configuration files for DNS servers. To improve compatibility,
       /etc/resolv.conf is read in order to discover configured system DNS
       servers, but only if it is not a symlink to
       /run/systemd/resolve/stub-resolv.conf, /usr/lib/systemd/resolv.conf or
       /run/systemd/resolve/resolv.conf (see below).

这里说DNS servers是在全局配置文件/etc/systemd/resolved.conf中确定的,因此我们需要区修改/etc/systemd/resolved.conf中的信息。终端输入

sudo gedit /etc/systemd/resolved.conf

修改DNS=8.8.8.8

 

  保存并退出。然后在终端输入

sudo systemctl restart systemd-resolved
sudo systemctl enable systemd-resolved
 
sudo mv /etc/resolv.conf  /etc/resolv.conf.bak
sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

再查看 /etc/resolv.conf 文件就可以看到新的DNS信息已经写入其中了,接下来再使用就不会被重置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值