Skip to main content

Command Palette

Search for a command to run...

How to Turn On Mobile Broadband Data Connection on Ubuntu 22.04 from the Terminal

Published
4 min read

In a nutshell, I have an older laptop equipped with a built-in mobile data connection. Currently, I've repurposed this laptop as a home lab and installed Ubuntu 22.04 LTS as the operating system. However, I've encountered an issue where the mobile data connection occasionally turns off on its own. To ensure a more reliable connection, I've been actively exploring ways to control the toggling of the mobile data, both on and off, through scripts or the terminal.

After conducting several internet searches, I discovered a solution using the nmcli command. nmcli is a powerful command-line tool designed for managing NetworkManager and providing detailed network status reports. It serves as a versatile alternative to graphical clients like 'nm-applet.' With nmcli, you can effortlessly create, display, edit, delete, activate, and deactivate network connections. Additionally, it provides comprehensive control and status information for network devices.

Step 1: Identify Your Mobile Broadband Interface

Before you proceed with configuring the mobile data connection, it's crucial to pinpoint the name of your mobile data connection interface. Keep in mind that this interface name may vary depending on your specific system and hardware configuration. To locate it, open a terminal and execute the following command:

nmcli connection show

This will display a list of all the network connections configured on your system.

Look for the active mobile broadband connection in the list. It will typically have a name like "Default" or something similar. To quickly identify it, look for "gsm" in the "TYPE" column of the output.

For example:

NAME               UUID                                  TYPE      DEVICE
Wired connection 1  12345678-1234-1234-1234-1234567890ab  ethernet  eth0
Default             98765432-5678-5678-5678-0987654321cd  gsm       wwan0  activated

In the above example, "Default" is the active mobile broadband connection. To ensure that the indicator you plan to use can successfully toggle your connection, you can test it with the following commands:

To turn off the connection:

nmcli connection down Default

And to turn it back on:

nmcli connection up Default

Remember that "Default" is the interface name associated with your mobile broadband connection. These commands should help you verify if your indicator is working correctly.

If the commands mentioned earlier are effective, the next step involves creating automated scripts to monitor and promptly reestablish the mobile data connection if it happens to turn off. This approach ensures that your laptop maintains a consistent and reliable internet connection.

Step 2: Create a Reconnection Script

To automate the process of checking and reconnecting your mobile broadband data connection, we'll create a simple shell script. Open your favorite text editor and create a new file. For example:

nano reconnect_mobile.sh

Now, paste the following code into the script:

#!/bin/bash

CONNECTION_NAME="YOUR_INTERFACE_NAME"

# Check if the mobile data connection is active
if nmcli connection show --active | grep -q "$CONNECTION_NAME"; then
    # Mobile data connection is already active, no need to log anything
    echo "Mobile data connection is already active"
    exit 0
else
    # Mobile data connection is not active. Reconnecting...
    echo "Mobile data connection is not active. Reconnecting..."
    nmcli connection up "$CONNECTION_NAME"

    # Check if the connection was successfully established
    if [ $? -eq 0 ]; then
        echo "Mobile data connection reestablished successfully."
    else
        echo "Failed to reconnect mobile data connection."
    fi
fi

Replace YOUR_INTERFACE_NAME with the actual name of your mobile broadband interface that you noted down in Step 1. Save the file and make it executable with the following command:

chmod +x reconnect_mobile.sh

Step 3: Run the Script

To manually run the script and reconnect your mobile broadband, execute:

sudo ./reconnect_mobile.sh

If your mobile data connection is down, the script will attempt to bring it back up and restore your internet access. You'll receive status messages indicating whether the process was successful or not

To wrap things up, these steps give you more power over your mobile broadband connection in Ubuntu 22.04. By using the command line and setting up scheduled tasks with cron, you can keep your internet running like a charm. No more interruptions while you're in the zone. Now you can just get on with your stuff and stay seamlessly connected wherever you take your Mobile broadband Ubuntu 22.04 setup!"