將樹莓派配置成路由

blabla發表於2019-11-27

安裝pppoe撥號上網軟體

sudo apt install pppoe pppoeconf pppstatus

更新系統

sudo apt update 
sudo apt dist-update

安裝hostapd和dhcp

sudo apt install hostapd isc-dhcp-server

修改/etc/dhcp/dhcpd.conf 配置檔案

  • 註釋以下兩行
    # option domain-name "example.org";
    # option domain-name-servers ns1.example.org, ns2.example.org;
  • 取消註釋下面這行
    #authoritative;
  • 在行尾加上
    subnet 192.168.42.0 netmask 255.255.255.0 {
        range 192.168.42.10 192.168.42.50;
        option broadcast-address 192.168.42.255;
        option routers 192.168.42.1;
        default-lease-time 600;
        max-lease-time 7200;
        option domain-name "local";
        option domain-name-servers 114.114.114.114, 114.114.115.115;
    }

修改/etc/default/isc-dhcp-server配置檔案

  • 更改INTERFACESv4並註釋INTERFACESv6
    INTERFACESv4="wlan0"
    #INTERFACESv6=""
  • 關閉wlan0介面
    sudo ifdown wlan0

修改/etc/network/interfaces配置檔案

  • 將下面行放到檔案末尾
    auto lo
    iface lo inet loopback
    iface eth0 inet dhcp
    allow-hotplug wlan0
    iface wlan0 inet static
    address 192.168.42.1
    netmask 255.255.255.0
    post-up iw dev $IFACE set power_save off
  • 強行更改ip
    sudo ifconfig wlan0 192.168.42.1

建立/etc/hostapd/hostapd.conf配置檔案設定wifi

  • 寫入如下行
    interface=wlan0
    ssid=wifiname
    hw_mode=g
    channel=6
    macaddr_acl=0
    auth_algs=1
    ignore_broadcast_ssid=0
    wpa=2
    wpa_passphrase=password
    wpa_key_mgmt=WPA-PSK
    wpa_pairwise=TKIP
    rsn_pairwise=CCMP

修改/etc/sysctl.conf配置檔案將pppoe與wifi連線起來實現wifi上網

  • 在檔案最後加上
    net.ipv4.ip_forward=1
  • 刷一下iptables
    sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
    sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
    sudo iptables -A FORWARD -i ppp0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
    sudo iptables -A FORWARD -i wlan0 -o ppp0 -j ACCEPT
  • 加入啟動項全家通
    sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
  • 最後在檔案/etc/network/interfaces後加入如下行
    up iptables-restore < /etc/iptables.ipv4.nat

最後

sudo service hostapd start
sudo service isc-dhcp-server start
sudo reboot

相關文章