NetworkManager – Terminal Commands Equivalent to ‘Use as Hotspot’ Option

internetiptablesnetworkingnetworkmanagerwifi

I'm making a gui application which will control my laptop in order to make it act as a wifi hotspot. I may change distributions so I want to find a solution which will work generally for all distributions. I am currently doing this on Fedora 17.

Using NetworkManager's "Use as Hotspot" button works very well to create an ad-hoc hotspot:
enter image description here

I want to recreate this functionality with terminal commands.

Below are two methods I tried to accomplish this but I havn't found a solution yet.


Method 1: iwconfig

Using research from accross the web, I tried the following command:

# ifconfig wlan0 10.42.0.1 netmask 255.555.255.0 broadcast 10.42.0.255 up
# iwconfig wlan0 essid my-lappy mode ad-hoc key 0123456789
# iptables-restore < saved-hotspot-iptables
# echo 1 > /proc/sys/net/ipv4/ip_forward
# dhclient wlan0

where saved-hotspot-iptables is a file which I generated with iptables-save > saved-hotspot-iptables with the iptables set by the "Use as Hotspot" function.

Method 1 Problem

Devices can see the SSID but are unable to connect. Running ip a reveals the following about wlan0:

...
3: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state DORMANT qlen 1000
...

Notice NO-CARRIER is present, and the state is DORMANT instead of UP.

Running ifconfig wlan0 shows the following:

...
wlan0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
...

Notice that RUNNING is missing as one of the flags.


Method 2: nmcli

If NetworkManager's "Use as Hotspot" button is pressed at least once on the system, /etc/sysconfig/network-scripts/ifcfg-Hotspot is created and I can use the following command to start a working hotspot:

# ifconfig wlan0 up
# nmcli con up id Hotspot
# iwconfig wlan0 essid my-lappy key 0123456789

Method 2 Problem

If the user has never pressed "Use as Hotspot" on NetworkManager before, nmcli con up id Hotspot won't work. I've looked at nm-connection-editor --create but couldn't find much information. I've also looked into manually creating /etc/sysconfig/network-scripts/ifcfg-Hotspot but this method does not seem elegant or extendible to other distributions.

Best Answer

See my answer to the "Create wireless access point and share internet connection with nmcli"

I created a Hotspot with the GNOME Network-Manager. The problem was, that I can not configure the SSID and password in GUI. If you create the Hotspot with the Network-Manager GUI, it creates the file /etc/NetworkManager/system-connections/Hotspot. In that file it is possible to edit the SSID and the password.

sudo vim /etc/NetworkManager/system-connections/Hotspot

The content of the file looks like this:

[connection]
id=Hotspot
uuid=0bf627gd-8e34-48c6-865a-06f898b4y1hb
type=wifi
autoconnect=false
permissions=
secondaries=

[wifi]
hidden=false
mac-address=YOUR_WIFI_INTERFACE_MAC_ADDRESS
mac-address-blacklist=
mode=ap
seen-bssids=
ssid=SSID_NAME

[wifi-security]
group=ccmp;
key-mgmt=wpa-psk
pairwise=ccmp;
proto=rsn;
psk=YOUR_WIFI_AP_PASSWORD

[ipv4]
dns-search=
method=shared

[ipv6]
dns-search=
method=auto

I only changed the ssid and the psk properties to my needs. Then I restarted my computer because the command: sudo systemctl restart NetworkManager for network restart seems not to work correctly, because in the Network-Manager GUI I have no wireless network settings anymore and also the following command worked not before the restart. After restart you can use the nmcli command to start the access point.

nmcli con up Hotspot ifname YOUR_WIFI_INTERFACE

YOUR_WIFI_INTERFACE you can find out with the command iwconfig.

Related Question