Skip to content

Instantly share code, notes, and snippets.

@narate
Last active April 15, 2024 14:22
Show Gist options
  • Save narate/d3f001c97e1c981a59f94cd76f041140 to your computer and use it in GitHub Desktop.
Save narate/d3f001c97e1c981a59f94cd76f041140 to your computer and use it in GitHub Desktop.
Create Wi-Fi Hotspot on Linux using nmcli

Create a Wi-Fi hotspot on Linux using nmcli

Original post : https://unix.stackexchange.com/a/310699

nmcli con add type wifi ifname wlan0 con-name Hostspot autoconnect yes ssid Hostspot
nmcli con modify Hostspot 802-11-wireless.mode ap 802-11-wireless.band bg ipv4.method shared
nmcli con modify Hostspot wifi-sec.key-mgmt wpa-psk
nmcli con modify Hostspot wifi-sec.psk "veryveryhardpassword1234"
nmcli con up Hostspot

Note

If after reboot nmcli con up Hotspot doesn't work

Use this command instead to start Hotspot

UUID=$(grep uuid /etc/NetworkManager/system-connections/Hotspot | cut -d= -f2)
nmcli con up uuid $UUID
@sevillaarvin
Copy link

Found this: https://unix.stackexchange.com/questions/234552/create-wireless-access-point-and-share-internet-connection-with-nmcli

nmcli dev wifi hotspot ifname wlp4s0 ssid test password "test1234"

@AugustoCiuffoletti
Copy link

I'm configuring a Raspberry PI with a UMTS dongle as a standalone AP for an IoT application using the last release of Raspian (Buster light) updated. After successfully configuring the dongle (ping www.example.com successful), I tried configuring the AP, but your script fails at the very last step:

augusto@raspberrypi:~ $ sudo nmcli con up  Hotspot
[sudo] password for augusto: 
Error: Connection activation failed: No suitable device found for this connection (device eth0 not available because profile is not compatible with device (mismatching interface name)).

Maybe the following helps (with fake ssid and psk):

augusto@raspberrypi:~ $ nmcli connection show
NAME                UUID                                  TYPE      DEVICE 
Wired connection 1  2514808d-1a55-3af8-8bbf-572d9619769a  ethernet  eth0   
Hotspot            9db27d3b-a22b-4bd9-9e32-fc256963e5d2  wifi      --     
augusto@raspberrypi:~ $ sudo cat /etc/NetworkManager/system-connections/Hotspot.nmconnection
[connection]
id=Hotspot
uuid=9db27d3b-a22b-4bd9-9e32-fc256963e5d2
type=wifi
interface-name=wlan0
permissions=

[wifi]
band=bg
mac-address-blacklist=
mode=ap
ssid=myap

[wifi-security]
key-mgmt=wpa-psk
psk=1234

[ipv4]
dns-search=
method=shared

[ipv6]
addr-gen-mode=stable-privacy
dns-search=
method=auto
augusto@raspberrypi:~ $ ifconfig wlan0
wlan0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 6a:99:14:b9:f7:df  txqueuelen 1000  (Ethernet)
        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


Any help? Thank you!

@AugustoCiuffoletti
Copy link

AugustoCiuffoletti commented Sep 5, 2019

I fixed my problem: network-manager was interfering with the native network management. The fix consisted of purging the openresolv and dhcpdc5 packages:

#  apt purge openresolv dhcpcd5

which after all looks quite obvious :-)
All is fine now with Narate solution, while sevillaarvin's one gives incomplete results.
I have posted a complete solution for the RPi as a mobile hotspot as a gist, giving credits to your one. Thanks!

@smahi
Copy link

smahi commented Dec 3, 2019

thank you so much

@rayanprasanna
Copy link

Thanks a lot.

@lwlagrange
Copy link

much simpler alternative to hostapd and dnsmasq access point. bravo!

@dakataca
Copy link

dakataca commented Feb 8, 2021

In archlinux to share the wired internet via wifi, the dnsmasq packet is required for the network to lift properly.

It is also possible to create the network in a single command.

$ IFNAME="wlan0" && CON_NAME="MATARIFE" && PASSWD="password" && nmcli c add type wifi ifname $IFNAME con-name $CON_NAME autoconnect yes ssid $CON_NAME 802-11-wireless.mode ap 802-11-wireless.band bg ipv4.method shared wifi-sec.key-mgmt wpa-psk wifi-sec.psk "$PASSWD"

And then just lift the network.

$ nmcli con up $CON_NAME

We check that the wired network is sharing the internet via wifi.

$ nmcli d s | grep ^$IFNAME
wlan0          wifi      connected     MATARIFE

I also have a script that automates the procedure. For more information I also have a post on my Telegram channel dedicated to the topic.

@anapeksha
Copy link

How to use the proxy section in the .nmconnection file?
I mean can I use the Tor Socks proxy in there to wrap my hotspot?

@AugustoCiuffoletti
Copy link

@anapeksha: Sorry, I have no experience on that.

@J-Rojas
Copy link

J-Rojas commented Mar 20, 2021

Thanks! This method solved this error for me (leaving this here for those searching for a solution).

Error: Connection activation failed: IP configuration could not be reserved (no available address, timeout, etc.)

@RickyCurci
Copy link

RickyCurci commented Aug 19, 2021

I have a problem:
~ sudo nmcli con up Hotspot
Error: Connection activation failed: No suitable device found for this connection (device eth0 not available because profile is not compatible with device (mismatching interface name)).

(i'm using voidlinux) can you help me please ?

@flintt
Copy link

flintt commented Oct 18, 2021

in the nmcli cmd code block, Hostspot should change to Hotspot, otherwise the UUID part will get no result.
and some system will add .nmconnection , so it better to using this to get UUID (as long there is no other connection name start with Hotspot)
UUID=$(grep uuid /etc/NetworkManager/system-connections/Hotspot* | cut -d= -f2)
and for the last part, dnsmasq binary is needed to turn on the hotspot

@groeneveld
Copy link

groeneveld commented Jul 22, 2022

Following these commands creates the wifi hotspot for me, but DHCP doesn't seem to work, even though I have dnsmasq installed. The clients are stuck on "self assigned ip". I'm on ubuntu server 22.04 on a raspberry pi w 2.

@JonnyTech
Copy link

Following these commands creates the wifi hotspot for me, but DHCP doesn't seem to work, even though I have dnsmasq installed. The clients are stuck on "self assigned ip". I'm on ubuntu server 22.04 on a raspberry pi w 2.

@groeneveld on Debian derivatives (ie Ubuntu, Raspian, etc) you need to install dnsmasq-base and not dnsmasq

@sp00ck
Copy link

sp00ck commented Aug 28, 2022

this is wep (very weak encryptions)
how using WPA3 ?

@CEMoreno
Copy link

I never ran into any problems with Ubuntu on a Pi. This week I've been transitioning to an Asus Tinker board S R2.0 running Debian 10 kernel 4.4.194 and it has been frustrating.
wlan0 is up with the correct IP address: 10.42.0.1
Hotspot is up and SSID is there, advertised
Select SSID, try to login and then
Screen Shot 2022-09-16 at 5 20 02 PM

UUID is correct,... basically everything seems correct. I don't know what I am missing
Any help will be appreciated

@funnywwh
Copy link

funnywwh commented Oct 5, 2022

In archlinux to share the wired internet via wifi, the dnsmasq packet is required for the network to lift properly.

It is also possible to create the network in a single command.

$ IFNAME="wlan0" && CON_NAME="MATARIFE" && PASSWD="password" && nmcli c add type wifi ifname $IFNAME con-name $CON_NAME autoconnect yes ssid $CON_NAME 802-11-wireless.mode ap 802-11-wireless.band bg ipv4.method shared wifi-sec.key-mgmt wpa-psk wifi-sec.psk "$PASSWD"

And then just lift the network.

$ nmcli con up $CON_NAME

We check that the wired network is sharing the internet via wifi.

$ nmcli d s | grep ^$IFNAME
wlan0          wifi      connected     MATARIFE

I also have a script that automates the procedure. For more information I also have a post on my Telegram channel dedicated to the topic.

greate on rk3568 debian

@cracktorio
Copy link

All my experiments with this didnt work, i think this is because of the security type specified.
If you have the networkmanager applet in some way or another, for example the xfce one, then you can just create new network and then after making it if you want to start it a later time just do nmcli con up NETWORKNAME

@opeolluwa
Copy link

opeolluwa commented Jul 8, 2023

Is there any shell scripts which could automate this.

I need a way to bundle it all up in a bash script, which I can just call once, to get me the ifname and create hotspot

@cracktorio
Copy link

I made one, it's very easy once you get to know the basics of shell. You'll need to know "read", variables, "case" if you want to get fancy, "echo", and obviously nmcli. For example, with a variable it would be nmcli con up $VARIABLENAME

@opeolluwa
Copy link

@Blokheck011 that's the challenge, I'm not quite great with Shell scripting and It's a lot to put on hold, the wifi file sharing application I'm building, and go on learning shell, I need something I can plug and play

@opeolluwa
Copy link

Plus, I still need to reimplement the solution in Rust

@abhishekmj303
Copy link

abhishekmj303 commented Sep 8, 2023

@CEMoreno @Blokheck011 This worked for me in when I was facing security type issue.

nmcli con modify Hotspot 802-11-wireless-security.pmf 1

Source: AskUbuntu

@gkucmierz
Copy link

Error: Connection activation failed: No suitable device found for this connection (device enp12s0 not available because profile is not 
compatible with device (mismatching interface name)).

I have this error, when I try to up hotspot.

@TriMoon
Copy link

TriMoon commented Nov 8, 2023

Please see: https://unix.stackexchange.com/a/760947/550161
Where i ask for a non-AP mode solution... 🤝

@shubham-kshetre
Copy link

this is wep (very weak encryptions) how using WPA3 ?

I'm also trying to use same security type (WPA3) glad to know if you succeeded for it?

@lalvesl
Copy link

lalvesl commented Feb 19, 2024

Thank you all, especially abhishekmj303 for this argument (802-11-wireless-security.pmf 1)

@arrio464
Copy link

Excellent!
In some cases, you may need:

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

@mohamad-supangat
Copy link

reference on https://owlhowto.com/how-to-create-a-wifi-hotspot-on-debian-12/

using this to auto create hotspot on boot

nmcli con mod Hostspot connection.autoconnect yes

@opeolluwa
Copy link

reference on https://owlhowto.com/how-to-create-a-wifi-hotspot-on-debian-12/

using this to auto create hotspot on boot

nmcli con mod Hostspot connection.autoconnect yes

Let me get this, it's possible to create hotspot without being connected to a WiFi?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment