Configure hostapd shared WiFi in CentOS

Source: Internet
Author: User
Tags pkill wpa passphrase

Configure hostapd shared WiFi in CentOS

There are two ways to achieve soft AP: one is the BRIDGE mode, that is, the new BRIDGE is used to connect the Intranet and the Internet; the other is the routing mode, use NAT to convert Intranet and Internet data packets. Here I use the routing mode.
Tips: First confirm that the wireless network adapter supports AP mode, [root @ localhost/] # iw list, and check whether the Supported interface modes field has an AP in the list content, when you confirm that the AP mode can be enabled, follow the instructions below. I use TP-WN822N V2 download speed at 400 KBps +, in Windows with 360wifi speed is less than KBps.

Install hostapd Configure hostapd Install dnsmasq Configure dnsmasq Route forwarding Start script Install hostapd

Download hostapd
Find the hostapd installation package on The ustc image or download it elsewhere. Find the corresponding version.

[root@localhost /]#wget mirrors.ustc.edu.cn/fedora/epel/6/x86_64/hostapd-2.0-5.el6.x86_64.rpm

Directly install

[root@localhost /]#yum install hostapd-2.0-5.el6.x86_64.rpm

Sometimes, you may need to install libnl. If an error occurs, install the following.

Configure hostapd

The hostapd configuration file is in/etc/hostapd. conf.
Check my hostapd. conf.

[root@localhost /]#cat /etc/hostapd/hostapd.conf## This will give you a minimal, insecure wireless network.# # DO NOT BE SATISFIED WITH THAT!!!## A complete, well commented example configuration file is# available here:## /usr/share/doc/hostapd-2.0/hostapd.conf## For more information, look here:## http://wireless.kernel.org/en/users/Documentation/hostapd##ctrl_interface=/var/run/hostapd#ctrl_interface_group=wheel# Some usable default settings...#macaddr_acl=0auth_algs=1#ignore_broadcast_ssid=0# Uncomment these for base WPA & WPA2 support with a pre-shared keywpa=1wpa_key_mgmt=WPA-PSKwpa_pairwise=TKIP#rsn_pairwise=CCMP# DO NOT FORGET TO SET A WPA PASSPHRASE!!wpa_passphrase=XXXXXX# Most modern wireless drivers in the kernel need driver=nl80211driver=nl80211# Customize these for your local configuration...interface=wlan0hw_mode=gchannel=11ssid=XXXXXX

You only need to modify the ssid -- wifi name, wpa_passphrase -- Wi-Fi password, and interface -- specify as the AP Nic. Other basic values can be left unchanged. Optional values include hw_mode, a, B, and g. channel values can also be set to and 11.
Driver = nl80211 is a standard wireless driver interface. If your Nic does not support this interface, try rtlXXX (forgot ).

Install dnsmasq

When we see the software name, we thought it was a DNS tool. In fact, we can also use DHCP. The function of dsnmasq installation is to dynamically allocate ip addresses to wifi clients, so that you do not need to enter them manually every time. Now, install it!

[root@localhost /]#yum install dnsmasq 

Install hostapd directly here, and find the appropriate source. You can also install # yum install hostapd directly.

Configure dnsmasq

The dnsmasq configuration file is in/etc/dnsmasq. conf. In fact, most of the software configuration files are under the/etc file.

[root@localhost /]#cat /etc/dnsmasq.conf# For debugging purposes, log each DNS query as it passes through# dnsmasq.#log-queries# Log lots of extra information about DHCP transactions.#log-dhcp# Include a another lot of configuration options.#conf-file=/etc/dnsmasq.more.conf#conf-dir=/etc/dnsmasq.dinterface=wlan0bind-interfaceslisten-address=192.168.0.1 #no-dhcp-interface= dhcp-range=192.168.0.2,192.168.0.224,12h dhcp-option=3,192.168.0.1 dhcp-option=6,202.114.0.242

The interface is configured with your AP wireless network card. Listen-address is the ip address of your Nic. Dhcp-range is the range in which your wifi client automatically obtains the ip address. Dhcp-option = 3, set the route. Dhcp-option = 6. the ip address of the DNS server is set. If you do not know it, query the following:

[root@localhost /]# cat /etc/resolv.conf nameserver 202.114.0.242nameserver 202.114.0.131

Fill in dhcp-option = 6. Do not set the same amount as I set, unless you know where I am ^! ^

Route forwarding

Start route forwarding

[root@localhost /]#echo 1 > /proc/sys/net/ipv4/ip_forward 

Specify an ip address for the wireless network adapter

[root@localhost /]#/sbin/ip addr add 192.168.0.1/24 dev wlan0 

The ip address 196.128.0.1 and subnet mask 255.255.255.0 are specified for the wlan0 device.

NAT ing package to create iptables rules

[root@localhost /]#iptables -F[root@localhost /]#iptables -X[root@localhost /]#iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Start script

Every time you enable AP shared wifi, you need to set the wlan0 IP address, enable dnsmasq, enable hostapd, and set iptables rules, which is quite troublesome.
Use the following script to start # sh/home/my/ap. sh start and # sh/home/my/ap. sh stop

[root@localhost /]#cat /home/my/ap.sh#!/bin/sh#Clean things upinit() { #Stop NetworkManager, if already running (it will disturb you)sysctl net.ipv4.conf.all.forwarding=1/usr/sbin/serviceconf network-manager stop#Stop named, if already running. dnsmasq cannot run because it take up port 53 #killall named #Stop dnsmasq, if already running rfkill unblock all/usr/sbin/serviceconf dnsmasq stop #Stop hostapd, if already running /usr/bin/pkill hostapd #Bring down wlan0 /sbin/ip link set down dev wlan0}start() { #First clean things up #Start hostapd, and it will automatically be bringed up hostapd -B /etc/hostapd/hostapd.conf #Set ip on wlan0 /sbin/ip addr add 192.168.0.1/24 dev wlan0 #Start dnsmasq /usr/sbin/serviceconf dnsmasq start#Start ip_forward echo 1 > /proc/sys/net/ipv4/ip_forward #add iptables rule for NAT #/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADEiptables -Fiptables -Xiptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE}stop() { #Remove iptables rule /sbin/iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE #Stop dnsmasq /usr/sbin/serviceconf dnsmasq stop #Stop hostapd /usr/bin/pkill hostapd #bring down wlan0, and its ip address will automatically be removed /sbin/ip link set down dev wlan0}case "$1" in'start') start ;;'stop') stop ;;*) echo "usage $0 start|stop"esac

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.