Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.

@rfloriano

This comment has been minimized.

Copy link

@rfloriano rfloriano commented Dec 11, 2020

Thanks for share! Nice job.

Just to add something, following your directions, I was stuck on an error when starting dnsmasq:
failed to create listening socket for port 53: Address already in use

This error occurs because systemd-resolved is running and use this port. Particularly, I don't want to change this behavior and just disabled dnsmasq DNS feature, so I simply changed the port adding to file /etc/dnsmasq.conf:
port=5353

Just in case someone else needs if found this tutorial.

@yaniron

This comment has been minimized.

Copy link

@yaniron yaniron commented Jan 2, 2021

if you want dnsmasq to be your resolver, you can also follow the instructions here:
https://computingforgeeks.com/install-and-configure-dnsmasq-on-ubuntu-18-04-lts/

@dony71

This comment has been minimized.

Copy link

@dony71 dony71 commented Jan 13, 2021

I have ubuntu 20.04 and I don't know why keep getting "unknown interface wlan0"
Anything I'm missing?

cat /lib/systemd/system/dnsmasq.service

[Unit]
Description=dnsmasq - A lightweight DHCP and caching DNS server
Requires=network.target
Wants=nss-lookup.target network-online.target
Before=nss-lookup.target
After=network.target network-online.target

[Service]
Type=forking
PIDFile=/run/dnsmasq/dnsmasq.pid

ExecStartPre=/usr/sbin/dnsmasq --test

ExecStart=/etc/init.d/dnsmasq systemd-exec

ExecStartPost=/etc/init.d/dnsmasq systemd-start-resolvconf
ExecStop=/etc/init.d/dnsmasq systemd-stop-resolvconf

ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
@Waiver-Wire-Addict

This comment has been minimized.

Copy link

@Waiver-Wire-Addict Waiver-Wire-Addict commented Feb 19, 2021

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

This comment has been minimized.

Copy link

@Baerlie 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

This comment has been minimized.

Copy link

@Robotgir 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

This comment has been minimized.

Copy link

@Claude-Mally Claude-Mally commented Jun 22, 2021

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

This comment has been minimized.

Copy link

@incebellipipo incebellipipo commented Oct 4, 2021

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

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