树莓派做无线路由器

eth0连接互联网,wlan0做wifi路由器

1.配置wlan0为固定ip

sudo vi /etc/network/interfaces

allow-hotplug wlan0
iface wlan0 inet static
address 192.168.111.1
netmask 255.255.255.0

2.安装hostapd

sudo apt-get install hostapd

3.配置hostapd

sudo vi /etc/default/hostapd

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

sudo vi /etc/hostapd/hostapd.conf

# 把无线网卡wlan0 作为接入点
interface=wlan0
# 使用nl80211驱动
driver=nl80211
#共享网络的SSID是RaspberryPi
ssid=RaspberryPi
# 网卡工作在802.11G模式
hw_mode=g
#无线网卡选用11信道
channel=11
# WPA2 配置
wpa=2
#wpa密码是raspberry
wpa_passphrase=raspberry
#认证方式为WPA-PSK 加密方式为CCMP
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP
beacon_int=100
auth_algs=3
wmm_enabled=1

sudo service hostapd restart

4.安装isc-dhcp-server

sudo apt-get install isc-dhcp-server

5.配置isc-dhcp-server

sudo vi /etc/dhcp/dhcpd.conf


下面的配置都是在配置dhcp数据,和eth0没关系

default-lease-time 600;
max-lease-time 7200;
log-facility local7;
 
subnet 192.168.111.0 netmask 255.255.255.0 {
  range 192.168.111.10 192.168.111.100;
  option routers 192.168.111.1;
  option broadcast-address 192.168.111.127;
  option domain-name-servers 8.8.8.8,8.8.4.4;
  default-lease-time 600;
  max-lease-time 7200;
}



sudo vi /etc/default/isc-dhcp-server

# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
#       Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACES="wlan0"


sudo service  isc-dhcp-server restart

6.配置数据转发

通过前面的配置过程,已经可以通过无线网络连接到Raspberry pi的无线网卡,但是还不能连接到互联网,下一步就是让从无线网卡 wlan0 进来的数据 转发到 有线网卡 eth0上面 通过有线网卡连接网络接到互联网。

因为eth0是获取的动态ip,所以这里通过iptables来实现简单的路由转发。

输入命令

sudo iptables -F
sudo iptables -X
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo bash
iptables-save > /etc/iptables.up.rules
exit

输入命令

sudo vi /etc/network/if-pre-up.d/iptables

把下面两行复制粘贴到编辑窗口

#!/bin/bash
/sbin/iptables-restore < /etc/iptables.up.rules

保存退出,输入命令

sudo chmod 755 /etc/network/if-pre-up.d/iptables

开启内核转发,输入命令

sudo vi /etc/sysctl.conf

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

输入命令

sudo sysctl -p

7.服务自动启动

$ sudo chkconfig --add hostapd 
$ sudo chkconfig --add isc-dhcp-server


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值