使用Rust的wifi-rs库连接WiFi时,如何禁用GNOME错误密码弹窗?
禁用GNOME错误WiFi密码连接弹窗的方法
针对你在Pop!_OS 22.04(GNOME 42.5)下使用wifi-rs 0.2.2连接WiFi时,错误密码触发GNOME默认弹窗的问题,可通过以下几种方式解决:
方法1:关闭GNOME网络小程序的通知
GNOME的nm-applet负责弹出网络相关通知,可通过gsettings直接禁用WiFi通知:
gsettings set org.gnome.nm-applet disable-wifi-notifications true
如果需要彻底关闭所有NetworkManager相关通知,执行:
gsettings set org.gnome.nm-applet disable-notifications true
此方法会直接关闭对应通知渠道,后续无论通过代码还是手动操作,相关WiFi连接错误的弹窗都不会再出现。
方法2:使用nmcli命令行替代wifi-rs,禁用通知参数
wifi-rs底层可能封装了NetworkManager的工具,但未暴露禁用通知的选项。直接调用nmcli并添加--notify no参数,可避免触发弹窗:
use std::process::Command; fn connect_wifi(ssid: &str, password: &str) { let output = Command::new("nmcli") .arg("device") .arg("wifi") .arg("connect") .arg(ssid) .arg("password") .arg(password) .arg("--notify") .arg("no") .output() .expect("Failed to run nmcli command"); if output.status.success() { println!("Connection Successful."); } else { let err_output = String::from_utf8_lossy(&output.stderr); match err_output.contains("invalid password") { true => println!("Invalid password."), false => println!("The following error occurred: {}", err_output), } } }
--notify no参数会告知NetworkManager不要发送连接状态的系统通知,从而绕过GNOME的弹窗逻辑。
方法3:修改NetworkManager连接配置(可选)
如果需要长期使用某个WiFi连接,可预先创建一个系统连接配置,并设置不触发通知:
- 先通过nmcli创建连接:
nmcli connection add type wifi con-name "MyWiFi" ifname wlan0 ssid "YourSSID"
- 修改连接的通知设置:
nmcli connection modify "MyWiFi" connection.notify no
之后在代码中连接该预配置的连接,即可避免弹窗。
内容的提问来源于stack exchange,提问作者zap
相关产品推荐
相关产品推荐

