inux, like all modern operating systems, has more nooks and crannies than a bargeload of English muffins. That means figuring out the best way to do something with Linux, or even knowing that you can do it in the first place, can be a challenge. And just to make matters even more interesting, Linux and its major components are all improving and changing at an astonishing rate. Whether you’re a long-time user or a rookie who’s just installed Linux for the first time, taming the complexity of your system and figuring out how to get the most out of it is an ongoing adventure.
That’s why we’re presenting our first annual all-tips article, to help you find the most direct path possible from what you want to do to how to do it with Linux. We’re going to take a look at four different facets of using Linux — checking system status, configuring your system, X/ KDE/GNOME, and working with RPM files — and present our favorite shortcuts, hints, and tricks.
Before we get to the tips it’s worth pointing out a few details. First, most of the commands we are presenting here are intended to be run from the command line. So if you are a heavy GNOME or KDE user, you’ll have to either fire up the ol’ xterm window, or else you can quit out of your GUI and experiment a little bit with a text-based shell.
Second, most of these commands have many options beyond the ones we’re showing. The quickest and easiest way to find out more about these commands is by using their man pages. Just enter the command:
and your system should display enough help (or more than enough, in some cases), to keep you busy experimenting for a while.
And last but certainly not least, be careful when editing configuration files such as XF86Config or fstab. Always make a backup copy of the file before you change it, and make sure you have a way to restore that file if something goes wrong. Remember: Computers don’t make mistakes, but they do execute your mistakes with extreme precision.
Free hard disk space
will display the total size, used and available space, and used percentage of all mounted filesystems. The -h option tells df to use a more friendly output format, with disk space measured in MB or GB, instead of disk blocks. You might want to also add the -l option to restrict df to reporting on just the local filesystems, meaning those that aren’t accessed over a network connection.
File space in a directory
You can find out how much disk space a directory of files, including subdirectories, requires by typing in the following command:
The h option tells du to present numbers in a friendlier format, and the s option tells it to present just a summary for the specified directory, instead of a total for that directory and each subdirectory beneath it.
To get a quick snapshot of the memory usage on your system, you can use the command:
to display a relatively straightforward summary. You can also add the -s <time> option to make free keep reporting new numbers every <time> seconds (until you cancel it with Ctrl-C).
| Figure One: You can use the mount command (with no options) to display all your currently mounted filesystems.|
Thanks to all the mountable devices people use today, including floppies, Zips, and CD-ROMs, it’s easy to forget what’s mounted where. Luckily this is one of the things the mount command can handle, in addition to mounting filesystems. Just type mount with no options, and it will show you all the currently mounted filesystems (see Figure One), as well as some information about each one, such as the filesystem type and its permissions.
Find that Program
The which command:
will search through all the directories in your current PATH environment setting and find all files named filename. The -a option tells which to list all matches and not stop at the first one it finds. Another interesting detail is that which counts on the executable flag being set on a file. This means that a script file my_program.sh that you can run with the command sh my_program.sh but that isn’t marked as executable won’t be found.
Which Processes are Running
Linux systems typically run numerous processes at once. You can list all the running processes with the ps command, but it has one of the most complex syntaxes of any Linux command. A better alternative is the command:
which displays a tree structure of the processes on your system, showing which ones started which others. The -p option tells pstree to include the PID (process ID) number in parentheses after the process name.
In true Linux/Unix style you can combine two commands to find files on your system that contain a given piece of text. For example, the command:
find dir_name -print |xargs grep char_string
will find all files in or under the directory dir_name that contain char_string. This command will generate a lot of output, including error messages you can ignore. You can hide the error messages by appending 2> /dev/null to the command. For example, the command:
find /home/lou -print | xargs grep
“warning” 2> /dev/null>
will find all files in the directory /home/lou that contain the string “warning,” and it will effectively hide the error messages by sending them to the “bit bucket,” /dev/null.
Which Linux am I running?
The uname command lets you check some basic information about the copy of Linux you’re running. The command:
will display all the information it has available, including the Linux release and CPU information. Notice that some of the information might not be what you expect. The operating-system release information (which you can ask for by itself by using the -r option instead of -a) will give you a kernel version number similar to “2.2.14.-5.0smp” instead of “Red Hat Linux 6.2.”
|Figure Two: Adding a directory to your PATHenvironment variable.|
Setting Your PATH Variable
When you type a command at the shell prompt, how does the shell find that command in order to run it? The answer is that the shell checks in all the directories listed in your PATH environment variable. If you’re using the bashcommand shell, you can make changes to your PATH in two different places: /etc/profile and ~/.bash_profile (the file .bash_profile in your home directory). Use /etc/profile for system-wide changes that will affect all users, and ~/.bash_profile for changes specific to one user. See Figure Two for an example of adding the directory /nonstandard/ bin to the PATH variable in ~/.bash_ profile.
The dreaded control-alt-delete reboot sequence for PCs can be configured under Linux. The file /etc/inittab normally contains a line similar to:
ca:ctrlaltdel:/sbin/shutdown -t3 -r nowc
which will shutdown your system gracefully (if somewhat abruptly). You can change this to run any command, of course, but it’s probably not a good idea to get too creative. Note that this setting only works in console mode, not in graphical desktop environments.
|Figure Three: Telling Linux how much memory your system really has via lilo.conf.|
Seeing More than 64 MB
If your PC has trouble recognizing more than 64 MB of RAM under Linux, you can solve the problem by adding the line appendmem=128MB (or whatever your actual memory is) to your lilo.conf file, as shown in Figure Three, and then running the command /sbin/ lilo to put your new settings into effect. Some systems use part of the last MB of memory, so you may have to subtract one from the number you specify on the append line. You can lie and tell Linux your system has less memory than you really do, which can come in handy for developer’s stress testing programs. One last point — you can also control this memory setting at any given system boot. If you want to start the linux kernel with 64 MB of RAM, you should enter:
at the boot: prompt.
|Figure Four: Finding the DOS/Windows partition on your Linux system with fdisk.|
|Figure Five: Configuring Linux to make your Windows partition accessible.|
Accessing a DOS/Windows Partition on Your Hard Drive
If you’re running Linux and Windows on the same system, you’ll likely want to access your DOS/Windows partition from Linux. This isn’t hard to do, but it’s a three-step process. First, you have to find out which disk partition contains your Windows files. From a Linux command line enter the command fdisk /dev/hda (substituting the device name for your hard drive in place of /dev/hda). Then enter p to display the list of disk partitions on that drive, and then q to quit fdisk. You should see something like Figure Four, which shows that on this system the DOS/Windows partition is hda1 (look for the vfat partition, which is normally the DOS/Windows partition under Linux).
Next, edit your /etc/fstab file as root and make sure you have a line that looks like the last one in Figure Five. (This configuration mounts the DOS/Windows partition read/ only. To mount it read/write, change the ro to rw.)
Finally, create the /mnt/windows directory (or whatever directory you used in /etc/fstab) with the command:
Now all users will automatically have access to the DOS/ Windows partition when they log in.
| Figure Six: Enabling your mouse scroll wheel under GNOME.|
If your mouse is compatible with the Intellimouse protocol and it has a wheel, you can enable the wheel (at least under GNOME) by editing the “Pointer” section of your /etc/X11/XF86Config file as shown in Figure Six. Make sure you comment out the Emulate3 lines, you use the IMPS/2 protocol, and that you add the ZAxisMapping line.
| Figure Seven: Setting the default color depth/number of colors for your X session.|
You can make X use a particular color depth (number of bits, and therefore colors, per pixel) via the command line with the startx command’s bpp option. For example:
will use 16 bits per pixel (65,536 colors). Note the double dash before the bpp option with the single dash.
To configure a default color depth, edit the “Screen” section of /etc/X11/XF86Config for the X driver you’re using (you’ll sometimes have more than one driver and Screen section on your system), and add the DefaultColorDepth line as shown in Figure Seven.
| Figure Eight: Choosing between a character mode and a graphical mode (shown here) login.|
Graphical or Text Boot
You can specify whether to have Linux start with a graphical or command-line interface a couple of ways. The first is to override the default setting at boot time. At the boot: prompt you can either enter linux3 to start in text mode, or linux5 to start in graphical mode. (Press the TAB key at the boot prompt to see a list of the kernels on your system.)
You can change the default mode by editing /etc/inittab and setting the initdefault line to include either a 3 or 5, as shown in Figure Eight.
Changing Screen Resolutions
If your X configuration supports more than one screen resolution (which is typical on modern hardware), you can cycle between them by pressing the Ctrl-Alt-<plus> key combination (using the + key on a PC’s numeric keypad) and Ctrl-Alt-<minus> (again, the one on the keypad). You can turn off this capability by adding the line:
to the “ServerFlags” section of your /etc/X11/XF86Config file. By default this line is present but commented out with a # character. You can remove the # and restart X to disable the resolution changes. See Figure Nine for this flag and the one in the next tip.
| Figure Nine: Turning off the X hotkeys that change your screen resolution and shut down your entire X session.|
If you want to immediately halt X and everything running on top of it also, press Ctrl-Alt-<backspace>. This is obviously a drastic step, since it doesn’t give your programs a chance to save data — they’re killed instantly. You can disable this keystroke by adding the line:
to the “ServerFlags” section of your /etc/X11/XF86Config file, as shown in Figure Nine. Be careful about choosing to leave this keystroke enabled, though. As painful as it can be to hit it accidentally and take down your entire work session, it can be even more frustrating to need this capability enabled in a dire emergency (such as when you are experimenting with X settings and you get something horribly incorrect) and not have access to it.
| Figure Ten: Controlling the use of a virtual desktop in your X sessions.|
Disabling a Virtual Screen
Some Linux distributions set up X and your desktop environment to use a virtual screen that’s larger than your physical screen, resulting in your screen scrolling when you move your mouse pointer near the edge of the screen and a vertigo-like reaction from users who aren’t used to this behavior.
You can turn off this feature by editing the “Display” subsection of the “Screen” section of your etc/X11/XF86Config file. Figure Ten shows a 32-bit depth “Display” section configured to use a virtual screen of 1280×1024 pixels, and a 16-bit “Display” section configured to always use a physical screen size of 1024×768 pixels without a virtual screen. Note that in the general case you can’t just comment out the “Virtual” statement in a “Display” section to turn off virtual screens, since the virtual screen defaults to being the same as the highest resolution defined in that “Display” section.
You can change your XF86Config file so that it has only one screen resolution defined that is the same as the “Virtual” setting, as shown in the 32-bit example in Figure Nine, or so that the “Virtual” setting matches the lowest physical screen resolution, which would be “1024 768″ for the 16-bit part of the example.
Changing between GNOME and KDE
Distributions that include both KDE and GNOME normally have one as the default desktop environment. You can change the default by editing the file ~/.xinitrc. To make KDE the default, the only non-comment line in the file should be “startkde,” while the line should be “gnome-session” for GNOME.
Switching Between Graphical and Text Mode
You can change from text mode to a graphical login using the command init 5, or from graphical to text with init 3. The 3 and 5 are Linux run levels, with 3 being the normal text mode and 5 being a normal X session. Note that this command will halt all your currently running programs and present you with a new login screen or prompt, and that you need to be running with root privileges to execute either command.
RPM (Red Hat Package Manager) has many options, some of which are relevant only in unusual circumstances. You should consult the documentation, including the man page, for more detail.
In particular, it’s worth noting that sometimes RPM requires the name of the physical package file (fileutils-4.0-8.i386.rpm) and at other times it wants just the logical name of the package (fileutils). In the examples below we have used actual file or package names to make it clearer when to use which one.
| Figure Eleven: An example of the information you can display about installed RPM packages with the -qi option.|
Working with RPM Packages
You can install an RPM package with the command:
rpm -ivh fileutils-4.0-8.i386.rpm
This will provide you with visual feedback in the form of a series of “hashmarks” (# characters) written to the screen as the package is installed.
To update a package that’s already on the system (which will uninstall the prior versions of the package), you can use the command:
rpm -Uvh fileutils-4.0-8.i386.rpm
You can also “freshen” a package with:
rpm -Fvh fileutils-4.0-8.i386.rpm
which will perform an upgrade but only if a version of the package is already installed.
Another useful option is -test, which lets you test drive an RPM installation and check for problems without actually performing the installation.
To uninstall or erase a package, use the command:
rpm -e fileutils-4.0-8.i386.rpm
It is possible to see the entire list of RPM packages that are currently installed on your system. This is usually quite a long list and therefore should be redirected to a file as shown below. To do this, simply enter the command:
rpm -qa > package_list.txt
To find out which version, if any, of an individual package is installed:
You can also see a list of the files that are installed as part of a package:
or see a list of the files in a package that has not yet been installed:
rpm -qpl fileutils-4.0-8.i386.rpm
You can display the description, build and installation dates, and many other details about a package with the following command, as shown below in Figure Eleven.
To find out which package a file on your system belongs to, you can use:
If you want to verify that an installed package is still intact, enter the command:
RPM will then check each file in the package for its size, checksum, and other attributes, a total of eight. If everything is as it should be, then RPM will display no output.
If it finds something amiss it will display a list of eight characters, with dots representing the tests that passed, and a letter or number indicating which one(s) failed.
Lou Grinzo is a technical writer and consultant living in the wilds of upstate NY. He can be reached at lgrinzo @stny.rr.com.