Skip to content

Instantly share code, notes, and snippets.

@fakeowl1
Last active April 12, 2025 17:33
Show Gist options
  • Save fakeowl1/ddf33d262bb2165e0bf147e375e0525b to your computer and use it in GitHub Desktop.
Save fakeowl1/ddf33d262bb2165e0bf147e375e0525b to your computer and use it in GitHub Desktop.
Create Wi-FI Hotspot

Create Wi-fi hotspot if Wi-Fi connection doesn't exist

auto-wifi-hotspot

#!/bin/bash

check_wifi_conn=$(nmcli conn show --active | grep wifi)

if [ ! -z "$check_wifi_conn" ]; then
  echo "Wifi connection exists"
  exit 0;
fi

source .env-hotspot

hotspot_info=$(nmcli -t conn show | grep "$CON_NAME")

if [ -z "$hotspot_info" ]; then
  echo "Connection doesn't exist"
  nmcli con add type wifi ifname $INTERFACE con-name $CON_NAME autoconnect yes ssid $HOTSPOT_SSID
  nmcli con modify $CON_NAME 802-11-wireless.mode ap 802-11-wireless.band bg ipv4.method shared
  nmcli con modify $CON_NAME wifi-sec.key-mgmt wpa-psk
  nmcli con modify $CON_NAME wifi-sec.psk $HOTSPOT_PASS
  nmcli con modify $CON_NAME wifi-sec.pmf 1
  hotspot_info=$(nmcli -t conn show | grep "$CON_NAME")
else 
  echo "Connection exists"
fi

IFS=':' read -ra hotspot_info_array <<< "$hotspot_info"
UUID=${hotspot_info_array[1]}

nmcli con up uuid $UUID

auto-wifi-hotspot.service

[Unit]
Description=Automatic create hotspot if wifi connection doesn't exist
After=NetworkManager.service

[Service]
Type=simple
User=q
ExecStart=/usr/bin/bash /home/q/bin/auto-wifi-hotspot

[Install]
WantedBy=multi-user.target

.env-hotspot

HOTSPOT_SSID="Hotspot"
HOTSPOT_PASS="qwerty123"
CON_NAME="Hotspot"
INTERFACE="wlo1"

May this bash script isn't best realisation. I always look forward your suggestions.

Sources

Create a Wi-Fi hotspot on Linux using nmcli

#OrangePi #RaspberryPi #Hotspot #Linux #nmcli

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