NAVIGATE

RESOURCES

CHEATSHEETS

INFO

WiFi connection with command line

How to connect to a Wi-Fi network just by writing a bunch of lines (on Linux)

Installation

A few years ago, I used to do literally everything on the terminal (I have been an Arch Linux user for a long), even setting up an internet connection. Now I don't have the time anymore to lose my mind on this crap so I moved to a more user-friendly Linux distribution; however, I'm still doing stuff on terminal. Here you can find the procedure to connect through WiFi to the internet just by using the terminal.

Before starting with the commands you need to be sure that the following packets are installed

  • rfkill tool for enabling and disabling wireless devices
  • wpa_supplicant tool for performing authentication, enabling and disabling interface
  • dhcpcd tool that serves as DHCP client on your machine
  • iw tool to manipulate wireless devices and related configuration. This is not necessary for this article but install it anyway for any future circumstance

If not, please, install the latest versions for your Linux distribution. You should type a command like

0
sudo apt-get install rfkill wpa_supplicant dhcpcd iw

or

0
sudo pacman -S rfkill wpa_supplicant dhcpcd iw

Connection

Unblock all the aerial devices which means unblock all the WiFi, Bluetooth, IR hardware mounted on the motherboard of your laptop

0
rfkill unblock all

Set your wifi interface up

0
ip link set dev your_wifi_interface up

Register your passphrase for your APssid. You will enter a passphrase that is not covered by asterisks, so pay attention.

0
wpa_passphrase APssid >> /etc/wpa_supplicant.conf

After pressing enter, a prompt requiring the passphrase should appear. Type it and press enter again. Now, you can connect to the AP by specifying the drivers to use and the configuration file generated in the previous passage

0
wpa_supplicant -D drivers -i your_wpa_interface -c /etc/wpa_supplicant.conf

Obtain an IP using DHCPCD supplicant program

0
dhcpcd your_wpa_interface

If everything went fine, now you should be connected to the desired WiFi network and do everything you want on the Internet.

wificonn: a ready-to-use tool

To speed up the connection every time I turned on my laptop, I wrote a very simple tool that helped me to do everything very fast. Below you can find the C code. After you compile it, you can place it in a bin folder so that the command is easily available as soon as you open the terminal. Moreover, you can set a bash alias so that the connection starts just by typing, let's say, 'conn'.

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DIM 20

void help(void);
void start(char* interface, char* ap, char* security);
void stop(char* interface);
void restart(char* interface, char* ap, char* security);
void list(char* interface);
void create_new(void);

// Arg0: Program name
// Arg1: Action to do
// Arg2: Interface to connect with
// Arg3: AP to connect to
int main(int argc, char* argv[]){
	char action[DIM+1];
	char interface[DIM+1];
	char ap[DIM+1];
	char security[DIM+1];

	// First usage error
	if(argc==1){
		printf("Type 'wificonn help' to obtain help.\n");
		return 1;
	}

	// Parse when number of arguments is 2
	if(argc==2){
		if(!strcmp(argv[1],"help")){
			help();
                        return 1;
	       	}
		else if(!strcmp(argv[1],"new")){
		 	create_new();
			return 0;
		}
		else{
			printf("Invalid command. Type 'wificonn help' to get a help message. \n");
		  	return 1;
		}
	}

	// Check on the number of parameters
	if(argc==4 || argc>5){
		printf("Invalid number of parameters.\n");
		help();
		return 1;
	}

	// Parameters length check
	if(argc==2 && strlen(argv[1])>DIM){
		printf("Argument too long.\n");
		return 1;
	}
	if(argc==3 && (strlen(argv[1])>DIM || strlen(argv[2])>DIM)){
		printf("Argument too long.\n");
		return 1;
	}
	if(argc==4 && (strlen(argv[1])>DIM || strlen(argv[2])>DIM || strlen(argv[3])>DIM)){
		printf("Argument too long.\n");
		return 1;
	}
	if(argc==5 && (strlen(argv[1])>DIM || strlen(argv[2])>DIM || strlen(argv[3])>DIM || strlen(argv[4])>DIM)){
		printf("Argument too long.\n");
		return 1;
	}


	// Execute command normally
	strncpy(action,argv[1],DIM);
	if(argc>=3) strncpy(interface,argv[2],DIM);
	if(argc>=4) strncpy(ap,argv[3],DIM);
	if(argc>=5) strncpy(security,argv[4],DIM);
	if(!strcmp(action,"start")){
		start(interface,ap,security);
		return 0;
	}
	if(!strcmp(action,"stop")){
		stop(interface);
		return 0;
	}
	if(!strcmp(action,"restart")){
		restart(interface,ap,security);
		return 0;
	}
	if(!strcmp(action,"list")){
		list(interface);
		return 0;
	}
	if(!strcmp(action,"new")){
		create_new();
		return 0;
	}

	printf("Invalid command. Type 'wificonn help' to get a help message.\n");
	return 0;
}

void help(void){
	printf("Generic usage:\n");
	printf("wificonn <action> <interface> <APname> <security>\n");
	printf("where:\n");
	printf("\t<action>\tSelect the action to do: start, stop, restart, new, list.\n");
	printf("\t<interface>\tInterface to connect with. To  be used only with start, restart or list.\n");
	printf("\t<APname>\tName of the AP to connect to. To  be used only with start or restart.\n");
	printf("\t<security>\tType of connection: wpa or open. To  be used only with start or restart.\n\n");
	printf("Usage example:\n");
	printf("wificonn start my_wifi_interface my_AP open\n");
	printf("wificonn start wlp1s0 superWIFI wpa\n");
	return;
}

void start(char* interface, char* ap, char* security){
	char command[100];
	printf("****** Turning on hardware interface %s...\n",interface);
	sprintf(command,"ip link set %s up",interface);
	system(command);
	if(strcmp(security,"wpa")==0){
		printf("****** Connecting to WPA network %s...\n",ap);
		sprintf(command,"wpa_supplicant -i %s -c /etc/wpa_supplicant/%s_supplicant.conf -B",interface,ap);
		system(command);
	}
	else if(strcmp(security,"open")==0){
		printf("****** Connecting to open network %s...\n",ap);
		sprintf(command,"iw dev %s connect %s",interface,ap);
		system(command);
	}
	printf("****** Getting IP...\n");
	sprintf(command,"dhcpcd %s",interface);
	system(command);
	printf("****** Connection established.\n");
	return;
}

void stop(char* interface){
	char command[60];
	printf("****** Stopping connection...\n");
	sprintf(command,"ip link set %s down",interface);
	system(command);
	printf("****** Killing orphan processes...\n");
	system("pkill wpa_supplicant");
	system("pkill dhcpcd");
	printf("****** Connection stopped.\n");
	return;
}

void restart(char* interface, char* ap, char* security){
	stop(interface);
	printf("\n****** Restarting connection...\n\n");
	start(interface,ap,security);
	return;
}

void create_new(void){
	char ap[40];
	char password[40];
	char command[200];

	printf("****** Create new configuration file for a wifi hotspot\n");
	printf("Insert the hotspot name: ");
	fgets(ap,39,stdin);
	printf("Insert the password: ");
	fgets(password,39,stdin);
	char *space = strstr(ap,(char*)' ');
	if(space!=NULL){
		*space = '_';
	}
	sprintf(command,"wpa_passphrase %s %s >> '/etc/wpa_supplicant/%s_supplicant.conf'",ap,password,ap);
	system(command);
	printf("****** Configuration successfully terminated.\n");
	return;
}

void list(char* interface){
	char command[40];
	sprintf(command,"ip link set %s up",interface);
	system(command);
	sprintf(command,"iw dev %s scan | grep SSID",interface);
	system(command);
	//sprintf(command,"ip link set %s down",interface);
	//system(command);
	return;
}

Common troubles

Is your hardware interface active?

First of all, check if your WiFi hardware is available and active by typing

0
rfkill list all

If you see the desired physical interface in the list and it is marked as blocked via software then type

0
rfkill unblock all

Pay attention: rfkill command can unblock devices only via software. If your device is not properly connected or blocked via hardware (using a button or a switch), then enable it before doing any other action on the terminal.

SuperUser required

Sometimes, the systems you work on allow you to connect to WiFi (to execute some of the installed software) only if you are logged as SuperUser. If so, please pay attention to what you do since you are going to gain all the privileges on that machine, then, when you are completely aware, type

0
su

Type the password and repeat all the passages described in the previous paragraph.

Is your hardware interface enabled?

If something is not working, probably you forgot to enable the interface. Type

0
ip addr

or

0
ip link

In both cases you should see some lines similar to the following one (in this example the name of the hardware interface is wlp1s0):

0
2: wlp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000

If there is no LOWER_UP indication between the brackets, maybe you forgot to type

0
ip link set wlp1s0 up

NOTE: this is different from unblocking the physical interface; a blocked interface is completely off while a disabled interface is on but in some sort of standby mode. Your hardware must be both unlocked and enabled to work properly.

Are paths specified correctly?

Maybe there is just a typo or another sort of similar problem so check that, when invoking wpa_supplicant, you are passing the right wpa_supplicant.conf file, in the right location.

Do you have the right drivers installed?

Sometimes people forget that a hardware device needs a driver to work properly, so, let's understand which network controller your computer is using. Type

0
lspci -k

You should see a list of hardware devices mounted on your motherboard, with the driver in use specified for each of them. Look for something like "Network controller", then read the item "Kernel driver in use". If the voice is null or void or something else, then you missed to install the driver. Here is a brief list of common drivers you could need, according to your chipset.

  • Atheros pacman -S ath9k
  • Broadcom pacman -S b43
  • Qualcom-Atheros pacman -S wcn36xx

Check the Linux drivers webpage to know which is your driver, then install it with your packet manager.

Share this page

Whatsapp Facebook LinkedIn Reddit Twitter Mail

Comments

Please, remember to always be polite and respectful in the comments section. In case of doubts, read this before posting.

Posted comments ⮧

Comment section still empty.

INDEX


INFO

vAuthor: Vanadium
vLast Mod: 2024-01-05

STATISTICS

VViews: 212
UpVote: 8
DownVote: 2

CONTACTS


SHARE

Whatsapp Facebook LinkedIn Reddit Twitter Mail

If you liked

🍵
♥