Skip to content

Instantly share code, notes, and snippets.

@ExtremeGTX
Last active September 13, 2024 22:26
Show Gist options
  • 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.

@Waiver-Wire-Addict
Copy link

Hi dony71,

wlan0 is the device name of the wireless card. The example was assuming Raspberry Pi hardware, and that should be the name of the built in wireless on a Raspberry by 3/4. The instructions have you edit a .yaml file in the /etc/netplan directory, and the device name is specified in that file.

If you are getting and unknown interface wlan0, it is most likely because your wireless card has a different name, and you would have to put it's correct name in the .yaml file.

To find the name of your wireless card device, you could do this:

sudo lshw -c network

In the output of that command you should look for "Wireless" in the description, and in that section, the logical name will be the device name you need. Unfortunately not all wireless card all supported, and it is still possible that lshw will not have a logical name for your device.

Hope that helps.

@Baerlie
Copy link

Baerlie commented Mar 31, 2021

Thank you for this short and easy setup procedure! I had the same problem as rfloriano with port 53. I changed the conf as advised and it's working. Thank you!

@Robotgir
Copy link

Robotgir commented Jun 2, 2021

Can any one kindly guide me?
how can i ssh into the rpi which is configured as access point?
What will be the ip address of the access point?

@Claude-Mally
Copy link

Following this guide, I can see the ssid from my phone, but attempting to connect invariably leads to an invalid password error. The iPhone error reads "Incorrect password for <>". Obviously my password is over 8 characters long, correctly typed. Any idea what can be wrong?

@incebellipipo
Copy link

I noticed that, in some cases systemd-resolved service and dnsmasq service can conflict with each other. This answer would help in those scenarios.
https://unix.stackexchange.com/a/358485

@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