CLI magic: an initiation more or less

42

Author: Joe Barr

Today is all about tips and tricks from the command line. The only thing they share is that at times they can be useful for life on the CLI-frontier and beyond. Most are exceedingly easy to learn and to use. But as always, be sure and do a man or info on each command covered here to fill in the gaps. The last one takes you a little deeper than we’ve been before. It will be an initiation of sorts. But don’t worry. You can handle it.

When less is more

There is a very handy program included in most Linux distributions called more. Like many other things, it came to Linux via BSD. It’s used to display the contents of a text file one page at a time, rather than simply dumping the entire file to the terminal. If you recall, that’s what the cat command does, and if you need information at the top of a file that’s more than a page long, you have to read real fast to get it.

According to the man page for more on my Red Hat 9 box:

The more command appeared in 3.0BSD. This man page documents more version 5.19 (Berkeley 6/29/88), which is currently in use in the Linux community. Documentation was produced using several other versions of the man page, and extensive inspection of the source code.

Scrolling a page at a time is a step in the right direction, but it’s not all that. What happens when you go a page or two further and find you need to go back a few pages? Scroll bars on your terminal window may get you back to where you want to be, but more won’t. That’s one case where less is more.

That same man page for more also refers you to a similar, but more advanced program, called less. It does everything more can do, and more. Most importantly, it allows you to go backward and forward as you browse. If you are used to using more and can’t remember to type less instead, you might want to create an alias and stick it in your .bashrc file. Do it like this and you won’t have to remember:

alias more='less'

Who am I, really?

This program is for those times when paranoia runs you over, and you’re suddenly afraid that you are no longer the only user on your system. To find out for sure, just type this in your nearest friendly terminal window:

who

Without any argument at all, who will show you everyone logged on and how long they’ve been there. It can also show you who you are, as in your user/host names. Try this:

who am i

The answer I get is “warthawg@microsoft.com”. Just kidding, but you get the idea. It shows your username@hostname.

Probably the easiest way to use who is with “-aH” as arguments. That prints just about everything it can find and puts heading lines over the columns to make it easier to read.

Is he a man with a plan?

One way to find out is to finger the person in question. Not literally, of course: by using the finger command. Say that you were curious about what Ryan Gordon, aka icculus, the Linux game guru, has been up to lately. Let’s find out. Enter this:

finger icculus@icculus.org

The finger command checks the user/host name combo that you specify to see if the host is running a finger server. If it is, and the user you specified has data in his .plan file, you get to see it. According to man, finger

also shows “the user’s home directory, home phone number, login shell, mail status, and the contents of the files “.planâ€, “.projectâ€, “.pgpkey†and “.forward†from the user’s home directory.”

Please note that Ryan uses a custom-made finger server at his end called IcculusFinger. Part of the reason for that is that the original fingerhas become something of a security concern over the years. Making all that information available to anyone on the Internet can make breaking into your system a little easier than it needs to be. If you’re a noobie, I recommend that you don’t run a finger server on your machine.

The mother of all processes

Honest, that’s what man says about init, in a gender-unspecific sort of way: “Init is the parent of all processes.” Feel the power, grasshopper. I promised you that knowing the secrets of the CLI meant you would feel pure energy at your fingertips. Here’s proof. With the init program, you can change run levels.

Aha, you may be thinking. Run levels. Great. But what are they? Good question. Run levels are a convention which identifies which processes are allowed to run at various times. Here’s a list of the primary runlevels:

  • runlevel 0 – system shutting down
  • runlevel 1 – single-user mode (normally used for system maintenance)
  • runlevel 2 – multi-user CLI/no NFS
  • runlevel 3 – multi-user CLI
  • runlevel 4 – (not used)
  • runlevel 5 – multi-user GUI
  • runlevel 6 – system boot

Exactly what processes are started when is decided by the contents of the /etc/inittab, so you can decide those things for yourself if you have a mind to. I’ve never felt the need to change inittab from stock condition, but I know some who have.

What if?

Say that you decide to upgrade your system with the latest 3D graphics card from Nvidia so that you can numb your brain playing some of the games icculus has been busy porting to Linux the past few years. You stick the new card in, hurriedly follow the directions to get it configured correctly for 3D, then log out or reboot to restart X. But horror of horrors, X won’t load because you did something wrong in the XF86Config file and it can’t find the right driver for your card. You might find yourself in a loop of attempts to start X and X crashes. Not a pretty picture.

So what can you do about it? A lot, if you know the magic of init. If you’re running a modern distribution, it probably uses grub to handle booting chores. That’s fine. Here’s what to do to boot into single-user mode and fix the problem.

Here’s how I do it in Red Hat. When the grub screen appears, I press the letter “e” to get into editing mode. Once you’re there, move up or down as necessary to get to the line that begins with “kernel”. Then press the letter “e” again. That allows you to edit the line you’ve highlighted. At the end of that line – the one that begins with kernel – add a single space and the word “single”. Then press the letter “b” to boot.

If you are using lilo instead of grub, highlight the version of Linux you want to boot from the menu, then press the “Tab” key, then enter “linux single” and hit return.

You should now boot into single-user mode – aka runlevel 1 – instead of runlevel 5. That’s the one with the X server problem. If you’re like me, you’ve probably forgotten to replace “nv” as the driver for your graphics card with “nvidia”. That’s one of the changes required to run your card in 3D. Use your favorite editor to fix and save the XF86Config file that lives in /etc/X11. Then reboot and you’ll be ready to take advantage of your new card.