Speedtest On Linux: Command Line Guide
So, you want to run a speed test on your Linux machine, huh? Forget about opening a browser and navigating to some website. We're going to do it the cool way – from the command line! This guide will walk you through installing and using the speedtest-cli tool. It's simple, fast, and gives you all the info you need right in your terminal.
Why Use Command Line Speedtest?
Before we dive in, let's talk about why you'd even want to use a command-line speed test. There are a few good reasons:
- No GUI Needed: Perfect for servers or systems without a graphical interface.
- Automation: Easily script speed tests to run regularly and log the results.
- Lightweight: Uses minimal resources compared to browser-based tests.
- Precision: Often provides more accurate results by eliminating browser overhead.
- Remote Access: Ideal for testing network speeds on remote machines via SSH.
Installation
Alright, let's get this show on the road. Installing speedtest-cli is super easy, but the method varies slightly depending on your Linux distribution. Here's how to do it on some of the most popular ones:
Debian/Ubuntu
For Debian-based systems like Ubuntu, you can use apt (Advanced Package Tool). Just open your terminal and type:
sudo apt update
sudo apt install speedtest-cli
The sudo apt update command updates your package lists, ensuring you get the latest version of speedtest-cli. The sudo apt install speedtest-cli command then installs the tool. You'll probably be prompted for your password, so go ahead and enter it. This is probably the easiest method for getting set up, and the one most users should try first. If you encounter issues with this, double check that you have configured the apt package manager to use the correct sources.
After the installation is complete, verify it by typing speedtest-cli --version in your terminal. It should display the version number, confirming that the installation was successful. This is a really crucial step because it ensures that your system correctly recognizes the command and that all the dependencies are correctly installed and linked.
CentOS/RHEL/Fedora
If you're running a Red Hat-based distribution, you'll use yum or dnf. For older systems using yum, the command is:
sudo yum install speedtest-cli
For newer systems using dnf, use:
sudo dnf install speedtest-cli
Just like with apt, you might need to enter your password. These commands will install the speedtest-cli tool from the system repositories. Sometimes, however, it might not be available in the default repositories, so you might need to add a repository containing the package before you can install it.
Again, verify the installation with speedtest-cli --version. Checking the version is especially important in these systems, as sometimes the package names can conflict, and you might inadvertently install a different, similarly named tool. This command confirms that the correct speedtest-cli is installed and ready to use.
Arch Linux
Arch Linux users can use pacman:
sudo pacman -S speedtest-cli
pacman will prompt you to confirm the installation. Just press y and enter. Arch Linux is known for its rolling-release nature, so you'll generally get the latest version of packages as soon as they're available. However, it's still good practice to ensure that your system is up-to-date before installing new packages.
And, you guessed it, verify with speedtest-cli --version. Consistent version checking ensures that regardless of the installation method, the tool is correctly installed and recognized by your system. This is particularly important in Arch Linux due to its frequent updates.
Python pip
Alternatively, you can use pip, the Python package installer, if you have Python installed:
sudo pip install speedtest-cli
Or, if that doesn't work, try:
sudo pip3 install speedtest-cli
Sometimes, you might need to use pip3 explicitly if you have both Python 2 and Python 3 installed. This method installs speedtest-cli as a Python package, which can be a good option if you prefer managing your tools within the Python ecosystem.
If you get a permission error, try adding the --user flag:
pip install --user speedtest-cli
Or:
pip3 install --user speedtest-cli
Using the --user flag installs the package in your user directory, avoiding the need for sudo. However, ensure that your user's bin directory is in your PATH so you can execute the speedtest-cli command. If you're having issues using this method, make sure that Python and pip are correctly installed and configured in your system.
And, as always, verify with speedtest-cli --version. This ensures the tool is correctly installed within your Python environment and accessible from your command line.
Running the Speedtest
Okay, now for the fun part! Running the speed test is incredibly simple. Just type:
speedtest-cli
And hit enter. The tool will automatically find the best server and start testing your download and upload speeds. The output will look something like this:
Retrieving speedtest.net configuration...
Testing from Comcast (your.ip.address)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Akamai Technologies, Inc. (Atlanta, GA) [7.21 km]: 12.412 ms
Testing download speed................................................................................
Download: 92.45 Mbit/s
Testing upload speed..................................................................................
Upload: 42.12 Mbit/s
Pretty cool, huh? The output shows your download speed, upload speed, and the ping to the server used for the test. This is a quick and easy way to check your internet speed without any fuss. It's also great for scripting, as you can easily parse this output in your scripts.
Understanding the Output
Let's break down what the output actually means:
- Download Speed: This is the rate at which data is transferred from the internet to your computer. It's measured in megabits per second (Mbit/s). Higher is better!
- Upload Speed: This is the rate at which data is transferred from your computer to the internet. Also measured in Mbit/s. Important for sending files, video conferencing, etc.
- Ping: This is the latency, or the time it takes for a small packet of data to travel from your computer to the server and back. Measured in milliseconds (ms). Lower is better! A low ping means a more responsive connection.
Advanced Options
speedtest-cli has a few extra options that can be useful. Here are some of the most common ones:
- 
List Servers: To see a list of available servers, use: speedtest-cli --listThis will display a list of servers with their ID, name, location, and distance from you. You can then select a specific server to use for your test. 
- 
Specify Server: To use a specific server, use the --serveroption followed by the server ID:speedtest-cli --server 1234Replace 1234with the actual server ID from the list. This is useful if you want to test your speed against a specific server for consistency.
- 
Share Results: To generate a shareable image of your results, use: speedtest-cli --shareThis will upload your results to speedtest.net and provide a URL to the image. You can then share this image with others. This is great for showing off your speeds or for troubleshooting with your ISP. 
- 
Simple Output: For a simplified output, use: speedtest-cli --simpleThis will display only the ping, download speed, and upload speed, making it easier to parse in scripts. 
- 
Bytes Instead of Bits: To display speeds in bytes instead of bits, use: speedtest-cli --bytesThis can be useful if you're more comfortable working with bytes. 
Automating Speed Tests
One of the coolest things about speedtest-cli is that you can easily automate speed tests using cron jobs. This allows you to monitor your internet speed over time and identify any potential issues.
Setting up a Cron Job
To set up a cron job, you'll need to edit your crontab file. Open your terminal and type:
crontab -e
This will open the crontab file in a text editor. If it's your first time, you might be prompted to select an editor. Choose your favorite.
Now, add a line to the crontab file that specifies when and how often you want to run the speed test. For example, to run the speed test every hour and save the results to a file, you can add the following line:
0 * * * * speedtest-cli >> /home/youruser/speedtest.log 2>&1
Let's break down this line:
- 0: This specifies the minute of the hour when the job should run (in this case, the top of the hour).
- * * * *: These asterisks represent the hour, day of the month, month, and day of the week. Using asterisks means the job will run every hour, every day, every month, and every day of the week.
- speedtest-cli: This is the command to run.
- >> /home/youruser/speedtest.log: This redirects the output of the command to a file named- speedtest.login your home directory. The- >>appends the output to the file, so you don't overwrite previous results.
- 2>&1: This redirects any error messages to the same file as the standard output.
Replace /home/youruser/speedtest.log with the actual path to the file where you want to save the results. Also, replace youruser with your actual username.
Save the crontab file and exit the editor. The cron job will now run automatically in the background.
Analyzing the Results
To analyze the results, you can simply open the speedtest.log file and view the output. You can also use scripting tools like awk or grep to extract specific information from the log file.
For example, to extract the download speeds from the log file, you can use the following command:
grep Download /home/youruser/speedtest.log
This will display all lines in the log file that contain the word "Download", which will include the download speeds from each test.
You can then use this information to create graphs or charts to visualize your internet speed over time. This can be very useful for identifying trends or problems with your internet connection.
Troubleshooting
Sometimes, things don't go as planned. Here are a few common issues and how to fix them:
- Command Not Found: If you get a "command not found" error, it means that the speedtest-clicommand is not in your system's PATH. Make sure that the directory wherespeedtest-cliis installed is in your PATH. If you installed using pip with the--userflag, ensure your user's local bin directory is in your PATH.
- Permission Denied: If you get a "permission denied" error, it means that you don't have the necessary permissions to run the command. Try running the command with sudo. If you're still having problems, check the permissions of thespeedtest-cliexecutable file.
- Connection Errors: If you get connection errors, it could be due to a firewall or network issue. Make sure that your firewall is not blocking speedtest-cli. Also, check your network connection to make sure that you have internet access.
Conclusion
And there you have it! You're now a command-line speed testing pro. You can impress your friends with your mad terminal skills and monitor your internet speed like a boss. Whether you're a system administrator, a developer, or just a curious Linux user, speedtest-cli is a valuable tool to have in your arsenal. So go forth and test those speeds!