-->
Page 47
Don't use Linux as root all the time! Although you might be tempted, there are some very good reasons not to. First, even though you might have aliased the rm command to rm -i in your .bashrc file, a simple # rm -fr /* will not only wipe out your Linux system, but also any DOS or Windows partitions if mounted under /mnt. Instead, create a user for yourself and use the su command when you need to do things as the root operator. If you have programs that need to run SUID root, see Phil Hughes's article, "Safely Running Programs as root," in the May 1997 issue of Linux Journal.
One of the first things you should do following a clean install and setup is to make a boot disk, using the current Linux kernel on your computer. You should always have a working copy in case you screw up when recompiling the kernel. Here's one quick way to not only make a copy of your current kernel, but also create an emergency boot disk. First, make sure your kernel points to your root device. You can check this on a recently built kernel with
# rdev zImage
Next, format a disk in your floppy drive using fdformat (assuming a 1.44MB drive A:):
# fdformat /dev/fd0H1440
Next, copy your kernel to the disk with
# dd if=zImage of=/dev/fd0
Now, assuming your computer is set to look at the floppy first, try rebooting with your boot disk with
# shutdown -r now
But you should also have a backup set of emergency boot disks that include not just the kernel, but also a minimal filesystem to get you started on the road to recovery. There are some excellent guides, scripts, and software to help you do this. (See "For More Information" at the end of this chapter.)
Generally, the approach is to create two disks, with one containing a kernel, and the other containing a compressed filesystem with a minimal directory of files, including file utilities. But guess what? Because you're a Red Hat Linux user, you don't have to! Read on to find out why.
If you accidentally delete a text file, don't panic! There's a handy tip, called "Desperate person's text file undelete," from Paul Anderson's "The Linux Tips HOWTO," courtesy of Michael Hamilton.
Page 48
Assuming you remember some of the text, know which partition the file was on, and have a spare partition with some room, you should be able to recover a good portion of the file. Hamilton's approach uses the egrep and strings commands. For example, if you lose a 100-line file with the phrase "Xena," followed by "Lawless," and have room on your DOS partition:
# egrep -100 `Xena.+Lawless' /dev/hda3 > /mnt/dos/lucy
Then you can look for the text with
# strings /mnt/dos/lucy | less
You should also learn about and know how to use some of the file tools included with Red Hat Linux. While e2fsck is run automagically from the rc.sysinit script, it can be helpful in diagnosing and fixing problems. Other commands, such as dumpe2fs and debugfs, provide detailed technical information concerning your Linux filesystem, while others, such as badblocks, can be helpful if you have a non-IDE hard drive.
Here's a list of just some of the programs available:
Most Linux users choose to use the second extended filesystem, and with good reason: e2fs is robust, efficient, speedy, and relatively impervious to fragmentation. This command has a plethora of options aimed at helping you check and repair your filesystem. For safety's sake, unmount the partition and then try
# e2fsck -p /dev/hda3
to automatically repair the partition /dev/hda3.
This command will search a device for bad blocks, and also has a number of options; but beware of the -w option, as it is a "write-mode" test and will destroy data on a partition.
This command is similar to e2fsck, as it checks and repairs Linux filesystems. Be sure to read its man page, as the -P option can be harmful.
The dump command can be used for filesystem backup, as it searches your files that need to be backed up. dump will also do remote backups. The companion program is restore, which also works across networks.
Page 49
This command will dump your filesystem information. You'll get the inode count, block count, block size, last mount, and write time. Running dumpe2fs on a 450MB partition will generate a 26,000-character report. An interesting part of the report is the mount and maximum mount count, which determines when e2fsck is run on a partition when Linux starts.
If you just have to mess with your system's performance, you can use this command to adjust its tunable parametersbut only if you have an ext2 filesystem. Use this command to adjust when e2fsck is run on your partition, but don't do it when the partition is mounted!
Linux hackers will be familiar with this program, which creates a Linux second extended filesystem on a partition. And you might need it too, if you want to create compressed filesystems on emergency disks, or if you install a new hard drive.
This is an ext2 filesystem debugger, with 34 built-in commands. If you call it with
# debugfs /dev/hda3
you can examine your filesystem in read-only mode.
Each of these utilities can help you maintain, diagnose, and repair a filesystem. But what if you can't boot? Read on!
There are a number of reasons why a Linux system might not boot. If you recall the earlier example of making a boot disk, you know that the rdev command is used to set the root device. Building a new kernel, and then trying to use LILO or LOADLIN to load the new kernel, won't work unless you've done this. You'll also have problems if you've rebuilt the kernel and hard-coded in the wrong root device.
I told you earlier that you'll appreciate being a Red Hat user. Here's another good reason: You get a set of emergency boot disks with your Red Hat distribution. If your system won't boot, here's how to possibly recover your system:
First, boot Linux from your Red Hat Linux boot disk. Next, at the boot: prompt, type boot: rescue, which will load a kernel from the disk. Follow the prompts, and when asked, insert the second disk, called "Supplemental Disk." A bar graph will show loading progress of a compressed filesystem, and you'll end up with a # prompt.