If you’re a Linux user, then you know that the network is one of the most important aspects of your computing experience. In this article, we’ll take a look at 11 commands that will help you work with the network from the terminal.
- ping -a This command will ping your computer and return its response time in milliseconds. This is great for checking whether your computer is online or not.
- netstat -an This command will show all active connections on your system as well as their status and type. This can be used to determine whether or not a connection is working properly.
- ifconfig -a This command will show all interfaces on your system as well as their status and type. This can be used to determine which interface is connected to which network card or device.
- wget -O http://www2-website-1/file/ | grep “Content-Type” This command will extract a file from an URL using grep, which allows you to see what type of file it is (text, image, etc).
- ssh -i keypair_id /path/to/file | ssh-keygen -t rsa This command will generate two SSH keys (one for yourself and one for the remote user). These keys can be used to connect to other computers over SSH without having to remember their individual key pairs!
Whether you want to download files, diagnose network problems, manage your network interfaces, or view network statistics, there’s a terminal command for that. This collection contains the tried and true tools and a few newer commands.
You can do most of this from a graphical desktop, although even Linux users that rarely use the terminal often launch one to use ping and other network diagnostic tools.
curl & wget
Use the curl or wget commands to download a file from the Internet without leaving the terminal. If you’re using curl, type curl -O followed by the path to the file. wget users can use wget without any options.. The file will appear in the current directory.
ping
ping sends ECHO_REQUEST packets to the address you specify. It’s a great way to see whether your computer can communicate with the Internet or a specific IP address. Bear in mind that many systems are configured not to respond to pings, however.
Unlike the ping command on Windows, the Linux ping command will keep sending packets until you terminate it. You can specify a finite amount of packets with the -c switch.
tracepath & traceroute
The tracepath command is similar to traceroute, but it doesn’t require root privileges. It’s also installed by default on Ubuntu, while traceroute isn’t. tracepath traces the network path to a destination you specify and reports each “hop” along the path. If you’re having network problems or slowness, tracepath can show you where the network is failing or where the slowness is occurring.
mtr
The mtr command combines ping and tracepath into a single command. mtr will continue to send packets, showing you the ping time to each “hop.” This will also show you any problems — in this case, we can see that hop 6 is losing over 20% of the packets.
Press q or Ctrl-C to quit when you’re done.
host
The host command performs DNS lookups. Give it a domain name and you’ll see the associated IP address. Give it an IP address and you’ll see the associated domain name.
whois
The whois command will show you a website’s whois records, so you can view more information about who registered and owns a specific website.
ifplugstatus
The ifplugstatus command will tell you whether a cable is plugged into a network interface or not. It isn’t installed by default on Ubuntu. Use the following command to install it:
Run the command to see the status of all interfaces or specify a specific interface to view its status.
“Link beat detected” means the cable is plugged in. You’ll see “unplugged” if it isn’t.
ifconfig
The ifconfig command has a variety of options to configure, tune, and debug your system’s network interfaces. It’s also a quick way to view IP addresses and other network interface information. Type ifconfig to view the status of all currently active network interfaces, including their names. You can also specify an interface’s name to view only information about that interface.
ifdown & ifup
The ifdown and ifup commands are the same thing as running ifconfig up or ifconfig down. Given an interface’s name, they take the interface down or bring it up. This requires root permissions, so you have to use sudo on Ubuntu.
Try this on a Linux desktop system and you’ll probably get an error message. Linux desktops usually use NetworkManager, which manages network interfaces for you. These commands will still work on servers without NetworkManager, though.
If you really need to configure NetworkManager from the command line, use the nmcli command.
dhclient
The dhclient command can release your computer’s IP address and get a new one from your DHCP server. This requires root permissions, so use sudo on Ubuntu. Run dhclient with no options to get a new IP address or use the -r switch to release your current IP address.
netstat
The netstat command can show a lot of different interface statistics, including open sockets and routing tables. Run the netstat command with no options and you’ll see a list of open sockets.
There’s a lot more you can do with this command. For example, use the netstat -p command to view the programs associated with open sockets.
View detailed statistics for all ports with netstat -s.
We’ve also covered commands for managing process and working with files in the past.