Setting up a Raspberry Pi 3 as a access point and a router
1. Set Up WiFi Access Point
- Install Packages
$ sudo apt-get install hostapd dnsmasq
hostapd
turns wlan0 into an access point, and Dnsmasq
plays an role of DHCP and DNS.
- Set Static IP Address
First, setdhcp
invalid in wlan0
$ sudo vi /etc/dhcpcd.conf
edit as follows:
~~dhcpcd.conf~~ denyinterfaces wlan0
Next, we need to set a static IP address for the WiFi interface.
$ sudo cp /etc/network/interfaces /etc/network/interfaces_org $ sudo vi /etc/network/interfaces
At the bottom, add the following:
~~interfaces~~ allow-hotplug wlan0 iface wlan0 inet static address 172.20.5.1 netmask 255.255.255.0 network 172.20.5.0 broadcast 172.20.5.255
- Configure Hostapd
$ sudo vi /etc/hostapd/hostapd.conf
enter the folloing into that file:
~~hostapd.conf~~ interface=wlan0 driver=nl80211 hw_mode=g channel=5 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 ieee80211ac=0 wmm_enabled=1 ieee80211d=1 ieee80211h=1 country_code=JP local_pwr_constraint=3 spectrum_mgmt_required=1 wpa=3 wpa_key_mgmt=WPA-PSK ssid=raspberrypi wpa_passphrase=**********
You can set ssid, passphrase, and it is better to select a channel which is not interfered by other interfaces.
Test your configuration file works good.
$ sudo hostapd /etc/hostapd/hostapd.conf
If it runs successfully, you can check ssid by other machine.
Then, we make this configuration enable by telling its place to hostapd.
$ sudo vi /etc/default/hostapd
Uncomment # and replace the line #DAEMON_CONF=""
with:
DAEMON_CONF="/etc/hostapd/hostapd.conf"
Set hostapd
runs automatically
$ sudo systemctl unmask hostapd.service $ sudo systemctl enable hostapd.service
- Configure Dnsmasq
Edit Dnsmasq configuration
$ sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf_org $ sudo vi /etc/dnsmasq.conf
paste in the text below
~~dnsmasq.conf~~ interface=wlan0 listen-address=172.20.5.1 bind-interfaces server=8.8.8.8 domain-needed bogus-priv dhcp-range=172.20.5.100,172.20.5.200,24h
Then restart the Raspberry Pi
$ sudo reboot
Now you can use Raspberry Pi as a Access Point.
2. Enable Packet Forwarding
-Configure NAT(Network Address Translation)
Edit /etc/sysctl.conf: uncomment net.ipv4.ip_forward=1
by deleting #.
$ sudo vi /etc/sysctl.conf
~~sysctl.conf~~ net.ipv4.ip_forward=1
Next, configure NAT(Network Address Translation).
$ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE $ sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT $ sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
After reboot, Raspberry Pi will revert back to its previous state. So it is needed to save and restore these settings.
$ sudo sh -c "iptables-save > /etc/iptables.ipv4.nat" $ sudo vi /etc/rc.local
In rc.local
script, just above the exit 0
add the following
iptables-restore < /etc/iptables.ipv4.nat
That's all for configuration.
Restart Raspberry Pi to test the setting works.
$ sudo reboot
Reference
Setting up a Raspberry Pi 3 as an Access Point - SparkFun Learn
Connecting PC(Ubuntu 18.10) to RaspberryPi3(Raspbian stretch) via ethernet cable , setting static IP addresses
PC(Ubuntu18.10) と RaspberryPi3を有線LANでつなぐ
APIPA does not automatically assign IP adresses in Ubuntu, so I tried to set static IP adresses.
1. PC側
$ ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp0s25: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000 link/ether 68:f7:28:88:05:29 brd ff:ff:ff:ff:ff:ff
enp0s25に固定ipアドレスを付与する。
/etc/NetworkManager/system-connectionsディレクトリに構成ファイルを作成する。
↑は既知のネットワークを記述しておく場所なのでこれではうまく行かない。
リンクローカル用yamlファイルを作成し、固定ipアドレスを設定する。
$ sudo vi /etc/netplan/01-netcfg-linklocal-staticIP.yaml
# Let NetworkManager manage all devices on this system network: version: 2 renderer: networkd ethernets: enp0s25: dhcp4: false addresses: [10.10.10.20/24]
2. Raspberry Pi側
$ ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether b8:27:eb:c2:cb:cf brd ff:ff:ff:ff:ff:ff 3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether b8:27:eb:97:9e:9a brd ff:ff:ff:ff:ff:ff inet 192.168.3.11/24 brd 192.168.3.255 scope global wlan0 valid_lft forever preferred_lft forever inet6 2400:2410:ae60:fe00:7a68:da9a:9599:83bd/64 scope global mngtmpaddr noprefixroute dynamic valid_lft 86062sec preferred_lft 14062sec inet6 fe80::1953:30df:7d21:9686/64 scope link valid_lft forever preferred_lft forever
eh0に固有ipアドレスを設定する。 /etc/dhcpcd.confに以下のように記述
$ sudo vi /etc/dhcpcd.conf
interface eth0 static ip_address=10.10.10.10/24 static routers= static domain_name_servers= static domain_search=
設定を読み込ませて反映されるか確認
$ sudo /etc/init.d/dhcpcd restart
以上で両方の固有ipアドレスが設定された。
3. 通信の確認
pingを通してみる
$ ping 10.10.10.10 PING 10.10.10.10 (10.10.10.10) 56(84) bytes of data. 64 bytes from 169.254.158.85: icmp_seq=1 ttl=64 time=0.437 ms 64 bytes from 169.254.158.85: icmp_seq=2 ttl=64 time=0.411 ms 64 bytes from 169.254.158.85: icmp_seq=3 ttl=64 time=0.481 ms 64 bytes from 169.254.158.85: icmp_seq=4 ttl=64 time=0.477 ms 64 bytes from 169.254.158.85: icmp_seq=5 ttl=64 time=0.398 ms 64 bytes from 169.254.158.85: icmp_seq=6 ttl=64 time=0.374 ms ^C --- 10.10.10.10 ping statistics --- 6 packets transmitted, 6 received, 0% packet loss, time 118ms rtt min/avg/max/mdev = 0.374/0.429/0.481/0.046 ms
機器間での通信がされていることがわかる。