You are not logged in.

#1 2018-10-23 19:17:01

Pablo Marcendo
Member
Registered: 2018-10-23
Posts: 1

Optionally starting hostapd at boot with systemd.

At home, my laptop is connected to the internet by an ethernet cable. To share this connection with my phone, I set up an access point using my laptop's wifi with the following script:

/usr/local/bin/enable-ap.sh
--------------------------------------------------------------------------------
#!/bin/bash
systemctl stop netctl-auto@wlp3s0

#Initial wifi interface configuration
ip link set up dev wlp3s0
ip addr add 192.168.178.50/24 dev wlp3s0

#Start dnsmasq
if [ -z "$(ps -e | grep dnsmasq)" ]
then
    dnsmasq
fi

#configure NAT
sysctl -w net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -o enp0s25 -j MASQUERADE
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i wlp3s0 -o enp0s25 -j ACCEPT

#start hostapd
hostapd -B /home/niek/.config/hostapd/hostapd.conf

which works fine.

When I'm not at home, I might want to connect to a wireless network instead, so I also created the reverse code:

/usr/local/bin/disable-ap.sh
--------------------------------------------------------------------------------
#stop hostapd
killall hostapd

sysctl -w net.ipv4.ip_forward=0
iptables -t nat -D POSTROUTING -o enp0s25 -j MASQUERADE
iptables -D FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -i wlp3s0 -o enp0s25 -j ACCEPT

killall dnsmasq

ip addr del 192.168.178.50/24 dev wlp3s0
ip link set down dev wlp3s0

systemctl start netctl-auto@wlp3s0

To switch between them I wrote a third script:

/usr/local/bin/toggle-ap.sh
--------------------------------------------------------------------------------
#!/bin/bash

if [ -e /usr/local/.ap-toggle ]
then
	disable-ap.sh
	rm /usr/local/.ap-toggle
else
	enable-ap.sh
	touch /usr/local/.ap-toggle
fi

So far no problems, but now I want the access point to start on boot if it was enabled before the laptop was shut down. I tried writing a systemd service

/etc/systemd/system/access-point.service
--------------------------------------------------------------------------------
[Unit]
Description=Enables the wifi access point if it is on.

[Service]
Type=idle
#ExecStartPre=/bin/sleep 60
ExecStart=/usr/local/bin/boot-ap.sh

[Install]
WantedBy=multi-user.target

which runs the following script:

/usr/local/bin/boot-ap.sh
--------------------------------------------------------------------------------
#!/bin/bash

if [ -e /usr/local/.ap-toggle ]
then
	enable-ap.sh
fi

Sadly the access point does not get enabled on boot. Most steps of enable-ap.sh are successfully executed, but I have to manually start dnsmasq and hostapd to finish enabling the access point. Why does enable-ap.sh work when I call it myself, but not when a systemd service calls it?
Here is the output of journalctl -b:
https://pastebin.com/FAfu8uv2
I noticed row 1172:

okt 23 20:00:08 Arch-Niek dnsmasq[669]: exiting on receipt of SIGTERM

Why does dnsmasq recieve a SIGTERM?

Offline

#2 2019-01-02 08:10:27

bugsmanagement
Member
Registered: 2017-04-21
Posts: 201

Re: Optionally starting hostapd at boot with systemd.

Hello,

Out of curiosity, how about creating a bridge interface and leave dnsmasq running? As far as why SIGTERM, I suspect perhaps it's systemd killing dnsmasq though not to sure about my hypothesis.

Regards

Offline

#3 2019-01-02 08:48:08

Kirill Bugaev
Member
Registered: 2018-08-03
Posts: 173

Re: Optionally starting hostapd at boot with systemd.

@Pablo Marcendod, try to add `After=network.target`, `After=sys-subsystem-net-devices-wlp3s0.device` and `After=sys-subsystem-net-devices-enp0s25.device` strings in [Unit] section of your access-point.service.
Something like this:

/etc/systemd/system/access-point.service
--------------------------------------------------------------------------------
[Unit]
Description=Enables the wifi access point if it is on.
After=network.target
After=sys-subsystem-net-devices-wlp3s0.device
After=sys-subsystem-net-devices-enp0s25.device

[Service]
Type=idle
#ExecStartPre=/bin/sleep 60
ExecStart=/usr/local/bin/boot-ap.sh

[Install]
WantedBy=multi-user.target

Last edited by Kirill Bugaev (2019-01-02 12:07:00)

Offline

Board footer

Powered by FluxBB