How can I restart my Wi-Fi connection from the command-line?

nmcli is very useful command-line utility for interacting with Network Manager. Use this command in Ubuntu 16.04 LTS

nmcli radio wifi off && sleep 5 && nmcli radio wifi on

For versions prior to 15.10 ( i.e. before transition to systemd ) the command would be slightly different:

nmcli nm wifi off && sleep 5 && nmcli nm wifi on

Good thing about it - this doesn't require root powers.

Restarting network manager itself is a good idea as well.

For 16.04 LTS:

sudo systemctl restart NetworkManager

and for 14.04 LTS:

sudo service network-manager restart

And if we really wanted to, we could even automate it with a script that will restart your wifi.

#!/bin/bash
# replace wlan0 with your device name
# as given by ip addr or ifconfig
while true 
do
    # keep checking if we have ip address    
    wifi_info=$(ip -4 -o addr  show wlan0 )
    while [ -n "$wifi_info" ];
    do
       wifi_info=$(ip -4 -o addr  show wlan0 )
       sleep 0.25
    done

    # We get here only if IP address is lost
    # which means we're off-line
    # restart wifi 
    nmcli radio wifi off && sleep 5 && nmcli radio wifi on
done

Two alternatives I'm thinking about follows,

First alternative, bring down/up the interface

ifconfig wlan0 down ## assumes your wlan is named wlan0
ifconfig wlan0 up

Second alternative, restart the entire network-manager. This assumes you have network-manager installed. If not, install it with the following cmd in your terminal sudo apt-get install network-manager.

sudo service network-manager restart

On Ubuntu 15.10 and 16.04 LTS you can use the systemd feature:

 systemctl restart NetworkManager.service