Friday, January 06, 2017

WIFI script for systemd init systems


I used the following Steps to create a startup script that will automatically connect my WIFI after a reboot. Not sure if there are more efficient way but this works for me so far. I normally do minimum install so there's no GUI running on my Linux machine. Tested it on Ubuntu 16.10 Yakkety.

Pre requisite
a. Disable network-manager service.
b. You need root access.



1. Create wifi WPA supplicant config appropriate for your Wifi Connection. The example was based on using WPA-Personal (Security Mode).

vi  /etc/wpa_supplicant.conf
ctrl_interface=/var/run/wpa_supplicant

network={
        ssid="WIFI SSID (ex FreeWIFI)"
        psk="wifipassword"
        key_mgmt=WPA-PSK
        proto=RSN WPA
        pairwise=CCMP TKIP
        group=CCMP TKIP
}


2. Create the script that will contains the CLI command that connects the wifi

vi /*path*/wifi.sh
#!/bin/bash
/sbin/wpa_supplicant -Dnl80211 -iwlp2s0 -c/etc/wpa_supplicant.conf &

------
*Change the *path* to a folder of your choice (do the same on Step 3)
wlp2s0 - is the name of your wifi interface name. Run "ip addr sh" if you're not sure.


3. Create the systemd  init script file

vi /etc/systemd/system/wifi.service
[Unit]
Description=This will and connect the wifi @ startup

[Install]
WantedBy=multi-user.target

[Service]
Exec=/*path*/wifi.sh
Type=oneshot
RemainAfterExit=yes


4. ADD the systemd init script during Startup using systemctl:

systemctl daemon-reload
systemctl enable wifi.service
systemctl start wifi.service


5. Reboot your system to test if script works

reboot

==================================================

On second note just run nmtui if your network manager is working fine will save you the trouble of manually creating the wpa_supplicant file. The tutorial above works best if you're only connecting to 1 wifi connection and you don't expect the linux box or server to be more elsewhere.

No comments: