13

What is the terminal command to turn Airplane Mode on/off in Ubuntu?

Is it simply sudo rfkill block all and sudo rfkill unblock all? I know that this will disable all wireless modules in the computer, but will this be noticed by the system, so that Airplane Mode is toggled off/on in network settings?

3
  • Try sudo rfkill unblock wifi
    – Mitch
    Sep 21, 2013 at 9:57
  • @Mitch Do we really need root privileges to climb in an airplane? Sep 21, 2013 at 10:54
  • @RaduRădeanu With all the security issues at airports sure :) lol
    – Mitch
    Sep 21, 2013 at 11:18

5 Answers 5

10

Running the following command in terminal:

gnome-control-center network

will open a window for network management which should be similar with:

Airplane mode on

You can observe that at this moment the "Airplane Mode" is off and the wireless is on.

Now, without to close this window, run the following command in terminal:

nmcli nm wifi off

The above window will be changed automatically to:

Airplane mode on

As you can see, now "Airplane Mode" is on and the wireless is off.

Running, again in terminal, the following command:

nmcli nm wifi off

will turn "Airplane Mode" off and wireless on again.

So, you don't need rfkill (which need also root privileges) to toggle "Airplane Mode" via terminal.

nmcli (see also man nmcli) it's enough and can be executed by any usual user... You don't need root privileges to climb in an airplane :)).

4
  • 1
    nmcli nm wifi off does not turn off airplane mode unless bluetooth is off.
    – kzh
    Dec 28, 2014 at 17:27
  • 1
    For 15.04 it would be: nmcli r all off and nmcli r all on. Or to include bluetooth: rfkill block bluetooth & rfkill block wlan and rfkill unblock bluetooth & rfkill unblock wlan .
    – VRR
    Jul 26, 2015 at 12:42
  • For 15.04 (with updates) I found this to work best: alias wifitoggle='nmcli r wifi off ; sleep 1 ; nmcli r wifi on' Oct 20, 2015 at 23:51
  • Thank you! I was having the same problem. Turns out my laptop was in "flight" mode, but the light indicator wasn't lit... Oct 15, 2019 at 8:48
4

For Ubuntu 18.04:

nmcli r wifi on turns airplane mode off, and the converse sets it on.

A simple bash script to toggle airplane mode on or off is below; save it to file and set its execute bit in properties.

#!/bin/bash
wifi="$(nmcli r wifi | awk 'FNR = 2 {print $1}')"
if [ "$wifi" == "enabled" ] 
then
    nmcli r wifi off
else
    nmcli r wifi on
fi
3
  • The script has 3 errors. First you do not need to test FNR==2 because the script returns only one line (no headers). Second if you want to test FNR, then you have to write FNR == 2. Third the backquote on the line testing the variable wifi has to disappear. Oct 24, 2020 at 14:00
  • @RudyVissers, thnaks for catching the errors inserted when a third party "edited" this answer. As for the script working, it is currently functioning on a PC with Ubuntu 18,04 in the original format, which I've reinstated. Oct 25, 2020 at 1:21
  • It is working because nmcli r wifi does not return a header and FNR = 2 has no effect. You can remove FNR = 2 and your script will continue to work. Oct 26, 2020 at 6:06
3

By combining answers in different threads, I got it working on Ubuntu 20.04

@Rudy's above and this: https://askubuntu.com/a/1144599/806813

#!/bin/bash

radio="$(nmcli radio all | awk 'FNR == 2 {print $2}')"

if [ "$radio" = "enabled" ]
 then
    nmcli radio all off
else
    nmcli radio all on
fi

if rfkill list bluetooth | grep -q 'yes$' ; then
    rfkill unblock bluetooth
else
    rfkill block bluetooth
fi

Assigned to Alt-A for me but that is personal choice.

Mind you I already have the built in WiFi adapter on my Lenovo T420 disabled because I am using an Asus USB Wifi adapter which this toggles on and off along with the Bluetooth.

2
  • That works like a charm in Ubuntu 22.04. Thanks!
    – piotr.d
    Sep 7, 2022 at 18:43
  • and additionally it shows the reassuring airplane icon in top bar.
    – pierrely
    Jan 2, 2023 at 3:23
2

Tested on 20.04.1 LTS. Let's disable all radio transmissions:

rudy@nbu130-rudy:~/bin$ pwd
/home/rudy/bin

rudy@nbu130-rudy:~/bin$ ./airplane_toggle 

rudy@nbu130-rudy:~/bin$ nmcli radio all 
WIFI-HW  WIFI     WWAN-HW  WWAN    
enabled  enabled  enabled  enabled 

rudy@nbu130-rudy:~/bin$ ./airplane_toggle 

rudy@nbu130-rudy:~/bin$ nmcli radio all 
WIFI-HW  WIFI      WWAN-HW  WWAN     
enabled  disabled  enabled  disabled 

rudy@nbu130-rudy:~/bin$ cat airplane_toggle 
#!/bin/bash
radio="$(nmcli radio all | awk 'FNR == 2 {print $2}')"
if [ "$radio" == "enabled" ]
 then
  nmcli radio all off
else
 nmcli radio all on
fi

It is even possible to assign the command '/home/rudy/bin/airplane_toggle' to a shortcut (tested).

1
  • This turns wifi off and on on Ubuntu 20.04 but not bluetooth that is already on Jan 12, 2021 at 2:27
1

On Debian- and Arch-based distros, inspired by the previous code, this will disable WiFi and Bluetooth and send a notification:

#!/bin/bash
wifi="$(nmcli r wifi | awk 'FNR = 2 {print $1}')"
if [ "$wifi" == "enabled" ]; then
    rfkill block all &
    notify-send 'Mode avion: actif'
else
    rfkill unblock all &
    notify-send 'Mode avion: inactif'
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .