分享

Turn Linux Computer into Wi

 罔鸟 2024-03-24 发布于广东

In this article, we will show how to create a Wi-Fi access point (Software AP) on any computer or laptop running Linux and equipped with a wireless adapter. You can use this access point (hotspot) to create a wireless local network in a small office or at home, or to share mobile Internet with other devices. In this example, we are using a laptop running the latest version of Ubuntu.

First, make sure that your wireless Wi-Fi module supports an Access Point (AP) mode. To do it, install iw package:

$ sudo apt install iw

The iw tool is used to configure wireless interfaces (nl80211 driver) from the command prompt. It supports all new drivers recently added to the Linux kernel. The old iwconfig tool, which uses the Wireless Extensions interface, is deprecated, so it is strongly recommended that you use iw and nl80211 instead.

Run the command below:

$ iw list

iw: check supported wireless inerface modes

In the Supported interface modes section, look for

  • AP
  • AP/VLAN

It means that your Wi-Fi adapter supports operation in the access point mode.

How to Create Wi-Fi Hotspot from Ubuntu GUI?

On Ubuntu 20.04 LTS and newer with GNOME (3.28+), you can run a wireless access point from the graphic interface. This is the easiest method for the average user.

Click the network connection icon and select Turn On Wi-Fi Hotspot in the Wi-Fi settings.

Wi-Fi settings in Gnome on Ubuntu

Turn On Wi-Fi Hotspot on Ubuntu

Enter the access point name and connection key (password)

Set the SSID (name) and the password for the Wireless Access Point

You should see a message that your access point is active. The same window contains a QR code to connect to your Wi-Fi access point.

generate wi-fi qr code on linux

Configure Wireless Access Point with Network Manager (nmcli) on Linux

Network Manager can be used to manage network connections in most modern Linux distros (Ubuntu, Debian, Mint, Fedora, CentOS, etc.). In this example, we will show how to create a software access point on Linux using the Network Manager and nmcli command line tool.

First, you need to find out the name of your wireless adapter in the system.

$ nmcli d

get wireless adapter name on ubuntu linux

In our example, the name of the Wi-Fi adapter is wlp4s0b1.

If dnsmasq (a light-weight DHCP/DNS server) is installed on your computer, you must disable it. It will conflict with the dnsmasq plugin in the NetworkManager.

To create an access point on the wlp4s0b1 interface with the network name (SSID) WOSHubWiFi and a network key (password) MaxPass21, run the commands below:

# nmcli con add type wifi ifname wlp4s0b1 mode ap con-name MyHomeWiFI ssid WOSHubWiFi
# nmcli con modify MyHomeWiFI 802-11-wireless.band bg
# nmcli con modify MyHomeWiFI 802-11-wireless.channel 1
# nmcli con modify MyHomeWiFI 802-11-wireless-security.key-mgmt wpa-psk
# nmcli con modify MyHomeWiFI 802-11-wireless-security.proto rsn
# nmcli con modify MyHomeWiFI 802-11-wireless-security.group ccmp
# nmcli con modify MyHomeWiFI 802-11-wireless-security.pairwise ccmp
# nmcli con modify MyHomeWiFI 802-11-wireless-security.psk MaxPass21
# nmcli con modify MyHomeWiFI ipv4.method shared

Start your hotspot:
# nmcli con up WOSHubWiFi

Use nmcli on ubuntu to start wireless hotspot

If you want the access point to operate at 5GHz and use a higher channel, change the corresponding commands to:

# nmcli con modify MyHomeWiFI 802-11-wireless.band a
# nmcli con modify MyHomeWiFI 802-11-wireless.channel 35

By default, the built-in DHCP server in the NetworkManager assigns IP addresses to clients from the 10.42.0.x/24 range (and the 10.24.0.1 is the address of your hotspot interface). You can change the DCHP range:

# nmcli con modify MyHomeWiFI ipv4.addr 192.168.31.1/24

To disable the access point, run this command:

# nmcli con down MyHomeWiFI

For complete removal of software hotspot on Linux:

# nmcli con delete MyHomeWiFI

Using Hostapd to Set Up a Virtual WiFi Access Point on Linux

A popular tool for creating a Wi-Fi access point on Linux computers is the hostapd package. Install it:

$ sudo apt install hostapd

Copy the original configuration file (/etc/hostapd/hostapd.conf). Add the following configuration to hostapd.conf:

$ sudo nano /etc/hostapd/hostapd.conf

interface= wlp4s0b1
# The nl80211 driver is suitable for most Wi-Fi adapters
driver=nl80211
# Your access point name
ssid=MyHomeAP
# The number of your Wi-Fi channel (1 to 13)
channel=11
#Select a mode for your access point (a = IEEE 802.11a, b = IEEE 802.11b, g = IEEE 802.11g)
hw_mode=g
# 1=wpa, 2=wep, 3=both
auth_algs=1
# Allow connections from all MAC addresses except those in the blacklist
macaddr_acl=0
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
#Set a password for your access point
wpa_passphrase=MyPass321
If you want to use 802.11 n mode, make the changes below:
ieee80211n=1
ht_capab=[HT40-][SHORT-GI-40]

Unmask the hostpad service:

$ sudo systemctl unmask hostapd

Then specify the path to your configuration file in the DAEMON_CONF parameter of /etc/default/hostapd file:

$ sudo nano /etc/default/hostapd

DAEMON_CONF="/etc/hostapd/hostapd.conf"

configure hostapd on linux

Then configure your wireless network interface. Set a static IP address, DNS, mode, and other options. Edit the /etc/network/interfaces file and add the following:

# nano /etc/network/interfaces

auto wlp3s0
iface wlp3s0 inet static
address 10.10.0.1
netmask 255.255.255.0

You will also need it to prevent NetworkManager from interfering with the Wi-Fi interface. After saving the settings, restart your computer.

Then you just have to configure a DHCP server that will assign IP addresses to the devices connecting to your Linux access point. In our example, we will use a light-weight dnsmasq:

$ sudo apt install dnsmasq

Edit dnsmasq.conf. Specify that dnsmasq will run on the wlp4s0b1 interface and assign IP addresses from the 10.10.0.x range. In the simplest case, you may have the following configuration:

$ sudo nano /etc/dnsmasq/dnsmasq.conf

interface=wlp4s0b1
dhcp-range=10.10.0.2,10.10.0.100,12h
#Set DNS servers
server=/www.google.com/8.8.8.8
The DHCP range IP addresses must belong to the same network as your WLAN adapter.

In order for your Linux host can route packets between the Wi-Fi clients and an external Internet connection, you need to enable the routing and add the corresponding rule to iptables:

sysctl net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o enp3s0 -j MASQUERADE
You can also use the bridge interface to provide Internet access to clients from your local network (we will leave this out to keep the article short).

Start these services:

# systemctl start dnsmasq.service
# systemctl start hosapd

Now you can connect to your access point from any client, check that it has received an IP address from the specified DHCP range and that it can access the Internet.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多