Finding system information from the command line

At Your Command


The command line provides a plethora of information that you cannot obtain from the desktop.

By Bruce Byfield

Sasha Radosavljevic, 123RF

The command line is an interface designed to give you full control over the system when you are logged in as root. Because information is needed for control, it's not surprising that the average free desktop contains dozens of commands designed to tell you exactly what is happening on your system. In fact, many commands give you so much more information than you could possibly be interested in that you might be tempted to shout, "Overshare!" in much the same tones you did when your roommate last gave you a play-by-play account of his or her most recent date.

To start, I will talk about some of the commands that can reveal most of the information you will likely want about your system. Many have no direct desktop equivalent, but if you are trying to manage a system, on however small a scale, you should find them useful.

Note that many of these commands are not available from everyday accounts. If you try one of them and the system claims that it does not exist, trying logging in as root or starting the command with sudo, and you should be in business.

General System Information

The most basic command for system information is uname, which gives you a grab bag of information about the system (Figure 1). Although you can choose which pieces of information to display, usually it is easier to simply list everything uname can give you by typing uname -a.

Figure 1: The uname command gives a grab bag of basic system information.

Uname's output with the -a option is a single line with information separated by spaces. From left to right, the information is the general name of the kernel, the name of your machine, the kernel version and when it was compiled, your processor type, and the operating system you are using.

If you want to know what kernel modules are in use, the command is lsmod - with no options (Figure 2). In fact, the command is so simple that it doesn't even have a man page. All lsmod does is list each kernel module, its size, and the modules that refer to it.

Figure 2: The lsmod command lists each kernel module installed. Note that this is only a small sampling of its output.

Memory Usage

On a modern system, hard drive space and system memory are less pressing concerns than they were five years ago. Still, enough songs and pictures can fill even the largest hard drive, and you do not need to go far into animation or sound mixing before you find yourself at the limits of both types of memory. Fortunately, you have several commands to help you track memory usage.

For hard drive memory, the basic command is df (short for disk space free). The command by itself lists memory on all partitions, or you can specify which partitions to display. To refine the command further, you can use the switch -type=[filesystem] and display only those partitions formatted with a particular filesystem, such as ext3 or ReiserFS. Another useful switch is -H, which displays in so-called "human-readable" form - for example, it would display a size of 269377692 as 270G (gigabytes) instead.

Whatever options you choose, df displays the total amount of space on the hard drive, the space used as both an amount and a percentage of the whole, the space free, and where each partition is mounted (Figure 3).

Figure 3: To check free hard drive space, use df.

In contrast, system memory is the RAM combined with the size of the swap partition, which you can discover with the free command (Figure 4). Just as with the df command, you can specify how memory statistics are displayed, but, in the case of free, you use either the option -m for megabytes or -g for gigabytes (options -k for kilobytes and -b for bytes are available, too, but neither is likely to get much use these days). Also, you might want to use -t to add a line to the output containing totals.

Figure 4: The free command shows the amount of system memory you have. System memory is the amount of RAM plus your swap partition.

If you are seriously tracking memory usage, then -s[SECONDS] updates the results at the interval given by [SECONDS].

Partition Information

The quickest source of information about how your hard drives are divided comes from displaying the contents of /etc/fstab. For example, on the machine I use, entering the command less /etc/fstab produces the results shown in Figure 5.

Figure 5: The fstab file has information about what drives are currently mounted.

From this information, I can quickly see that I have seven hard drive partitions (including one for Windows that isn't listed because I don't use it on /dev/sda1 and excluding /dev/sda3, which is an extended partition and therefore less important than the partitions that exist on it) and where each is mounted, the filesystem used to format each one, and the options with which each is mounted. To understand the filesystems, look for the -t option entry in the man page for the mount command. The -o option man page entry will give you a basic crib for the mounting options.

Should you want to know specifically what the swap partition on your system is doing, you can use the command swapon -s to see its size and how much of it is currently in use (Figure 6).

Figure 6: The swapon -s command tells you how your swap partition is functioning.

For more detailed information about all partitions, experienced users usually rely on the fdisk -l command (Figure 7). Although fdisk is the same basic command for creating and deleting partitions, you shouldn't be concerned - with the -l switch, it only displays where each partition starts and ends and how it is formatted. The command has the advantage over displaying /etc/fstab in that it shows the extended, as well as any unmounted, partitions.

Figure 7: The traditional way to read information about your hard drive partitions is the fdisk -l command.

However, even the venerable fdisk pales beside GNU Parted, the partition editor that comes with most distributions these days. If you enter the command parted, you start the application's own shell (Figure 8), which has its own set of basic commands. By entering help, you can get a full list of these commands, but the one you want here is print.

Figure 8: The GNU Parted shell's print command will tell you everything you want to know about your hard drive partitions, and then some.

The result is not only information about your hard drive's characteristics, but also each partition type (i.e., extended or logical) and any flags that might be on it, such as which one is marked as bootable. When you have the information you want, type quit to leave Parted's shell and return to Bash.

Other System Hardware Information

Outside of hard drives and processor chips, the basic command for hardware devices is lspci. As you might guess from the name, the command lists the PCI buses that are installed on the system. If you want to know what wireless card your laptop is using, so you can find a driver, or which video card you have, so you can look up information about it, then lspci is the command for you.

The first thing you need to know is that lspci displays information at three levels of detail - or verbosity. To get the least detail, enter only lspci (Figure 9). To get the next level of detail, type lspci -v, and for the most detail, use lspci -vv.

Figure 9: The lspci command describes your computer's hardware in exhaustive detail.

The next thing you need to know is that, even at the lowest level of detail, lspci produces more information than even a full-screen virtual terminal can display all at once. Therefore, you will want to pipe the command through less or more so that you can scroll up and down as needed: lspci | less or lspci | more. If you know information about the device you are looking for, such as its bus or slot, you can filter lspci's output, but in many cases, that is exactly the information you are trying to find, so this filtering is usually of limited use.

The lspci command also comes with a number of useful switches that provide even more information. For instance, -nn shows the numerical and English names of each device, whereas -k shows the kernel modules associated with the device. No matter what options you use, lspci gives so much information that only stone cold hardware gurus will understand it all, but if you use the right switches and take your time, you can usually find the information that you need.

An Alternative

These commands are not the only ones that give you information about your system - they're just the most basic. Other commands give at least part of the same information as those listed here; for example, you could use dnsdomainname to find the machine name instead of uname. However, the commands here are the most multipurpose and should give you most of the information you likely want or need.

However, one alternative is comprehensive enough to be worth mentioning. If you need information that these commands don't provide, or if you are curious about an alternative, you can also try looking at /proc, which is the pseudo-filesystem generated during bootup that contains information from the kernel. Ordinarily, the /proc directory is something that you should never meddle with, especially when logged in as root, because one wrong move could bring your system down. However, as long as you confine yourself to basic viewing commands like cat or more, you cannot damage it.

If you change to /proc and enter the ls command, you will find that the directory comprises a number of folders and files. If you view the files, you will see a variety of information about your system and how it operates. For instance, cat ./version gives information about the version of the kernel and the distribution you are using, whereas cat ./cpuinfo gives detailed information about the chipset in your system. Even if you can't guess what sort of information each file contains by its name, viewing is still harmless.

The advantage of reading from /proc is that you have fewer commands to remember. The disadvantage is that the information is not organized systematically from the human perspective.

However, between the traditional commands and /proc, you should find all the information you will ever need about your software and hardware and - as often as not - much more. Both are ways of using the command line to obtain information that is simply unavailable from the desktop.