System Administration

L

LinuxDotOrg

Guest
Administration of a Linux System

Linux, which inherits most of its design from Unix, also inherits from Unix the need for 'administration'. This means that it isn't just a system that gets installed and sits there waiting for something to go wrong. As the owner of a Linux system, you're responsible for a certain amount of maintenance. This amount depends on just how critical the system is, the number of users and services you're providing. Luckily, there is a wealth of tools to get the job done. There are tools that work on the command line as well as those with a graphical user interface. There are also web-based administration tools. First, we'll look at the traditional command line tools for system administration tasks.

Command Line Tools for System Administration

Those who come from the Unix world are (or at least, should be) accustomed to opening up plain text configurationfiles and working with them. Those who come from the Windows world probably aren't. Even though many people who administer Windows systems started off with MS-DOS, it's most likely only a distant memory for them. Windows systemadministrators should be aware that Linux system administration means getting back to doing things in text mode with text editors. Does that mean that there is no way to do it in graphic mode? Absolutely not. As we mentioned before, there exist GUI tools for this. But anybody who's used a Linux system for some time knows that, on one hand, command line tools can be very efficient and extremely fast if you're proficient with them. On the other hand, there may be cases where you can't run graphic tools on a particular system. And let's face it, the fact that you can deal with plain text configuration implies that you have a greater understanding of what's going on. This will come in handy at some point when you've got a large problem that needs to be solved.

Text Editors for Linux

If you followed our intermediate level course, you will have learned about the most popular text editors in Linux. Therefore, we'll just limit ourselves here to say that you need to pick one that you feel comfortable with. Basically, in the Linux world, it boils down to using one of the two most popular text editors. These are Emacs and vi. For more information on these, please consult the lessons in our intermediate course.

Text Processing Tools

Plain text is a way of life in the Linux world, whether they be log files or dumps of error messages.A Linux administrator, therefore, needs to be familiar with tools that make analysis of these files easier. Luckily, Linux has a large number of command line utilities to help you do this job.

GNU Awk or Gawk

GNU awk is a funny sounding name for a program, but it's one that will serve you well as you maintain your Linux system. Instead of having to look at everything in a log file, for example, awk will help you pick just the data you need out of it. To get started, let's look at a few simple examples:

First, let's start by getting 6 numbers for this week's lottery ticket:
Code:
awk 'BEGIN { for (i = 1; i <= 6; i++) print int(50 * rand()) }'

Ok. I think that's enough fun. Now, let's look at some examples that are more along the lines of what we want to use it for. For example, let's get the total kilobytes used by text files in a directory:

Code:
ls -l *.txt | awk '{ x += $5 } END { print "total Kb: " (x + 1023)/1024 }'

We can also get the total bytes used by the user 'mike' in a given directory:

Code:
ls -l | awk '$3 == "mike" { sum += $5 } END { print sum }'

You can even use awk to keep a simple spreadsheet. Awk is perfectly capable of adding up a column of numbers. Let's say you've had a yard sale. Let's say you're using your PDA to keep track of what you're selling and for how much, and saving the data in a simple text file. It might look something like this:

Code:
ls -l | awk '$3 == "mike" { sum += $5 } END { print sum }'

To get the total of what you've sold, awk can easily add up the third column:

Code:
awk '/:/ { sum += $3 }; END { print sum }' yardsale.txt

You'll notice the underscore between words in the description. It's there because two words are seen as two columns and since awk works by analyzing columns, we don't want to confuse it.

You can also see what you've specifically earned on beer mugs:

Code:
awk '/beer/ { sum += $3 }; END { print sum }'


Using awk for some administration tasks

If you use awk on the Apache log file, you can filter out the data to get the exact time of the hits on your website. The following will show you the frequency of visits.

Code:
cat access | awk '{print $4}' | uniq -c


The following will create a list of worm infected hosts that are trying (in vain) to infect you:

Code:
egrep -i "(root.exe|cmd.exe|_vti_bin)" access | awk '{print $1}' | sort -n | uniq

It wouldn't be too difficult to include output from a modified version of this to add these infected machines to our firewall. Including something like this in a script might work:

Code:
egrep -i "(root.exe|cmd.exe|_vti_bin)" access | sort -n | uniq | awk '{print "/sbin/iptables -I INPUT -p tcp --syn -s", $1, "-j DROP"}

Killing me softly

You can use awk for purposes of violence as well, namely, killing processes. Try the following example out. First, fire up an application. I'll use 'xcalc' here. Then you can use this awk one-liner to kill it, without having to use 'ps' and then look up the pid number.

Code:
ps uax | grep xcalc | awk '{print $2}' | xargs kill


Notes

The yard sale (or garage sale) is a typical US phenomenon. Families rid themselves of things they don't want by putting it out on tables in front of their house, confirming the proverb: One man's trash is another man's treasure. If you've seen the movie 'Toy Story 2', you remember that there's a scene where Woody is stolen while rescuing Wheezy, the Penguin (who bears a striking resemblance to Tux) from a yard sale.
 


great stuff. thanks for sharing , i was looking for more advance admin stuff like this.
 


Top