Traditionally, Linux has been very popular with system administrators. This has been due to not only Linux’s incredible flexibility and power but also to the UNIX philosophy that drives much of the Linux platform. System administrators spend much of their day in the command line crafting strings of commands that hook together to do something interesting. With this powerful underlying command-line platform, so much is possible.
System administration is sometimes fraught with its own fair share of problems, though. This section lines up some of the problems and attempts to resolve them.
How Do I Schedule Things to Happen?
Built right into Ubuntu is a very powerful system for scheduling things to happen at specific times or at regular intervals. This system, called cron, allows you to specify the timing details and the command to run in a special file called a crontab.
The crontab is a simple text file that holds a list of commands to be run at specified times. These commands, and their related run times, are controlled by the cron daemon and are executed in the system’s background. More information can be found by viewing the crontab’s man page, and we will run through a simple crontab example soon to demonstrate how it is used.
The system maintains a crontab for each user on the system. In order to edit or create a crontab, you need to load it into a text editor. This text editor is opened when you use the -e option on the crontab command. To create a crontab, open a terminal and run the following command:
foo@bar:~$ crontab -e
The default nano text editor will open an empty crontab file. When adding crontab instructions, each line represents a separate crontab entry, also known as a cron job.
A typical line in a crontab looks like this:
00 01 3 5 6 ps ax
Each of the sections is separated by a space, with the final section having one or more spaces in it. No spaces are allowed within sections 1–5, only between them. Sections 1–5 are used to indicate when and how often you want the task to be executed. This is how a cron job is laid out from left to right:
- Minute: (00–59)
- Hour: (00–23; 0 = midnight)
- Day: (1–31)
- Month: (1–12)
- Weekday: (0–6; 0 = Sunday)
- Command: code
If you read each line in the crontab from the left and use these column descriptions, you can see how the instruction is built up. Here’s an example:
01 04 1 1 1 /usr/bin/somedirectory/somecommand
This example runs /usr/bin/somedirectory/somecommand at 4:01 A.M. on any Monday that falls on January 1. An asterisk (*) indicates that every instance (every hour, every weekday, every month, and so on) of a time period should be used:
01 04 * * * /usr/bin/somedirectory/somecommand
This example will run /usr/bin/somedirectory/somecommand at 4:01 A.M. on every day of every month.
Comma-separated values can be used to run more than one instance of a particular command within a time period. Dash-separated values can be used to run a command continuously:
01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommand
This example will run /usr/bin/somedirectory/somecommand at 1 and 31 minutes past the hours of 4:00 A.M. and 5:00 A.M. on the 1st through the 15th days of every January and June.
The /usr/bin/somedirectory/somecommand text in these examples indicates the task that will be run at the specified times. It is recommended that you use the full path to the desired commands as shown here. The crontab will begin running as soon as it is properly edited and saved.
Crontab Command Options
There are a number of options you can pass to the crontab command to make it do different things. Here are some common options:
- The -l option causes the current crontab to be displayed on standard output.
- The -r option causes the current crontab to be removed.
- The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.
When you edit a crontab file, the modified crontab is checked for accuracy and, if there are no errors, installed automatically.
Following is an example of how to set up a crontab to run updatedb, which updates the slocate database. Open a terminal application, type crontab -e, and press Enter. Then type the following line into the editor, substituting the full path for the one shown here:
45 04 * * * /usr/bin/updatedb
Save your changes, and exit the editor. The crontab command will let you know if you made any mistakes. The crontab will be installed and begin running if there are no errors. That’s it. You now have a cron job set up to run updatedb, which updates the slocate database every morning at 4:45.
Note that a semicolon (;) or double ampersand (&&) can also be used in the command section to run multiple commands consecutively:
45 04 * * * /usr/sbin/chkrootkit && /usr/bin/updatedb
The semicolon will cause both commands to be executed. The double ampersand will cause the second command to execute only if the first command does not fail. This example will run chkrootkit and updatedb at 4:45 A.M. daily, provided that you have all the listed applications installed.
How Can I Copy a File from One Computer to Another?
The easiest way to copy files between machines is to use the Places > Connect to Server dialog box to make a connection using the graphical file manager. If you would prefer to do this on the command line, use the following command:
foo@bar:~$ scp file.txt email@example.com:/home/jimmy
The scp command works the same as the normal cp command, but it copies the file (here, file.txt) to another server (chin.com) using a specific user account (jimmy) and into a particular directory on the remote computer (/home/jimmy).
I Know an Application Is Available in Ubuntu, but Synaptic Can’t Find It
If you are browsing through Synaptic and can’t find a package that you know is available for Ubuntu, it is likely that you have not enabled the additional repositories.
To fix this, load Synaptic and select Settings > Repositories. Select the Community Maintained and Software Restricted by Copyright or Legal Issues checkboxes. Click Close to accept the settings and then click the Reload button to refresh the package list. Your package should now be listed.
I Am Running Ubuntu on an Older Computer, and I Would Like a Faster Desktop
Unlike other operating systems, Linux has the flexibility to scale incredibly well across different computers with different levels of horsepower. With the huge range of open source available, you can tweak your system so that it can be optimized in lots of different areas. This is particularly useful for recycling PCs. Some large organizations throw out older hardware that cannot run the latest OS from Microsoft. In many cases, these computers are actually perfectly usable if the software is optimized a little. Some open source groups have been set up to take these old machines, install Linux, and provide them to their local communities.
The first aspect to optimize is the GUI. Make certain you turn off any Desktop Effects. This can be done in the System > Preferences > Appearance tool under the Desktop Effects. After you have done that, you can change the way windows look when dragged. Open the terminal and type the following:
gconftool-2 --type bool --set /apps/metacity/general/reduced_resources true
Another option is to use another desktop environment, such as Xfce in Xubuntu (Figure 6-8).
You may also want to explore applications that are more lightweight. As an example, instead of using OpenOffice.org for word processing, try Abiword, a lightweight word processor than includes several features. For other types of applications, try the following alternatives:
- Web browser: Instead of Firefox, use Galeon.
- Terminal: Use an xterm instead of the GNOME terminal.
- Spreadsheet: An alternative to OpenOffice.org for spreadsheets is Gnumeric.
I Reinstalled Windows, and Now Ubuntu Won’t Start!
The first thing your computer does when you turn it on is read a special place on your hard disk called the master boot record (MBR). The information written there tells the computer what to do next. When you installed Ubuntu, it placed a boot menu on the MBR that lets you choose from which system to boot.
Unfortunately, when you reinstall Windows, it will recreate the MBR, not taking into consideration that any other OS may exist and replacing it with an MBR that only boots Windows. This is no good, and you naturally want to be able to replace it with the menu that lets you choose which system to boot.
Grab the CD you used to install Ubuntu on your computer. If you don’t have it anymore, download a CD image from www.ubuntu.com/download, and burn it on a blank CD. If you used the live CD to install, you will need to use the alternative installation CD with the traditional text mode installer.
Insert the CD in the drive, and restart your computer. It will boot on the CD instead of using the hard disk. Now highlight the Rescue a Broken System line, and press Enter. Select your language and keyboard, and let the installer detect the network (for the computer name, you can leave the default Ubuntu) just as you did when you installed Ubuntu for the first time.
You will then be presented with a list of available partitions on your hard disks. Don’t worry about the first line (/dev/discs/disc0/part1). Start looking at the other lines. You need to remember on which disk and on which partition Ubuntu is installed. Most of the time, you probably have just one disk. If you have Windows installed on it, it is probably located on the first partition of this disk, and Ubuntu should be on the second one. Therefore, you probably want to select /dev/discs/disc1/part2 (and then press Enter), unless your situation is more complex.
On the last screen, select the first line, Execute a shell in.... You can now enter commands. Start by mounting the disks on your system:
foo@bar:~# mount -a
Look at the list printed on the screen. Search the last column for the single / character, and on the corresponding line look for the first column. It should say something such as /dev/hda2 (the last few characters may be different for you). Now type the following command, and replace /dev/hda with what you have just read, but without the last digit (e.g., without the 2 from /dev/hda2):
foo@bar:~# grub-install /dev/hda
Wait for the process to finish. It might take a few minutes. About a dozen lines should have appeared on the screen. Check whether you find the text installation finished. If you do, everything went fine. You can now restart your computer by typing this:
Eject the CD, and then select Reboot the System. Welcome back to Ubuntu!
How Do I Fix My Disk After a Power Failure?
Although the Ubuntu development team takes every care to ensure every possible situation is catered for, power cuts are one of the most difficult problems to resolve. Computers rely on power, and when it is dramatically removed from the system, the whole Ubuntu world in your computer shuts down immediately. The problem with this is that sudden power failure causes your Ubuntu machine to shut down improperly. When you next start the computer, you may then be prompted with a confusing fsck message. What is this, and how do you fix it?
The fsck program is a little tool to fix hard disks that don’t have consistent filesystems, that is, filesystems typically made inconsistent by power failures. When the disk is inconsistent, Ubuntu automatically runs fsck to fix it. It asks you a bunch of questions that only a filesystem developer really understands, and you feel obliged to say Yes to each of them. As such, you sit there hammering the Y key over and over answering the questions.
There is a quick and simple fix to this problem. Instead of wearing out your Y key, you can simply edit one file and have any errors automatically fixed for you.
If your system is already running the desktop, open a terminal. Enter the following command:
foo@bar:~$ sudo gedit /etc/default/rcS
Now change the FSCKFIX line to the following:
Save the file, and the next time you reboot, fsck will fix any detected disk problems without you having to intervene.
If you are using the character-based login, use the following command:
foo@bar:~$ sudo nano /etc/default/rcS
Change the FSCKFIX line as above, and then press Ctrl-O and Ctrl-X to save and exit.
Ubuntu Takes Up Too Much Disk Space on My Old Computer
If you are running Ubuntu on an older computer with a limited amount of disk space, you may want to choose software with more limited space requirements. Luckily, Ubuntu gives you incredibly flexible options when you’re choosing which software you want to install. There is a variant of Ubuntu specifically for older computers, called Xubuntu. Xubuntu is based on Xfce, which is a lighter desktop than KDE or GNOME. To install Xubuntu on an existing Ubuntu system, install the xubuntu-desktop package. You can also download an .iso file of Xubuntu at www.xubuntu.org.
My Computer Is Running Quite Slowly—How Can I Find Out What Is Going On?
If you are having performance problems, there may be a particular process on your computer gobbling up all of the memory. Launch the System Monitor under the System > Administration menu and go the Processes tab. You will see which applications are using more CPU and RAM. You can also use top on the command line:
The top command shows the current processes on your computer that are using the most system resources. If you see a particular program taking up an unusual amount of resources, that may be the culprit.
Some processes (such as the Apache Web server) fork and replicate themselves when used. Another useful technique is to see how many of these processes are running:
foo@bar:~$ ps ax | grep theprogram | wc -l
This command takes a listing of the processes running on the system, uses grep to search for a specific process, and then counts the number of lines returned, thus indicating how many processes are running.
How Can I Learn the Different Options for Commands?
Every command in Ubuntu has a small reference card included called a man page. This page displays the range of available options. Access it by typing man, and then enter the command:
foo@bar:~$ man grep
Another method of listing the options is to use the -h or -help options:
foo@bar:~$ grep --help
How Do I Get My Root Account Back?
In a default Ubuntu installation, the root account is disabled. Instead, the user account created during the installation process is used with sudo to access administer facilities. The sudo command is used extensively to temporarily take on root privileges when needed.
If you want to get the root account back, run the following command:
foo@bar:~$ sudo passwd root
Enter your user account’s password, and then enter a new root password. You will be asked to verify the new root password.
To disable the root account at a later date, run the following command:
foo@bar:~$ sudo passwd -l root
This will lock the root account.
I Forgot My System Password—What Can I Do?
Although passwords are indefinably essential and useful, they are also prone to being forgotten. With an increasing number of nasties out there on the Internet wanting to suck away your passwords, you need to think of more complex passwords, which are in turn harder to remember and easier to forget.
If you forget the system password, you need to jump through a few more hoops to reset your password. Restart your computer, and when you see the word GRUB appear on the screen, press Escape to see the boot menu. Select the recovery mode option from the menu. When the computer boots, it will present you with a root shell. At the prompt type:
foo@bar:~# passwd username
Follow the prompts to set a new password. Finally, reboot the computer with the following command:
How Do I Access My Windows Partitions?
If you are running Ubuntu on a computer with a Windows disk, you may want to read and write to the disk. Look on your Desktop for an icon labeled sda1 or hda1. This is your Windows partition.
How Do I Add Users?
To add a new user to your computer, select System > Administration > Users and Groups. When the window loads, click the Add User button, and then just fill in the details. To do this in the command line, use the adduser command:
foo@bar:~$ sudo adduser jimmy
This adds a user called jimmy.