Skip to content

Instantly share code, notes, and snippets.

@ExtremeGTX
Last active December 20, 2023 22:17
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save ExtremeGTX/ea1d1c12dde8261b263ab2fead983dc8 to your computer and use it in GitHub Desktop.
Save ExtremeGTX/ea1d1c12dde8261b263ab2fead983dc8 to your computer and use it in GitHub Desktop.
Setup Ubuntu server as Access point

Introduction

This tutorial for setting up Ubuntu Server (RPi 3B) as Wifi access point

Overview:

The main steps can be listed as following:

  1. Install required packages
  2. Setup hostapd
  3. Setup DNSmasq
  4. Configure AP IP Address

Install required packages

sudo apt-get install hostapd dnsmasq

Hostapd

  • The purpose of Hostapd is to set WiFi as an access point

  • we need to write a new config file for hostapd sudo vi /etc/hostapd/hostapd.conf

    interface=wlan0
    driver=nl80211
    ssid=MyWiFiNetwork
    hw_mode=g
    channel=7
    wmm_enabled=0
    macaddr_acl=0
    auth_algs=1
    ignore_broadcast_ssid=0
    wpa=2
    wpa_passphrase=12345678
    wpa_key_mgmt=WPA-PSK
    wpa_pairwise=TKIP
    rsn_pairwise=CCMP
    
    
  • Then we need to tell hostapd to use our config file, edit /etc/default/hostapd and change the line starts with #DAEMON_CONF, remember to remove #

    DAEMON_CONF="/etc/hostapd/hostapd.conf"
    
  • Then Let's start hostapd

    sudo systemctl unmask hostapd
    sudo systemctl enable hostapd
    sudo systemctl start hostapd

dnsmasq

  • The purpose of dnsmasq is to act as DHCP Server, so when a devies connects to Raspberry Pi it can get an IP assigned to it.

  • make a backup of default config by: sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.org

  • Create a new config file by: sudo vi /etc/dnsmasq.conf

  • This config file will automatically assign addresses between 192.168.4.2 and 192.168.4.20 with lease time 24 hours.

    interface=wlan0
    dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
    
    
  • Then Let's reload dnsmasq config

    sudo systemctl reload dnsmasq

Solving startup Error:

  • On System startup, dnsmasq will not wait for wlan0 interface to initialize and will fail with error wlan0 not found.

  • We need to tell systemd to launch it after network get ready, so we will modify dnsmasq service file by adding After= and Wants= under [Unit] section.

    sudo vi /lib/systemd/system/dnsmasq.service

    [Unit]
    ...
    After=network-online.target
    Wants=network-online.target
    
    

Config static IP

  • Ubuntu uses cloud-init for initial setup, so will modify the following file to set wlan0 IP.

  • DON'T USE TABS IN THIS FILE, IT WILL NOT WORK, EVER!!

  • Modify the cloud-init file by sudo vi /etc/netplan/50-cloud-init.yaml

  • Add the following content to the file:

            wlan0:
                dhcp4: false
                addresses:
                - 192.168.4.1/24
    
  • The file will finally looks like this:

    # This file is generated from information provided by
    # the datasource.  Changes to it will not persist across an instance.
    # To disable cloud-init's network configuration capabilities, write a file
    # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
    # network: {config: disabled}
    network:
        version: 2
        ethernets:
            eth0:
                dhcp4: true
                match:
                    macaddress: 12:34:56:78:ab:cd
                set-name: eth0
            wlan0:
                dhcp4: false
                addresses:
                - 192.168.4.1/24
    
    

Finally:

  • Reboot your Raspberry Pi and check if you can connect to it over WiFi and can SSH.

Notes:

  • if you can't see Raspberry Pi Hot spot then hostapd is not working, you can check its logs by sudo systemctl status hostapd.

  • if you can coonect to Raspberry Pi but can't get an IP then dnsmasq is not working, you can check its logs by sudo systemctl status dnsmasq.

@grkidwell
Copy link

grkidwell commented Dec 11, 2021

am successfully getting an ip address from the pi and can ssh into the pi, but cannot access the internet from a client using the pi as an access point. pings to 8.8.8.8 or any other ip address do not return. I am a bit confused in how ip forwarding is handled with this overall procedure, as in the past when creating an accesspoint on raspiOS, ip forwarding had to be enabled and iptables edited.

@AnthonyOGorman
Copy link

I was having issues with the DHCP server not allocating ip's. Seems like it was my firewall stopping it.
You can find the ufw rules here:
https://ubuntuforums.org/showthread.php?t=1146048

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