Speedtest CLI: Test Your Internet Speed On Linux

by Jhon Lennon 49 views

Hey there, tech enthusiasts! Ever wondered how to check your internet speed directly from your Linux terminal? Well, you're in the right place! In this article, we'll dive into using the Speedtest CLI (Command Line Interface) client. It's a nifty tool that lets you measure your internet's performance without needing a web browser. Let's get started!

Why Use Speedtest CLI on Linux?

Before we jump into the how-tos, let’s talk about why you might prefer using the Speedtest CLI over the traditional web-based speed tests. There are several compelling reasons:

  • Automation: The CLI is perfect for scripting and automation. Imagine scheduling regular speed tests to monitor your network's performance over time. This is super useful for diagnosing intermittent issues or ensuring you're getting the speeds you're paying for.
  • Server Selection: While the default Speedtest server is usually fine, the CLI allows you to specify a particular server. This can be invaluable when troubleshooting connectivity problems with specific services or when you want to test your connection to a server in a different geographic location.
  • Lightweight: The CLI client is lightweight and doesn't hog resources like a web browser. This is especially beneficial on older hardware or systems with limited resources.
  • No Browser Required: Sometimes, you might be working on a headless server (a server without a graphical interface). In such cases, a web browser is out of the question, making the CLI client the ideal solution.
  • Detailed Results: The CLI provides detailed test results, including ping, download speed, upload speed, and more. This information can be helpful for diagnosing network issues or optimizing your internet connection.

For those of you who love the command line, this is a no-brainer. It's fast, efficient, and gives you the data you need without any fluff. Plus, it's really satisfying to see those numbers pop up in your terminal!

Installation

Alright, let's get this show on the road! Installing the Speedtest CLI client is generally straightforward. Here’s how you can do it on different Linux distributions:

Debian/Ubuntu

For Debian-based systems like Ubuntu, you can use apt to install the Speedtest CLI. First, update your package list:

sudo apt update

Then, install the Speedtest CLI:

sudo apt install speedtest-cli

You might encounter a situation where the package isn't available in the default repositories. In that case, you can download the binary directly from the Speedtest website and place it in your /usr/local/bin directory. Don't forget to make it executable!

Fedora/CentOS

On Fedora or CentOS, you can use dnf or yum to install the Speedtest CLI. First, make sure your system is up to date:

sudo dnf update

Or, if you're using CentOS:

sudo yum update

Then, install the Speedtest CLI:

sudo dnf install speedtest-cli

Or:

sudo yum install speedtest-cli

If the package isn't available, you can download the binary and place it in /usr/local/bin, just like with Debian/Ubuntu. Make sure it's executable!

Arch Linux

For Arch Linux users, you can use pacman to install the Speedtest CLI:

sudo pacman -S speedtest-cli

Arch users usually have the latest packages, so this should work without any issues. If not, you know the drill: download the binary and make it executable.

Manual Installation

If none of the above methods work, or if you prefer to do things manually, you can always download the appropriate binary from the official Speedtest CLI website. Once downloaded, extract the contents and move the speedtest executable to a directory in your $PATH, such as /usr/local/bin. Finally, make sure the file is executable:

chmod +x /usr/local/bin/speedtest

No matter which method you choose, once the installation is complete, you can verify it by running:

speedtest --version

This should display the version number of the Speedtest CLI, confirming that it's installed correctly.

Running Speedtest

Okay, now that you've got the Speedtest CLI installed, let's run some tests! It's super simple. Just open your terminal and type:

speedtest

The CLI will automatically find the nearest Speedtest server and start the test. You'll see the ping, download speed, and upload speed displayed in your terminal. It's like magic, but with networking!

Understanding the Output

The output of the speedtest command provides valuable insights into your internet connection's performance. Here's a breakdown of what each value represents:

  • Ping: Ping, also known as latency, measures the time it takes for a small data packet to travel from your computer to the Speedtest server and back. It's measured in milliseconds (ms). Lower ping values indicate a more responsive connection, which is crucial for online gaming, video conferencing, and other real-time applications.
  • Download Speed: Download speed measures how quickly data can be transferred from the Speedtest server to your computer. It's measured in megabits per second (Mbps). Higher download speeds are essential for streaming videos, downloading large files, and browsing websites with rich content.
  • Upload Speed: Upload speed measures how quickly data can be transferred from your computer to the Speedtest server. It's also measured in megabits per second (Mbps). Higher upload speeds are important for video conferencing, uploading files to cloud storage, and sending large email attachments.

Command Options

The Speedtest CLI comes with a bunch of cool options that let you customize your tests. Here are a few of the most useful ones:

  • --server [SERVER_ID]: Specifies a particular server to use for the test. You can find a list of server IDs by running speedtest --list.
  • --list: Displays a list of available Speedtest servers, along with their IDs, names, and locations. This is handy when you want to test your connection to a specific server.
  • --bytes: Displays the results in bytes instead of bits. This can be useful if you're more comfortable working with bytes.
  • --share: Generates a shareable URL with the test results. This is great for sharing your results with others or for keeping a record of your internet speed over time.
  • --simple: Displays a simplified output with only the ping, download speed, and upload speed. This is useful if you want a quick and easy way to check your internet speed.
  • --json: Outputs the results in JSON format. This is perfect for scripting and automation, as you can easily parse the JSON output and extract the data you need.

Example Usage

Here are a few examples of how you can use the Speedtest CLI with different options:

  • List available servers: speedtest --list
  • Test against a specific server: speedtest --server 1234
  • Display results in bytes: speedtest --bytes
  • Generate a shareable URL: speedtest --share
  • Output results in JSON format: speedtest --json

Advanced Usage

For those of you who want to take things to the next level, the Speedtest CLI can be integrated into scripts and automated tasks. Imagine creating a script that runs a speed test every hour and logs the results to a file. This can be incredibly useful for monitoring your internet connection's performance over time and identifying any potential issues.

Scripting

Here's a simple example of how you can use the Speedtest CLI in a bash script:

#!/bin/bash

# Get the current date and time
date=$(date +"%Y-%m-%d %H:%M:%S")

# Run the speed test and output the results in JSON format
results=$(speedtest --json)

# Extract the ping, download speed, and upload speed from the JSON output
ping=$(echo "$results" | jq .ping.latency)
download=$(echo "$results" | jq .download.bandwidth)
upload=$(echo "$results" | jq .upload.bandwidth)

# Convert the bandwidth values from bytes per second to megabits per second
download_mbps=$(echo "scale=2; $download / 125000" | bc)
upload_mbps=$(echo "scale=2; $upload / 125000" | bc)

# Log the results to a file
echo "$date - Ping: $ping ms, Download: $download_mbps Mbps, Upload: $upload_mbps Mbps" >> speedtest.log

echo "Speed test completed and results logged to speedtest.log"

This script uses jq to parse the JSON output and extract the ping, download speed, and upload speed. It then logs the results to a file named speedtest.log, along with the current date and time. You can then schedule this script to run automatically using cron or a similar task scheduler.

Automation

To automate the speed test, you can use cron to schedule the script to run at regular intervals. Here's an example of how you can set up a cron job to run the script every hour:

  1. Open the crontab editor by running crontab -e in your terminal.
  2. Add the following line to the crontab file:
0 * * * * /path/to/your/script.sh

Replace /path/to/your/script.sh with the actual path to your script. This will run the script every hour at the top of the hour.

Troubleshooting

Sometimes, things don't go as planned. Here are a few common issues you might encounter and how to resolve them:

  • Command Not Found: If you get a "command not found" error when running speedtest, make sure that the Speedtest CLI is installed correctly and that the executable is in your $PATH. You may need to log out and log back in for the changes to take effect.
  • Connection Errors: If you're getting connection errors, make sure that your internet connection is working properly. You can try pinging a known website, such as Google, to see if you can reach the internet.
  • Inaccurate Results: If you're getting inaccurate results, try testing against a different server. You can use the --list option to find a list of available servers and then use the --server option to specify a particular server.
  • Firewall Issues: In some cases, your firewall may be blocking the Speedtest CLI from accessing the internet. Make sure that your firewall is configured to allow the Speedtest CLI to connect to the internet.

Conclusion

So there you have it! The Speedtest CLI is a powerful tool for measuring your internet speed on Linux. It's easy to install, simple to use, and can be integrated into scripts and automated tasks. Whether you're a casual user or a seasoned sysadmin, the Speedtest CLI is a valuable addition to your toolkit. Now go forth and test your speeds, and may your bandwidth be ever in your favor!