I have a bash script that checks my network environment and mounts shares accordingly. If I'm at work and have a hard-wire connection to the LAN, then I'd like to activate a hotspot connection for the wireless devices in my office.

Here's the script:

# are we at home?
if [ "$(ping -q -c1" ]; then
  mount <home shares>
# well then are we on campus?
elif [ "$(ping -q -c1" ]; then
  mount <work shares>
  # if we're on campus, then are we connected to an ethernet cable?
  if [ "$(nmcli dev list iface eth0 | grep GENERAL.CONNECTION)" != "GENERAL.CONNECTION:                     not connected" ]; then
  nmcli con up uuid <uuid> iface wlan0

The nmcli command fails; there are several problems.

The nmcli tool doesn't seem to allow disconnecting a wireless connection arbitrarily. You can do nmcli con down but only if you can specify the <id> or <uuid>. In my environment wlan0 will connect to one of several wireless networks somewhat arbitrarily.

The nmcli dev disconnect works with a specified <iface> but seems to prevent reconnecting to anything except by manual selection. Reconnecting in a bash script doesn't seem to work.

The nmcli con up command (currently in the script above) doesn't seem to work if the <iface> has already established a connection.

Any suggestions on how to use nmcli to force a specified <uuid> connection when an unspecified <uuid> connection has already been established? Would something besides nmcli would work better?


  • What about cycling nm? nmcli nm enable false; nmcli nm enable true?
    – muru
    Commented Dec 3, 2014 at 13:22
  • 1
    This page could help: wiki.ubuntu.com/OnNetworkConnectionRunScript
    – datka
    Commented Sep 22, 2016 at 14:49
  • How about nmcli networking off;sleep 2;nmcli networking on
    – waltinator
    Commented Dec 19, 2020 at 21:32
  • @waltinator. thanks for that nmcli networking off, it works splendidly for me without sudo needed in 20.04.01 for off AND on, when I often have had to reboot to get the connection back up after using ifconfig down, as well as ifconfig giving me permission errors even though I had set privileges in sudooers for that and the script . I am using these for a script to disconnect when my tun0 from ifconfig goes down when VPN is lost.(their killswitch does not work). also changing usb ethernet gives different names for the connection so your way is best by far.
    – pierrely
    Commented Feb 15, 2021 at 6:47
  • as usual I spoke too soon, sometimes nmcli networking on does not bring up a disconnected eth0 connection ("it is the ubuntu way").
    – pierrely
    Commented Feb 16, 2021 at 6:03


You must log in to answer this question.

Browse other questions tagged .