Ubuntu下创建Wifi(AP)

前言

前几天,博主本人想用Android手机连接wifi,但又不想买无线路由,于是便打算用笔记本当无线路由使,但是随即发现本人目前用的Ubuntu下的wifi模式默认是ad-hoc,于是在网上找了大堆资料,但是这些资料都非常混乱,但是经过一番倒腾,本人还是成功了。为方便后来者,特此把解决方法整理如下。(不用谢~~~)


准备工具

1.安装dnsmasq(下载

2.安装hostapd(下载


步骤

1.配置dnsmasq配置文件(/etc/dnsmasq.conf),内容如下:

interface=wlan0
dhcp-range=192.168.0.2,192.168.0.255,6h
注:更完整的dnsmasq.conf配置可参考 这里

2.配置hostapd配置文件(/etc/hostapd.conf),内容如下:

interface=wlan0
driver=nl80211
ssid=MyWiFI_AP
country_code=US
hw_mode=g
channel=7
macaddr_acl=0

注:更完整的hostapd.conf配置可参考 这里

3.创建启动wifi脚本(/bin/openwifi),内容如下:

#! /bin/sh

hostapd_filepath='/etc/init.d/hostapd'
hostapd_pid_filepath='/var/run/hostapd.pid'
hostapd_name='hostapd'
dnsmasq_filepath='/etc/init.d/dnsmasq'
dnsmasq_pid_filepath='/var/run/dnsmasq/dnsmasq.pid'
dnsmasq_name='dnsmasq'

stop_service(){
    $1 stop
    sleep 2s
    if [ -f $2 ]; then
        pid=`cat $2`
        if [ -f "/proc/$pid/status" ]; then
            result=`cat /proc/$pid/status|grep "$3"`
            if [ $result = "" ];  then
                echo "$3关闭[\033[32;1m成功\033[0m]"
            else 
                echo "$3关闭[\033[31;1m失败\033[0m]"
                exit 1
            fi
        else
            echo "$3关闭[\033[32;1m成功\033[0m]"
        fi
    else
        echo "$3关闭[\033[32;1m成功\033[0m]"
    fi
}
start_service(){
    $1 restart
    pid=`cat $2`
    if [ "$pid" = "" ]; then
        echo "$3打开[\033[31;1m失败\033[0m]"
        exit 1
    else 
        result=`cat "/proc/$pid/status" | grep "$3"`
        if [ "$result" != "" ]; then
            echo "$3打开[\033[32;1m成功\033[0m]"
        else
            echo "$3打开[\033[31;1m失败\033[0m]"
            exit 1
        fi
    fi
}

case $1 in
    start)
        #设置默认转发的网卡eth0
        defautIface=eth0
        #从路由表查询当前使用外网的网卡,如果有讲数据转发到这个网卡,如果没有,默认转发到eth0
        result=`netstat -r|grep default|awk '{print $8}'`

        if [ "$result" = "" ]; then
            echo 当前没有使用外网的网卡,数据默认转发到$defautIface网卡上
        else
            defautIface=$result
            echo 当前使用外网的网卡$result,数据转发到$defautIface网卡上
        fi

        #启用wifi设备
        rfkill unblock wifi

        #测试wlan0接口是否启用
        ifconfig wlan0 192.168.0.1 netmask 255.255.255.0 up
        result=`grep -e "wlan0" /proc/net/dev_mcast|awk '{print $2}'`
        if [ "$result" != "" ]; then
            echo "The device is ready for use-->[\033[32;1m成功\033[0m]"
        else
            echo "The device is not ready for use-->[\033[31;1m失败\033[0m]"
            exit 1
        fi

        #开启hostapd
        start_service $hostapd_filepath $hostapd_pid_filepath $hostapd_name 
        #开启dnsmasq
        start_service $dnsmasq_filepath $dnsmasq_pid_filepath $dnsmasq_name
        
        #打开ip转发
        sudo sysctl -w net.ipv4.ip_forward=1
        result=`cat /proc/sys/net/ipv4/ip_forward`
        if [ "$result" = "1" ]; then
            echo "打开IP转发[\033[32;1m成功\033[0m]"
        else
            echo "打开IP转发[\033[31;1m失败\033[0m]"
            exit 1
        fi

        iptables -F
        iptables -X
        iptables -t nat -F
        iptables -t nat -X
        iptables -t nat -A POSTROUTING -o $defautIface -j MASQUERADE
        echo "NAT 激活[\033[32;1m成功\033[0m]"
        echo "WiFi启动[\033[32;1m成功\033[0m]"

        ;;

    stop)
        #关闭hostapd
        stop_service $hostapd_filepath $hostapd_pid_filepath $hostapd_name 
        #关闭dnsmasq
        stop_service $dnsmasq_filepath $dnsmasq_pid_filepath $dnsmasq_name

        #测试wlan0接口是否启用
        ifconfig wlan0 down 
        result=`grep -e "wlan0" /proc/net/dev_mcast|awk '{print $2}'`
        if [ "$result" = "" ]; then
            echo "wlan0关闭[\033[32;1m成功\033[0m]"
        else
            echo "wlan0关闭[\033[31;1m失败\033[0m]" 
            exit 1
        fi
        
        #打开ip转发
        sudo sysctl -w net.ipv4.ip_forward=0
        result=`cat /proc/sys/net/ipv4/ip_forward`
        if [ "$result" = "0" ]; then
            echo "关闭IP转发[\033[32;1m成功\033[0m]"
        else
            echo "关闭IP转发[\033[31;1m失败\033[0m]"
            exit 1
        fi
        echo "WiFi停止[\033[32;1m成功\033[0m]"
        ;;
    *)
        echo "Usage: $0 {start|stop}"
esac

4.启动wifi,执行如下命令:

sudo openwifi start


猜你喜欢

转载自blog.csdn.net/LYH66/article/details/39584985