Setting up a WiFi access point on Ubuntu using Netplan might seem daunting at first, but trust me, it’s totally doable and pretty straightforward once you get the hang of it. Netplan is the network configuration tool introduced in Ubuntu 17.10, designed to simplify network management. Instead of wrestling with a bunch of different configuration files, Netplan uses YAML files to define your network interfaces. This guide will walk you through creating a WiFi access point using Netplan on your Ubuntu system. So, let's dive in and get your WiFi hotspot up and running!

    Prerequisites

    Before we get started, there are a few things you’ll need to have in place:

    • An Ubuntu System: This guide assumes you're running Ubuntu 17.10 or later.
    • WiFi Adapter: A wireless network adapter that supports Access Point (AP) mode. Most modern adapters do, but it’s worth checking.
    • Internet Connection: Your Ubuntu system needs to have an existing internet connection, either through Ethernet or another WiFi connection, that you want to share.
    • Root Privileges: You’ll need sudo access to modify network configurations.

    Make sure your system is up-to-date by running these commands:

    sudo apt update
    sudo apt upgrade
    

    Step 1: Install Necessary Packages

    First, you need to install a few packages that will help create the access point. We'll be using hostapd to create the access point and dnsmasq to handle DHCP and DNS services. hostapd (host access point daemon) is a user space daemon for access point and authentication servers. It allows your wireless card to act as a network hub. dnsmasq provides DNS forwarding and DHCP server functionality. It’s lightweight and easy to configure, making it perfect for our needs.

    Open your terminal and run the following command:

    sudo apt install hostapd dnsmasq
    

    Once the installation is complete, you're ready to move on to configuring these services.

    Step 2: Configure hostapd

    Now, let’s configure hostapd. This involves creating a configuration file that tells hostapd how to behave as an access point. This file will specify the network name (SSID), the channel, and the security settings.

    1. Create the Configuration File:

      Create a new configuration file for hostapd using your favorite text editor. For example, you can use nano:

    sudo nano /etc/hostapd/hostapd.conf ```

    1. Add the Configuration:

      Add the following configuration to the file. Make sure to replace your_wifi_name and your_password with your desired network name and password.

      interface=wlan0
      driver=nl80211
      ssid=your_wifi_name
      hw_mode=g
      channel=6
      wmm_enabled=0
      macaddr_acl=0
      auth_algs=1
      ignore_broadcast_ssid=0
      wpa=2
      wpa_key_mgmt=WPA-PSK
      wpa_pairwise=TKIP
      rsn_pairwise=CCMP
      wpa_passphrase=your_password
      

      Here’s a breakdown of what each line means:

      • interface=wlan0: Specifies the wireless interface to use (usually wlan0).
      • driver=nl80211: Specifies the driver to use.
      • ssid=your_wifi_name: Sets the name of your WiFi network.
      • hw_mode=g: Sets the wireless mode (g is compatible with most devices).
      • channel=6: Sets the channel for the WiFi network. You can choose a different channel if needed.
      • wmm_enabled=0: Disables Wi-Fi Multimedia (WMM).
      • macaddr_acl=0: Disables MAC address filtering.
      • auth_algs=1: Sets the authentication algorithm.
      • ignore_broadcast_ssid=0: Makes the SSID visible.
      • wpa=2: Uses WPA2 encryption.
      • wpa_key_mgmt=WPA-PSK: Uses WPA pre-shared key.
      • wpa_pairwise=TKIP: Sets the WPA pairwise cipher.
      • rsn_pairwise=CCMP: Sets the RSN pairwise cipher.
      • wpa_passphrase=your_password: Sets the password for the WiFi network.
    2. Save and Close the File:

      Press Ctrl+X, then Y, then Enter to save and close the file.

    3. Configure hostapd to Use the Configuration File:

      Next, you need to tell hostapd to use this configuration file. Open the default hostapd configuration file:

    sudo nano /etc/default/hostapd ```

    Find the line that says `#DAEMON_CONF=`, and uncomment it, then set it to the path of your configuration file:
    
    ```
    DAEMON_CONF="/etc/hostapd/hostapd.conf"
    ```
    
    Save and close the file.
    

    Step 3: Configure dnsmasq

    Now, let’s configure dnsmasq to handle IP addresses for devices that connect to your WiFi access point. dnsmasq will act as a DHCP server, assigning IP addresses to connected devices, and as a DNS forwarder, resolving domain names.

    1. Backup the Original Configuration File:

      Before making changes, it’s a good idea to back up the original configuration file:

    sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig ```

    1. Create a New Configuration File:

      Create a new configuration file for dnsmasq:

    sudo nano /etc/dnsmasq.conf ```

    1. Add the Configuration:

      Add the following configuration to the file:

      interface=wlan0
      dhcp-range=192.168.10.100,192.168.10.200,24h
      dhcp-option=3,192.168.10.1
      dhcp-option=6,192.168.10.1
      server=8.8.8.8
      server=8.8.4.4
      

      Here’s what each line does:

      • interface=wlan0: Specifies the interface to listen on.
      • dhcp-range=192.168.10.100,192.168.10.200,24h: Sets the range of IP addresses to assign (from 192.168.10.100 to 192.168.10.200) and the lease time (24 hours).
      • dhcp-option=3,192.168.10.1: Sets the default gateway for connected devices.
      • dhcp-option=6,192.168.10.1: Sets the DNS server for connected devices.
      • server=8.8.8.8 and server=8.8.4.4: Sets Google's public DNS servers.
    2. Save and Close the File:

      Press Ctrl+X, then Y, then Enter to save and close the file.

    Step 4: Configure Netplan

    Netplan is the network configuration tool that Ubuntu uses to manage network interfaces. We need to configure Netplan to set up a static IP address for our wireless interface and to enable IP forwarding so that connected devices can access the internet.

    1. Find Your Netplan Configuration File:

      Netplan configuration files are located in the /etc/netplan/ directory. The file name usually starts with a number and ends with .yaml. For example, it might be named 01-network-manager-all.yaml or 50-cloud-init.yaml. Identify the correct file.

    ls /etc/netplan/ ```

    1. Edit the Netplan Configuration File:

      Open the Netplan configuration file with root privileges:

    sudo nano /etc/netplan/your_netplan_file.yaml ```

    Replace `your_netplan_file.yaml` with the actual name of your Netplan configuration file.
    
    1. Add the Configuration:

      Modify the file to include the following configuration. Adjust the wlan0 interface name if necessary. Be very careful with the indentation, as YAML is sensitive to it.

      network:
        version: 2
        renderer: networkd
        ethernets:
            eth0:
                dhcp4: no
                dhcp6: no
                addresses: [192.168.0.20/24]
                gateway4: 192.168.0.1
                nameservers:
                        addresses: [8.8.8.8,8.8.4.4]
        wifis:
            wlan0:
                dhcp4: no
                dhcp6: no
                addresses: [192.168.10.1/24]
                access-points:
                    "your_wifi_name":
                        password: "your_password"
                mode: ap
                gateway4: 192.168.0.1
                nameservers:
                        addresses: [8.8.8.8,8.8.4.4]
      

      Here’s a breakdown of what each line does:

      • network: Top-level key indicating network configuration.
      • version: 2: Specifies the Netplan configuration version.
      • renderer: networkd: Specifies the network renderer to use.
      • ethernets : Ethernet configuration * eth0: your ethernet port name
        • dhcp4: no: Disables DHCP for IPv4.
        • dhcp6: no: Disables DHCP for IPv6.
        • addresses: [192.168.0.20/24]: Sets a static IP address for eth0.
        • gateway4: 192.168.0.1: Sets the default gateway for IPv4.
        • nameservers: Specifies DNS servers.
          • addresses: [8.8.8.8,8.8.4.4]: Sets Google's public DNS servers.
      • wifis: Wireless interface configuration.
        • wlan0: Specifies the wireless interface.
          • dhcp4: no: Disables DHCP for IPv4.
          • dhcp6: no: Disables DHCP for IPv6.
          • addresses: [192.168.10.1/24]: Sets a static IP address for the wireless interface.
          • access-points: Configuration for the access point.
            • "your_wifi_name": The SSID of your WiFi network.
              • password: "your_password": The password for your WiFi network.
          • mode: ap: Sets the interface to access point mode.
    2. Apply the Netplan Configuration:

      Apply the Netplan configuration using the following command:

    sudo netplan apply ```

    If you encounter any errors, Netplan will let you know. Double-check your YAML file for syntax errors, especially indentation.
    

    Step 5: Enable IP Forwarding

    To allow devices connected to your WiFi access point to access the internet, you need to enable IP forwarding. This allows your Ubuntu system to route traffic between the wireless interface and the internet connection.

    1. Edit the sysctl.conf File:

      Open the /etc/sysctl.conf file with root privileges:

    sudo nano /etc/sysctl.conf ```

    1. Uncomment the IP Forwarding Line:

      Find the line #net.ipv4.ip_forward=1 and remove the # to uncomment it:

    net.ipv4.ip_forward=1 ```

    1. Save and Close the File:

      Press Ctrl+X, then Y, then Enter to save and close the file.

    2. Apply the Changes:

      Apply the changes by running:

    sudo sysctl -p ```

    Step 6: Configure NAT (Network Address Translation)

    Network Address Translation (NAT) is necessary to allow devices connected to your access point to access the internet through your Ubuntu system's internet connection. We'll use iptables to set up NAT.

    1. Set up NAT Rules:

      Run the following commands to set up NAT. Replace eth0 with the name of your internet-connected interface if it’s different.

    sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE sudo iptables -A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT sudo iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT ```

    Here’s what these commands do:
    
    *   `-t nat -A POSTROUTING -o eth0 -j MASQUERADE`: Enables NAT for traffic going out through the `eth0` interface.
    *   `-A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT`: Allows forwarded traffic from the wireless interface (`wlan0`) to the internet-connected interface (`eth0`) for established connections.
    *   `-A FORWARD -i eth0 -o wlan0 -j ACCEPT`: Allows forwarded traffic from the internet-connected interface (`eth0`) to the wireless interface (`wlan0`).
    
    1. Make the NAT Rules Persistent:

      These iptables rules are not persistent across reboots. To make them persistent, you can install the iptables-persistent package:

    sudo apt install iptables-persistent ```

    During the installation, you’ll be asked if you want to save the current IPv4 and IPv6 rules. Choose `Yes` for both.
    

    Step 7: Start the Services

    Now that you’ve configured everything, it’s time to start the hostapd and dnsmasq services.

    1. Start hostapd:

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

    1. Start dnsmasq:

    sudo systemctl enable dnsmasq sudo systemctl restart dnsmasq ```

    1. Check the Status of the Services:

      You can check the status of the services to make sure they are running correctly:

    sudo systemctl status hostapd sudo systemctl status dnsmasq ```

    If the services are running without errors, congratulations! Your WiFi access point should now be up and running.
    

    Troubleshooting

    If you encounter any issues, here are a few things to check:

    • Configuration Files: Double-check your hostapd.conf, dnsmasq.conf, and Netplan configuration files for any typos or errors.

    • Interface Names: Make sure you’re using the correct interface names (e.g., wlan0, eth0).

    • Driver Support: Ensure your wireless adapter supports Access Point (AP) mode. You can check this using the iw list command and looking for the AP capability.

    • Firewall Rules: Verify that your firewall rules are not blocking traffic.

    • Logs: Check the logs for hostapd and dnsmasq for any error messages:

    sudo journalctl -u hostapd sudo journalctl -u dnsmasq ```

    Conclusion

    And there you have it! You’ve successfully set up a WiFi access point on Ubuntu using Netplan. This can be super handy for sharing your internet connection, creating a local network, or setting up a dedicated network for IoT devices. By using hostapd for the access point functionality, dnsmasq for DHCP and DNS, and Netplan for network configuration, you have a robust and manageable setup. Remember to double-check your configurations and troubleshoot any issues that arise. Happy networking, guys! This detailed guide should help you get your WiFi access point up and running smoothly on your Ubuntu system. Good luck, and have fun experimenting with your new network setup!