nmcli Hotspot Gateway
By default, when you create a Wi-Fi hotspot using nmcli
,
the gateway IP is automatically set to match the IP address of the wireless device:
nmcli device wifi hotspot \
ifname wlp2s0 \
con-name sprint \
ssid Sprint \
channel 6 \
band bg \
password 'alaneuler+1'
This setup works out of the box, but sometimes you may need to specify a different gateway IP for your hotspot.
I've asked ChatGPT and Gemini this question, they all suggest to set the ipv4.gateway
for the hotspot,
which has no effect on the actual gateway assignment.
Thus, I decided to go through the NetworkManager source code to find out a way. Under the help of Cursor, I found that the NetworkManager also uses dnsmasq for its DNS and DHCP functionalities:
#define CONFDIR NMCONFDIR "/dnsmasq-shared.d"
static GPtrArray *
create_dm_cmd_line(...)
{
...
/* dnsmasq exits if the conf dir is not present */
if (g_file_test(CONFDIR, G_FILE_TEST_IS_DIR))
nm_strv_ptrarray_add_string_dup(cmd, "--conf-dir=" CONFDIR);
...
}
You can view the source code here.
Concatenating the /dnsmasq-shared.d
with NetworkManager's configuration directory (/etc/NetworkManager
),
I tried to create a configuration file under it, and it worked!
Example file /etc/NetworkManager/dnsmasq-shared.d/gateway.conf
:
dhcp-option=option:router,10.10.0.86
Actually, you can set any dnsmasq option in this file for your hotspot.