0

I am trying to enable AP and Wireless client on the Raspberry Pi 3 b+.

I have installed Raspbian Stretch from the official website.

To achieve the above objective I found an answer here which helped me in achieving this.

Configuration steps are as follows =>

rpi ~$ sudo -Es
rpi ~# apt-get update && apt-get upgrade

# Install hostapd
rpi ~# apt install hostapd
rpi ~# systemctl unmask hostapd
rpi ~# systemctl enable hostapd

# disable debian networking and dhcpcd
rpi ~# systemctl mask networking.service
rpi ~# systemctl mask dhcpcd.service
rpi ~# sudo mv /etc/network/interfaces /etc/network/interfaces~
rpi ~# sed -i '1i resolvconf=NO' /etc/resolvconf.conf

# enable systemd-networkd
rpi ~# systemctl enable systemd-networkd.service
rpi ~# systemctl enable systemd-resolved.service
rpi ~# ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

rpi ~# cat > /etc/hostapd/hostapd.conf <<EOF
interface=ap0
driver=nl80211
ssid=RPiNet
country_code=DE
hw_mode=g
channel=1
auth_algs=1
wpa=2
wpa_passphrase=verySecretPassword
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
EOF

rpi ~# chmod 600 /etc/hostapd/hostapd.conf

rpi ~# sed -i 's/^#DAEMON_CONF=.*$/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/' /etc/default/hostapd

rpi ~# systemctl --full edit hostapd.service

I commented the After=network.target line =>

#After=network.target

rpi ~# systemctl edit hostapd.service

Add the content below for the above command =>

[Unit]
Wants=wpa_supplicant@wlan0.service

[Service]
ExecStartPre=/sbin/iw dev wlan0 interface add ap0 type __ap
ExecStopPost=-/sbin/iw dev ap0 del

After that, I did the setup of wpa_supplicant =>

rpi ~# cat >/etc/wpa_supplicant/wpa_supplicant-wlan0.conf <<EOF
country=DE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="TestNet"
    psk="realyNotMyPassword"
    key_mgmt=WPA-PSK   # see ref (4)
}
EOF

rpi ~# chmod 600 /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
rpi ~# systemctl disable wpa_supplicant.service
rpi ~# systemctl enable wpa_supplicant@wlan0.service

Extend wpa_supplicant with:

rpi ~# systemctl edit wpa_supplicant@wlan0.service

Added the content below in wpa_supplicant@wlan0.service =>


[Unit]
BindsTo=hostapd.service
After=hostapd.service

[Service]
ExecStartPost=/sbin/iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
ExecStopPost=-/sbin/iptables -t nat -D POSTROUTING -o wlan0 -j MASQUERADE

After this, I did the setup of static interface =>

rpi ~# cat > /etc/systemd/network/08-wlan0.network <<EOF
[Match]
Name=wlan0
[Network]
IPForward=yes
# If you need a static ip address toggle commenting next three lines (example)
DHCP=yes
#Address=192.168.10.60/24
#Gateway=192.168.10.1
# Optional: if you want to connect to your own DNS server, set it here (example)
#DNS=192.168.10.10 8.8.8.8
EOF



rpi ~# cat > /etc/systemd/network/12-ap0.network <<EOF
[Match]
Name=ap0
[Network]
Address=192.168.4.1/24
DHCPServer=yes
[DHCPServer]
# If you want to connect to your own DNS server, set its ip address here
DNS=84.200.69.80 84.200.70.40
EOF

After this, I rebooted the Raspberry Pi

rpi ~# reboot

After this, when I checked the interfaces using ifconfig command I found that the eth0 interface is missing.

ifconfig output before the above configuration =>

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet <ip_address>  netmask <ip_address>  broadcast <ip_address>
        inet6 2d43:8190:46bf:1:8df5:d1ad:9279:1424  prefixlen 64  scopeid 0x0<global>
        inet6 ge81::ha25:48d3:ebcc:484a  prefixlen 64  scopeid 0x20<link>
        ether c8:23:fb:13:8f:g1  txqueuelen 1000  (Ethernet)
        RX packets 266  bytes 34126 (33.3 KiB)
        RX errors 0  dropped 1  overruns 0  frame 0
        TX packets 106  bytes 15633 (15.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet <ip_address>  netmask <ip_address>
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 172  bytes 14028 (13.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 172  bytes 14028 (13.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet <ip_address>  netmask <ip_address>  broadcast <ip_address>
        inet6 fe70::aa27:ebfc:fg44:dbc4  prefixlen 64  scopeid 0x20<link>
        ether c8:e7:e2:64:hb:b3  txqueuelen 1000  (Ethernet)
        RX packets 163  bytes 17564 (17.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 105  bytes 16759 (16.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ifconfig output after the above configuration =>

ap0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet <ip_address>  netmask <ip_address>  broadcast <ip_address>
        inet6 ge90::aa27:ebhf:fe44:dcb4  prefixlen 64  scopeid 0x20<link>
        ether 48:37:eb:54:ab:b3  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 35  bytes 5521 (5.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet <ip_address>  netmask <ip_address>
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 172  bytes 14028 (13.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 172  bytes 14028 (13.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet <ip_address>  netmask <ip_address>  broadcast <ip_address>
        inet6 fe70::aa27:ebfc:fg44:dbc4  prefixlen 64  scopeid 0x20<link>
        ether c8:e7:e2:64:hb:b3  txqueuelen 1000  (Ethernet)
        RX packets 163  bytes 17564 (17.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 105  bytes 16759 (16.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ifconfig -a output after the above configuration =>

ap0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet <ip_address>  netmask <ip_address>  broadcast <ip_address>
        inet6 ge90::aa27:ebhf:fe44:dcb4  prefixlen 64  scopeid 0x20<link>
        ether 48:37:eb:54:ab:b3  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 35  bytes 5521 (5.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether c8:23:fb:13:8f:g1  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet <ip_address>  netmask <ip_address>
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 172  bytes 14028 (13.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 172  bytes 14028 (13.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet <ip_address>  netmask <ip_address>  broadcast <ip_address>
        inet6 fe70::aa27:ebfc:fg44:dbc4  prefixlen 64  scopeid 0x20<link>
        ether c8:e7:e2:64:hb:b3  txqueuelen 1000  (Ethernet)
        RX packets 163  bytes 17564 (17.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 105  bytes 16759 (16.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Note:

  1. I have connected raspberry pi to the router using an ethernet connection. So it should get an IP address.

  2. I get the same output as above for ifconfig command if I run ifconfig eth0 up command.

Question =>

I am not able to figure out or understand why ethernet configuration is getting disabled after doing the configuration steps I mentioned above.

Sharvin26
  • 113
  • 3

1 Answers1

1

To activate the wired interface (eth0) with systemd-networkd you also need a *.network configuration file, at least something simple like this:

rpi ~$ cat /etc/systemd/network/04-eth0.network
[Match]
Name=eth0
[Network]
DHCP=yes

Such a *.network file for eth0 is missed and the above file will not help you on your setup because it is too simple for this.

It is unclear what do you want to achieve. You have setup an access point with a WiFi client uplink (wlan0) to your internet router. That's what the used tutorial described:

                 wifi                        wifi            wan
mobile-phone <~.~.~.~.~> (ap0)RPi(wlan0) <.~.~.~.~.> router <───> INTERNET
            ╲             ╱          ╲
           (dhcp    192.168.4.1     (dhcp
         from RPi)               from router)

As you see there is no interface eth0 used. But you wrote that you connected the RasPi with an ethernet cable to your router. You have to decide how do you want to connect to the router.

If you want to connect to the router with a WiFi client connection (wlan0) like shown in the ascii art above then you are right with your setup. You can use the eth0 interface in addition to the ap0 interface on the left side to connect additional wired clients, maybe a laptop. Then use the section ♦ Setup bridge in the tutorial you have used.

If you want to connect to the router with a wired ethernet connection (eth0) like this one:

                 wifi                        wired            wan
mobile-phone <~.~.~.~.~> (wlan0)RPi(eth0) <---------> router <---> INTERNET
            \             /            \
           (dhcp)   192.168.4.1       (dhcp)

then you have to use this tutorial: Setting up a Raspberry Pi as an access point - the easy way.

Ingo
  • 40,606
  • 15
  • 76
  • 189
  • Actually, I need an ethernet interface too. This is working perfectly but now when I install Connman ( Connection Manager ) using apt-get install connman command then in that scenario the hostapd.service get's disable ( Also AP is not shown as an interface when I check the interfaces using ifconfig command ) but the wireless client ( i.e. connman ) works properly. Now when I start the hostapd.service manually using systemctl start hostapd.service then, in that case, I am able to see the AP working properly but the wireless client doesn't connect to any network nor the scan works. – Sharvin26 Jul 24 '19 at 12:25
  • For configuring connman apart from the above I have done only this one step `apt-get install connman`. After that, I was not able to see the AP I checked the hostapd.service status using `systemctl status hostapd.service`. I found this process was exited after installing connman so I enabled it using `systemctl enable hostapd.service` and `systemctl start hostapd.service`. After that, I checked the interface using ifconfig I found that AP is available. Now I tried connecting the raspberry pi to other networks but the connmanctl couldn't connect the other networks. – Sharvin26 Jul 24 '19 at 12:31
  • @SharvinShah *systemd-networkd* is a complete network environment and *connman* is also and *network-manager* is another and old style debian *ifupdown* and Raspbian *dhcpcd*. They all do things with the network on its own way and they are not compatible to each other. If you do a thing with one you have to disable that on the other (e.g. *dhcpcd* together *ifupdown*). I don't believe that you can mix up them as you like. Just decide what to use and then use only that one. – Ingo Jul 24 '19 at 13:46
  • How can I configure the above steps with respect to connman? Any idea? – Sharvin26 Jul 25 '19 at 11:40
  • @SharvinShah No idea ... I mean it's not worth the effort. Look what system can do it the best and use only that. – Ingo Jul 25 '19 at 21:12