Write a bash script to configuring basic network connectivity

Update on:
May 17, 2021

In this article, we are going to configure basic network connectivity using wpa_supplicant.


Besides having a terminal open, we need to remember a few concepts:

  • Check whether wpa_supplicant is installed or not.
  • You should know the SSID and password.
  • Remember that you have to run your program as a root user.

How to do it

Create a script called wifi_conn.sh and write the following code in it:

ifdown wlan0 
rm /etc/network/interfaces 
touch /etc/network/interfaces 
echo 'auto lo' >> /etc/network/interfaces 
echo 'iface lo inet loopback' >> /etc/network/interfaces 
echo 'iface eth0 inet dhcp' >> /etc/network/interfaces 
echo 'allow-hotplug wlan0' >> /etc/network/interfaces 
echo 'iface wlan0 inet manual' >> /etc/network/interfaces 
echo 'wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf' >> /etc/network/interfaces 
echo 'iface default inet dhcp' >> /etc/network/interfaces 
rm /etc/wpa_supplicant/wpa_supplicant.conf 
touch /etc/wpa_supplicant/wpa_supplicant.conf 
echo 'ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev' >> /etc/wpa_supplicant/wpa_supplicant.conf 
echo 'update_config=1' >> /etc/wpa_supplicant/wpa_supplicant.conf 
wpa_passphrase $1 $2 >> /etc/wpa_supplicant/wpa_supplicant.conf 
ifup wlan0

How it works

Execute the script using sudo:

sudo bash wifi_conn.sh <SSID> <password>

Related Posts

Creating a config file and using it in tandem with your scripts

In this article, we are going to create a config file and use it in our shell script.PrerequisitesBesides having a terminal open, you need basic knowledge of creating scripts and config files.Write scriptNow, we are going to create a script and config file. The...

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

Using Bash to monitor battery life and optimize it

In this article, we will learn about the TLP Linux tool. TLP is a command-line tool; it is used for power management and will optimize the battery life.PrerequisitesBesides having a Terminal open, you need to ensure that you have TLP installed on your system.How to do...

Creating a simple NAT and DMZ firewall using bash script

In this article, we will create a simple NAT firewall with DMZ using iptables.PrerequisitesBesides having a Terminal open, you need to ensure that iptables is installed in your machine.Write scriptWe will write a script to set up a DMZ using iptables. Create...

Follow Us

Our Communities

More on BASH

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!



Submit a Comment

Your email address will not be published.

two × 5 =