2.2 Basic Unix

This section should provide a basic overview of Unix for someone who doesn't know anything about it. If you do, you can skip to the next chapter. If you don't, this will only be a snippet of knowledge, and you need to get a real Unix book to put by your bedside, alongside the USAH (which covers this ground most excellently), HLE, and Unix Power Tools [Powers+ 02] (hereafter called UPT). The goal here is not to explain every nuance of Unix but to make the first-time user aware and knowledgeable enough to ask the right questions. We hope that sometimes simply a mention is enough to get you to ask the right questions.

First, Linux is primarily used through a command line interface . You open a terminal shell and enter text commands on the command line ”see Figure 2.1 for an example of the ls and date commands entered into a shell. You probably installed one or more of the Linux desktop graphical interfaces, Gnome or KDE. When your Linux system boots, it will probably come up with a login to this graphical interface. [8] After logging in to the GUI as a normal user, you can open a terminal shell (look in the menus ), work from there, and log in as root from there.

[8] Unless you have told the system to bring you up into text-shell (runlevel 3 instead of runlevel 5, found in /etc/inittab ). You can always boot into nongraphical mode by editing LILO or GRUB to add either the failsafe or single command to the kernel parameters.

Figure 2.1. The shell command line interface


Once you open a terminal shell, you can get information on any of the commands by using the Unix man (short for manual) system. The command man man gives you information on how man works. Each of these pages gives information on the valid options for the command. Some provide examples and pointers to other, similar commands. If you don't know the name of the command, you can do man -k commandname . This often gets you much more information than you want, so you can pipe it through a pager such as less or more : man -k commandname less .

Most Linux distributions come with some sort of desktop to give you that Apple/Windows feel, and these work fine ”even better than Apple/ Windows in some respects, because OS X and XP have yet to come up with anything equivalent to the multiple virtual desktops that are standard in Gnome and KDE. But since much of the work that has to be done in web site development and administration has to be done as root , it's still better to do most things in a shell through the command line. You can move files and folders (in Unix, folders are really directories) via a graphical window interface, but you typically don't want to do this as root . It's better to learn how to drive a stick ”you have more control.

2.2.1 Shell

You have many choices for your shell interface. This is a Good Thing, because we all have different preferences. Many, if not most, people go with bash as their shell interpreter (largely because bash is the default shell when Red Hat is installed). Others prefer tcsh , a variation on the original csh . There are also zsh and ksh . You can pick and choose. If you are new to this, go with the default on your system until you know enough to have a preference.

The preferences for your shell are found in .tcshrc or .bashrc or .zshrc , and so on (use ls -a to see the normally hidden files that start with a " . "). Even if you haven't created a local .tcshrc , a systemwide resource file provides global defaults. Most Unix folks pick a shell, develop an extensive customized dotfile (as these things are called ”there are also dotfiles for X Windows, Gnome, KDE, SSH, and combing your hair), and just move it around from system to system. There is also a dotfile generator, which you can use to create dotfiles for many different programs until you get the hang of it (www.blackie.dk/dotfile/).

You can change the shell with the chsh command, which asks for a password.

2.2.2 Owner, Groups, Permissions, Ownership

Everything in Unix is a file, and each of the files has associated with it an owner and a group . If you do an ls -l in a directory, you might see something like Table 2.2, though it won't have column titles like those shown here.

The long listing shows that J. Random Luser ( jrl ) owns three files ( junk.txt , foo.html , and bar.cgi ) and one directory ( bin ) in this directory. The permissions of junk.txt are such that the owner of the file ( jrl ) and the group associated with the file ( jrl ) can read and write the file, and the rest of the world can read it. The file foo.html is a bit more private, and only jrl can alter it, though staff members may view its contents. The world cannot see it at all. The file bar.cgi appears to be an executable, and J. Random Luser can change it, staff can view it, and both staff and he can execute it.

Table 2.2. Sample Directory Listing














Jan 8








Jan 10








Jan 9








Jan 8



The ownership of a file can be changed using the chown command, and the permissions changed via chmod . A typical thing to do would be chmod a+x bar.cgi , which would change the permissions to allow all to execute it. Similarly, one could change the permissions via chmod go-r junk.txt to allow only the owner to read the file.

Old-school Unix types like to use the octal numeric mode to change file permissions, thus leading to such commands as chmod 666 foo.bar . The value 666 is an octal number representing the three permission groups: user/group/world. If the value of one of the groups is 6 , the permissions are "read on, write on, execute off," represented as rw- . Therefore, 666 represents rw-rw-rw- . The value 751 would be rwxr-x--x . Other file attributes, such as the sticky bit, can be changed with these commands; use man chmod to see all the possibilities.

2.2.3 Processes

Every object in Unix is a file; everything that runs is a process . Some run once and quit; others run constantly in the background as daemons . Each process has an owner and a process ID (PID). The owner is important because that's who controls the process ”J. Random Luser can't kill rootly processes, though root can kill JRL's processes. There are also subtler issues with process ownership ”processes that run under root ownership are vulnerable to being cracked and allowing the cracker to gain root access (which is very bad).

Useful commands associated with processes are ps , which shows the processes running, and top , which gives an overview of the processes running. End top with q . There are many useful options for these programs ”as usual man function is a good place to start. Another useful command is man-k best guess at a useful function .

Unix is not perfect, of course, and sometimes a process gets out of hand and must be terminated . The appropriate command for this is either kill or killall . [9] The kill command takes as its argument a PID gotten from ps or top , and killall works on a command name. Typical usage is kill 396 or kill -9 396 if 396 is the PID to be terminated. A killall foo kills all processes named foo . The -9 or -KILL signals indicate the severity of the action you wish to take: -1 is a gentle request, whereas -9 is termination with extreme prejudice. Do a man 7 signal for more details on signals.

[9] Use with caution on Solaris.

2.2.4 PATH and Environment

When a Unix command, such as ls , is executed, how does Unix know where to find the ls program? An environment variable is set, called the PATH , which defines where things are looked for first. Execute the command printenv from a terminal shell. You'll see many environment variables , one of which is the PATH , and in that variable you should see something like /usr /local /bin: /usr /bin: /bin: /usr /bin /X11 . [10] This says that Unix first looks in /usr/local/bin , then in /usr/bin , then in /bin , then in /usr/bin/X11 for any program or command you want to execute, if you don't specify the direct path. It executes the first one it finds, so two programs of the same name could exist in different directories, and Unix would always execute the first one found in the order of the path unless explicitly told to go the other. You can alter the PATH , but the installer probably gave you an excellent beginning one, so leave it alone until you are sure you have a reason to change it.

[10] You could simply do a printenv PATH , but that's much less interesting.

Environment variables tell many different programs where to look to find things. For instance, what is the default editor (the variable EDITOR will tell you), where is the mail spool ( MAIL ), what is the default shell ( SHELL ), and so on? For an example of a few environment variables displayed in a shell, see Figure 2.2.

Figure 2.2. Environment variables


2.2.5 Commands

Next we provide a short introduction to commands we use in this book, and we leave you to man to find out more about them. Raising your awareness is all we can achieve here. Also, be aware that any problem you've had, any trick you seek to accomplish has probably been tried by someone else. USAH, UPT, and/or Google can ease your efforts.

man The first command to know is man , your friend and helper, the manual command ”that is, show the manual for this command, the options, and typical usage. Use it. The command man man is a good place to start. Most man pages have usage examples and further links at the bottom. You can use man -k foo when you don't know exactly what you are looking for.

cd Change directory. It operates either on a fixed path ( cd /usr/share/misc ) or on a relative path ( cd bin would take you to the bin directory below your current working directory, if that directory exists). The following are a few standard shortcuts:

  • ~ is your home directory. The command cd ~/bin means go to /home/jrl/bin .

  • . is the current directory. The command cd ./bin means go the directory bin below this one. The command ./configure means execute the configure file in the current working directory. Because the current directory is probably not in the PATH , if you typed configure at the shell prompt, Unix would first search through all the directories in the defined PATH , not find configure , or find a different one and execute that instead ”the Wrong Thing. Some people put . in their PATH , but this is a Bad Thing for many reasons.

  • .. is the directory above this one. The command cd .. means go to the next directory up. Similarly, ../.. means go two directories up.

mkdir Make a new directory. If you should decide to delete a directory, you have to use rm -rf , but this is powerful and dangerous, so be sure you know what you are doing.

pwd Print working directory (where am I?). There are many ways to have this information show up in your prompt and/or the top of your terminal shell. Do a Google search for your particular shell to find out how to do this.

ls List the files in the current directory. The options ls -l , ls -a , and ls -F are very useful.

mv Move this file. The command mv junk junk2 moves the existing file junk to junk2 . There is no rename command, only mv . A useful option is mv -i for interactive, which prompts before overwriting another file.

rm Remove. Definitely read the man page before using this.

cp Copy.

ln Create a link (alias or shortcut in the Apple/Windows world) to another file. There are hard and soft links ”read the man page.

popd Pop to another directory, remembering where you came from, so that you can pop back.

pushd Push the current directory on the stack so that you can pop back to it.

df Disk free. How much disk space is being used?

du Disk usage. How big are the files in this directory?

grep Find a string within a file. The command grep -i string *.tex finds all the occurrences of string regardless of case ( -i ) in any or all of the files ending with .tex ( * is a wildcard ) in the current directory. The grep command has amazing power, and it is well worth spending some time learning regular expressions to use it. [11]

[11] We discuss the basics of Perl regular expressions in Chapter 4, and much of that information applies to grep .

locate Find all files on the computer with names matching the given string. There is also a similar command, find . The locate command works via a database that is created only at specific times (via the cron daemon, usually at night), so it may not find files that have been added since the last time the database was updated, but is very fast. The find command does a real-time search (which might be very slow) and has a more complicated syntax.

more Page through a file without using an editor. It also can be used to view multiple pages ”for example, locate config more generates pages and pages of output that normally scroll past, but when pipe d (the character is a pipe ) through more , the output shows up one terminal screen at a time. A similar command is less .

uname Basic system description. Try uname -a .

ifconfig, netstat What is the network doing?

chkconfig A Red Hat program that controls which daemons in /etc/init.d run at what point in the start-up (and shutdown) process.

which, where These commands are useful for figuring out where commands live, what a command might be aliased to, and which commands are executed first. Try it with any of the commands listed previously.

who Who's logged in to the system?

There are a few commands that we have aliased with various options in our .bashrc permanently, and you might find them useful also. For example:

 alias ls=ls -F --color=auto 

This modifies the default nature of ls by adding some helpful information to the typical list, by using markers ( -F ) and colors in the directory listings (if you have a color xterm , links, directories, devices, etc., will all have different colors). In bash , all command-line arguments to the command are passed along automatically to the alias. If you simply type ls , you get ls -F --color . But if you type ls -a -B foo*.*bar* , the qualifiers and wildcards are passed into the alias for the plain ls to use as usual, in addition to the -F --color switches already defined.

Here are a few other useful aliases for ls :

 alias ll=ls -l --color=auto  alias l.=ls -d .[a-zA-Z0-9]* --color=auto 

The first alias shortens the directory in long format (show file permissions, owner, size, date, etc.) to ll . The second displays all files, including hidden files that begin with the period character, shows directories as entries (instead of contents) and uses color markup.

Other useful Unix staples include sed and awk , which allow one to do such useful things as replace all the occurrences of a string within many files with another string. They are much more powerful than this, but that's a start.

2.2.6 Basic Filesystem Essentials

For a complete explanation of where things go and why they go there, see www.pathname.com/fhs/. Here we give a brief list of directories that you'll probably visit and what they are. The various versions of Unix each do things slightly differently, so there are no hard-and-fast rules. By the way, looking in all these directories to see what's there is an excellent learning exercise.

/home User directories. Your directory will be under /home/your_name_goes_here .

/root The home directory of root .

/usr/bin The main executables are here. This should be in your path.

/bin Boot-level executables are here, and this should be in your path.

/usr/sbin The main superuser commands. This should be in the root path but not in a user path.

/sbin Basic boot-level superuser commands. Ditto.

/usr/lib Contains dynamic libraries and static files for the executables in /usr/bin and /usr/sbin .

/lib Contains libraries for the executables in /bin and /sbin .

/usr/src Kernel source.

/usr/local, /opt This is where optional ( local ) software is installed.

/usr/X11R6 X Windows commands and libraries.

/tmp Temporary files are stored here. In most Unices, cron runs a daemon that eventually deletes anything here, and some systems delete these files every time the system is rebooted.

/var/tmp/ Another place for temporary files, which will not be deleted automatically.

/var The "variable" directory. Everything here changes often.

/var/spool Mail, printer, and other spools. Mail that is sent to J. Random Luser comes to /var/spool/mail/jrl until J. Random transfers the mail to another directory.

/var/log Log files. These files keep track of what's gone on your system. Do a tail -50 /var/log/maillog to see an example, or do a less /var/log/boot.log . Logwatch and other tripwire security programs watch these files to see what's going on ”so should you. What is written here is determined by /etc/syslog.conf .

/var/www Web files (this is where the action is for us in this book).

/boot Basic Linux boot files. The Linux kernel lives here. The kernel is the basic executable that runs everything else in Linux. Generally, the kernel is very stable, and the average non “power user never has to deal with it (unless you want to get into the fun business of compiling your own kernels ).

/etc Systemwide configuration files. For example, Apache configuration files are under /etc/httpd/conf/ and sendmail is under /etc/mail/ . Studying /etc is an excellent start to a sysadmin education. [12]

[12] One of the best sysadmin/programmers (Brian Hatch) we know learned Unix with vi /etc/* and learned to program in C with man gcc .

/etc/X11 X Windows configuration files.

/etc/init.d A link to /etc/rc.d/init.d , and the place where the startup configuration files live. For instance, this is where you can execute /etc/init.d/httpd status .

/mnt Mount points for removable media (CD-ROMs, floppies). Do a man mount .

/dev The Unix device directory, which you will probably not have to deal with until you have some advanced problem, such as your mouse not working.

/proc The Unix process directory, which acts as an interface to the internal data structures of the kernel. Some of these are readable directories, and you can do such things as cat /proc/uptime . Try it and see what happens.

2.2.7 Useful Programs

Here is a list of Linux programs that we find extremely useful. A good place to find these sorts of things is freshmeat .net. For RPM-based distributions, another good site is rpmfind .net. Some of these programs are included with Red Hat and some are not.

log watch, swatch These programs watch log files, can send e- mails documenting daily occurrences, and can alert based on unusual behavior.

bk2site Turns a list of bookmarks into a set of web pages that can be browsed.

gkrellm A system monitor that keeps track of CPU, memory, the network, what's playing on your CD player, etc.

etherape, ntop Network traffic monitors .

linuxconf, webmin Configuration programs for newbie system administrators. They don't relieve the sysadmin from the responsibility of knowing what's going on, but they help. Be careful ”you can overwrite important system configuration files with these programs. If you use these to alter system files, save the original elsewhere before you fire them up. Using them is also good practice for figuring out what configuration files control what and how changes affect the system. Eventually, the budding sysadmin needs to be able to configure the system by editing text files, but these are a fairly painless way to start.

CUPS A printer administration tool.

nessus A system security tool that scans your system (or someone else's) to look for security holes. SAINT and SARA are similar.

AIDE, tripwire Watch system configuration files to see if a cracker is altering them.

Privoxy Gets rid of banner ads in your browser, and controls cookies.

If you need to change your configuration after installation (and you will), Red Hat includes GUI configuration programs that save the budding system administrator from having to edit text files by hand. Among these are the following:

neat Network

sndconfig Sound card

Xconfigurator X Windows

usbview USB

mouseconfig Mouse

kbdconfig Keyboard

printtool Printer

linuxconfig System

Open Source Development with Lamp
Open Source Development with LAMP: Using Linux, Apache, MySQL, Perl, and PHP
ISBN: 020177061X
EAN: 2147483647
Year: 2002
Pages: 136

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net