Chapter 46. Quick Introduction to UNIX

UNIX is an operating system, similar to DOS. It can run on IBM PCs, Sun Workstations, HP computers, etc. It has been ported to many environments. It is also a multiuser environment: several users can access the same machine simultaneously.

The purpose of this section is to introduce Unix. The reader should be aware that there are many in-depth books written on this subject.

History

The name Unix is not an acronym, but a pun on an early operating system called Multics. Unix was original thought of as an "emasculated" version of Multics (called Unics).

Unix was developed in the early 70's by Bell Telephone Laboratories. Unix was developed using the C language, and is easily ported to other platforms. Unix is non-propriertary - it is not tied to a specific software vendor, nor tied to a specific hardware platform.

Design of the Unix System

The Unix system consists of the kernel system layer atop the hardware.

The essential core of the Unix operating system is called the kernel. This is the software layer that interacts most closely with the computer hardware. The command interpreter--which implements the users commands--is called the shell. This is similar to DOS's command line. The shell can also be a GUI (Graphical User Interface): like Windows is to DOS, X Windows is to Unix.

User applications and utilities also run on top of the kernel. Utilities are print managing programs, format commands, etc.

One of the main reasons that Unix has become so popular is the layered approach that it has taken. This has made it very easy to port to other hardware systems.

Unix Variants

Because Unix is non-propriertary, and is modified by thousands of programmers at Bell Systems, universities, and research organizations around the world, it has may variants.

The most popular flavors of Unix are (in no particular order):

These Unix variants are descendants of the original AT&T Unix code.

Other operating systems are Unix-like in that they have been written from scratch to emulate the behaviour of some versions of Unix. Examples of this are Coherent and QNX.

Personal Unix Systems

Unix can be run on IBM PCs; the most common implementation is Linux. Linux is available on the Internet (for free) from various distributions. All distributions have the same basic kernel, called Linux. The distributions package the Linux kernel with the programs that they feel will provide the best overall package. The programs that they add are custom installation programs, office packages, programming suites, server software, networking software, games, and so on.

The most common distributions are (in no particular order):

Command Line Operation

The Command Line tells Unix what you want it to do. Unix's command line tends to be cryptic, and most people use some sort of GUI.

GUIs

GUIs are Graphical User Interfaces. They all have a look and feel similar to the Windows environment (which is based on the MAC Desktop and X Windows).

A graphical user interface for Unix consists of 2 major parts: the X Windows System (often called X) and a Window Manager. An X server on a computer system manages the screen, keyboard, and mouse, and their interactions with client applications (that reside either on the same system or on another computer on a network).

The window manager (a client application) controls the window decorations and behaviors, such as resizing stacking order of multiple windows.

XFree86 is the free X Windows system that is available with Linux. There is a wide choice of free Window Managers available:

OSF/Motif is the most popular commercial window manager for Unix vendors (other than Sun). Motif is based on the work done by members of the Open Software Foundation (OSF). Motif is also the interface for Open Desktop (ODT), from Santa Cruz Operations (SCO).

Case Sensitive

Unix's command line is case sensitive. Most commands and responses are presented in lower case letters. Unlike DOS and many other systems, you cannot use upper case letters when lower case letters are expected. Thus, typing "LS" to list a directory instead of "ls" will result in an error (the command won't be found). This is especially true of passwords - make sure that when you enter a new password, you remember whether it is upper case or lower case!

Multi-User Operating System

Unix systems run comfortably in a variety of situations: single-user, host systems with users on local or remote terminals, and networked arrangements of workstations and multiuser systems.

Multiple Operating Systems

One of the most attractive features of the personal Unix systems is their ability to run programs designed for other operating systems. Versions of Unix designed for IBM PCs lets you load DOS and Windows applications using programs (such as dosemu and wine respectively), and run them in windows along side Unix specific programs.

Unix is a Pre-emptive Multi-tasking environment. You can have several programs or processes running simultaneously. This also allows Unix to be a multiuser environment.

File Naming Conventions

Once again, Unix is case sensitive, which is a difficult area to get used to when you migrate from the DOS environment.

File Name Length

Unix allows up to 255 characters in the file name; this allows for unique and informative file descriptions.

There is a problem with having long file names if you are working from the command line. Typing in a file name with 255 characters and keeping the case correct can be very frustrating. It is recommended that you keep the file names short and sweet, and also use DOS's naming convention if you are going to transfer files between DOS and Unix (very common occurrence).

Allowable Characters in Filename

Only the "/" (forward slash) is not allowed in filenames because it is used as the pathname separator (DOS uses the back slash "\").

Characters to avoid: ? @ # $ ^ * ( ) ` [ ] \ | ; ' "

You can use spaces or tabs in filenames if you enclose the names in quotation marks on the command line, but they are hard to work with. Use underscores or periods to get visual separation.

Ex. "this is my file" or my_file_is_this_one or here.is.another.file

Don't use - or + as the first character of a filename. Many commands use the - or + to introduce options or switches.

Filenames starting with "." are used by the system to make names invisible to normal directory listings. Typically, preferences or configuration files are "hidden" using a "." prefix. An example is ".signature" used for your electronic signature in E-mail.

Pathnames (/)

Unix uses the forward slash "/" as the pathname separator. Unix's top directory is called the root directory and is indicated by "/".

Compatibility with Other Systems

If you are going to use Unix with other systems, such as DOS, make sure that you follow a file naming convention that is compatible with both systems. It is possible that characters that are allowed in one system may not be allowed or reserved in the other system.

DOS only allows 8 characters followed by a 3 character extension while Unix allows up to 255 characters. When transferring from DOS to Unix there is no problem. But when transferring from Unix to DOS, the Unix filename becomes truncated and converted to the 8.3 format.

Ex. Unix: "this_file_is_ver01_of_pkzip"becomes "this_fil.e_i" in the DOS world.

Wild Cards

Unix allows wild card characters in the file names, similar to DOS. The Wild cards allowed are the asterisk "*" and the question mark "?".

For example:

ls *xt will list all files ending with xt regardless of filename length.
Such as:cat_text  readme.txtls ?xt will only list 3 character long filenames that end with xt.
Such as:txt  cxt

Basic Unix Commands

The Basic Unix commands required to navigate through Unix are shown below:

ls

ls stands for list directory. It is the equivalent of DOS's dir command. Available options are shown below:

-alists all files including hidden files-lgives a long listing including rights

"ls" by itself will not display hidden files. T here are many other options available but these are the most commonly used ones.

Ex. ls...readme.txtKrustys_revengemore_stuffEx.
ls -a...readme.txtKrustys_revengemore_stuff.signature.profileEx.
ls -ltotal 956drwxr-xr-x6 arhother1024Dec 16 09:44.drwxrwxr--x5
rootsys96Dec 12 09:05..-rw-r--r--1 arhother681Jan 28 04:56.profileetc...

pwd

pwd stands for Print Working Directory. pwd displays on the screen the current directory that you are in. Before CRTs, all communication with mainframes was printed out on "teletype" style terminals, thus the origins of the name pwd.

expwd/home/bart

mkdir

mkdir stands for make directory. It is similar to DOS's "md" command. In actual fact, DOS's make directory command was mkdir (for pre-DOS 3.x.).

ex.mkdir homermakes the directory "homer" in the current working directory.

rmdir

rmdir stands for remove directory. It is similar to DOS's "rd" command. In actual fact, DOS's remove directory command was rmdir (for pre-DOS 3.x).

ex. rmdir homer removes or erases the directory
"homer" from the current directory.

cat

cat stands for catalog, and is used for displaying files to the screen (similar to DOS's "type" command). "cat" allows single screen paging: it waits for a response before displaying the next screen of information.

Ex. cat readme.txtthis will display the file readme.txt
onto the screen one page at a time.

cp

cp stands for copy. It is used to copy files similar to DOS's "copy" command.

Ex. cp readme.txt springfield.txt copies the file
readme.txt and names the new file springfield.txt

mv

mv stands for move. It is used to move files from one directory to another, and to rename files.

Ex. mv readme.txt /home/x-filesmoves readme.txt from current directory
to directory called /home/x-filesEx.mv agent.sculley agent.muldar renames file agent.sculley
to newname agent.muldar

rm

rm stands for remove. It is used to delete files (similar to the DOS "del" command). It will verify that you want to delete the file. Wild cards can be used with rm.

Ex.rm dana.sculley deletes file named dana.sculley

cd

cd stands for change directory. It is used to change the current directory (similar to DOS's "cd" command). Note: use of the double dots requires a space - cd ..

Ex.cd /x-files/fox.muldarMakes /x-files/fox.muldar the current directory

51b. Access and Permissions

The owner of the file or directory determines who can access the file and for what purposes. The type of access can be read, write, or execute.

User/Group/Other

With respect to file and directory access, the user community is divided into 3 categories: user (or owner), group and other.

user (u)The owner of the files or directories group (g)Group members.
Groups are users who agree to share certain files and directories.
Groups are usually formed along project or business organizational lines.other
(o)All other users of the system.

Each file has a set of values stored in its inode that specifies its permissions. An inode is an entry in the table of inodes that describes the file or directory. The Table of Inodes is Unix's version of the DOS File Allocation Table.

Read/Write/Execute

The permissions indicate, for each category of user, the kind of access that's allowed. Permission is also called the file's protection mode, or simple mode.

TypeFile ActionDirectory Accessread (r)
Allows file to be viewedAllows directory to be listedcopied and printedwrite (w)
Allows file to be moved,Allows files to be created in removed and modifieddirectoryexecute
(x)Allows file to be run as aAllows directory to be searchedcommand

Unix displays a file's permissions in the following order:

rwx where "r" is read, "w" is write and "x" is execute

It is also expressed in Octal Code:

ModeOctalBinaryDescriptions---0000No permissions allowed
--x1001Execute only-w-2010Write only-wx3011Write and execute onlyr--
4100Read onlyr-x5101Read and execute onlyrw-6110Read and write onlyrwx7111Read,
Write and Execute

It is important to know the octal code or how to figure out the octal code if you need to change permissions.

When listing a directory using the "ls -l" long directory listing, the files permissions will appear:

ls -ltotal 8drwxr-xr-x2rockyother96
Dec 26 23:16.drwxrwxr-x7rootsys96Dec 2407:40 ..-rw-r--r--1rockyother613
Nov 212:30readme.txtdrwxr-xr-x2rockyother234Feb 2803:40x-files

The long directory lists all of the rights associated with the file or directory. The mode bits are organized as follows:

The default permissions (when you create a file) are 777, which is 111 111 111 in binary or (rwxrwxrwx). When a directory is created, the default permissions are 666, which is 110 110 110 or (rw-rw-rw-).

Changing Permissions

When a file is first created, it is created with the default permissions 777 (rwxrwxrwx). This means that anyone can read, write, or execute the new file. Unix provides a command to modify the default permissions: umask. "umask" works by deselecting the permissions that you do not want from the default permissions. "umask" by itself reports what the current mask is.

Exumaskwill report current mask000000
indicates no mask and default permissions existExumask 027
will set the mask to 0278 or 000 010 1112000  010  111777
(default permission)rwx  rwx  rwx027 corresponds to
---  -w-  rwx permissions deselectedResulting permissions:
rwx  r-x  ---user has rwx(all permissions) 000group has
r-x(read and execute only) 010other has ---(no permissions) 111

The umask command is used during your shell startup script or login script. You set it once during logging into the system, and normally won't have to use it again.

chmod

If you need to change a resource (file or program rights) , use chmod to alter the permissions. chmod is the more common method of changing permissions. You can alter the permissions 2 ways: use the read/write/execute switches, or use octal coding.

Ex.chmod +r index.txt changes the permission for user,
group & other to read.chmod u +r index.txt changes
the permission for only the userchmod 755 index.txt changes
the permission to rwxr-xr-x

Notice that chmod works the opposite of unmask: you set the permissions that you want.

Changing Ownership & Group

You can change the owner of a file by using the chown (change owner) command. You must be the current owner of the file to change its owner.

Ex. chown bullwinkle help.rocky.txt
This changes the owner of the file help.rocky.txt to "bullwinkle"

Similarly, you can change the group identification of the file by running chgrp (change group). You must be the owner of the files, or a user who has group write privileges, to change the group ownership of files.

Ex. chgrp brains mr.peabody.doc
This changes the group of the filemr.peabody.doc to the group "brains".

Note: You can remove a file that you don't own if it is in a directory where you have write permission.

Links, Instances & Processes

Links

Links are aliases that point to other files (that can be on the same filing system (Unix), or across several filing systems). Linking a file is a cross between renaming the file and copying the file to your Home directory. When you link a file, you add a second name to the file (to the operating system, it looks like there are two files). A link is actually in a directory, like a file is, but whenever a program tries to get at the file the link represents, Unix sends the program to find the "real" file.

A Soft Link can span file systems (two different servers). If you delete the original file, you delete the Link.

A Hard Link is restricted to two items on the same file system (same server). If you delete either name, the original item is still there (under the remaining name only).

You can use a link to shorten typing by linking a file (with a long path name) to your current directory. For example: if you want to run the file /usr/local/scripts/startup.ksh from your home directory without typing the entire name, you can create a link to it in your home directory called "Startup".

ln -s  /usr/local/scripts/startup.ksh Startup

"ln" by itself is the command to create a hard link; the "-s" option makes it a soft link.

To delete a link, remove it the way you would an ordinary file, using "rm". You are actually just removing the link, not the original file.

Instances & Processes

A process is an execution environment that's set up by the operating system kernel. A process consists of these 3 major components:

Unix is a multi-tasking environment. This means that multiple processes can be run simultaneously (sometimes processes are called sessions). If a program is being run by more than 1 user at a time, each process that is running the program is called an instance.

Example: If the E-mail reader program "pine" is run, it is considered a process under Unix. If another person runs "pine" at the same time, we say that there are 2 instances of "pine" being operated.

Processes are also called "jobs".

ps - Process Status

In order to see which processes are operating under your account, type "ps". "ps" stands for Process Status, and will display all processes that are currently operating.

psPIDTTYTIMECOMMAND168521a0:02ksh168891a0:00ps

When the "ps" command is entered, a display similar to the above will be displayed.

The "ps" command is very useful in determining if you have safely exited a program. Quite often in Unix, you may get bumped from a process (such as ftp), and end up disconnected. The ftp process may still be running. When you log back on, check to see what processes you have open by using "ps".

kill

If you were bumped out (or locked up in a process), you can usually escape back to the command line by doing one or all of the following key stroke commands:

ctlr dEnd input (End of file)ctrl zPause
Jobctrl cKill jobctrl xQuit program

In order to terminate the process, you would first use the "ps" command to find out if the process is still running (and what PID is associated with it).

Example:

While using the e-mail package "pine", it locked up, and the above key combinations were pressed in a mad frenzy. Fortunately, we were returned to the command line prompt. "ps" was entered to see what the process status was:

psPIDTTYTIMECOMMAND168521a0:02ksh168891
a0:12pine169571a0:12ps

This indicated that "pine" was still running. In order to terminate or stop "pine", the kill command was used with the PID of the process that we want to terminate:

kill 16889This will kill "pine"

Unfortunately, the kill command by itself will not work on all flavors of UNIX. Sometimes you may have to use some extra options, such as the following:

kill -kill 16889

You can escalate the "degree" of kill by using the option "-9." And if that doesn't work, then perform the kill on steroids "-15":

kill -9 16889kill -15 16889

Background Processing

All Unix systems have the ability to simultaneously run multiple commands. The process that is currently displayed on the screen is said to be running in the foreground. Other processes (by the same user) are said to be running in the background (not currently displayed on the terminal).

Job Control is used to manage multiple processes, and allows users to manipulate foreground and background processes.

To run a process in the background, add "&" to the end of the normal command:

sleep 120&

In the above example, the sleep command causes the UNIX terminal to do nothing for 120 seconds. The "&" indicates that it is performed in a background process.

Moving a Foreground Process into the Background

In order for a foreground process to be moved into the background, it first must be running in the foreground. The foreground process must then be stopped by using "ctrl z" (which is the pause command). Once stopped, the bg (background) command can be used to start the process running again.

Example:

pr index.txt(prints the file index.txt to standard output)
ctrl z(stops process - pauses)stopped(Unix replies with "stopped" message)
bg(instructs process to move to the background)[1]
pr index.txt(displays process moved to background)

To display currently running jobs, type the following:

jobs[1]RUNNINGpr index.txt

Moving a Background Job to the Foreground

To move a background job or process to the foreground, you must know the job number. The number that is in square brackets [ ] indicates the job number.

jobs[1]RUNNINGpr index.txt

To move the background job to the foreground, use the fg (foreground) command shown below:

fgOR fg1 ORfg %1(1 indicates job number)

This will move job #1 to the foreground. The fg command will be implemented just a little bit differently, depending on the shell or flavor of Unix run.

Shell Programs

Unix allows each individual user to select a customizable command line interpretor. The standard Unix shell, called the Bourne shell (after its author), is called sh: it resides in the /usr/bin directory.

The shell is a program that, like any other program, can be replaced. The shell is a command interpreter and a command programming language. It prompts for, reads, and executes commands. The commands can come directly from a terminal, or from a file.

csh /ksh

All Unix operating systems have the standard Unix shell "sh." Two alternative shells, csh and ksh, are also available (and widely used). "csh" was developed by the University of California at Berkley. "ksh" was developed by AT&T and the "k" stands for Korn (for the developer David Korn). Linux uses a shell called bash, for "Bourne-again" shell. It has these added refinements from both csh/ksh shells.

Features of the "csh"

The Korn shell (ksh) uses the best features of the csh, with the following enhancements:

ksh enhancements:

The ksh uses the .profile file for startup configuration.

Aliases

Depending on the shell that you are using, you will have a startup file called either .cshrc (csh) or .profile (ksh). These files contain startup information for the user. Each user has a startup file in their home directory that can be modified to suit their individual needs.

Aliases are a means of individualizing a user's account. The user can use aliases as a macro to shorten command-line entries. In the startup file, there is an area with the heading "# handy alias". The "#" indicates that this is a comment, and is ignored by the operating system. Following this heading is a list of aliases as follows:

alias vt100="set term = vt100"

This line substitutes "set term = vt100" for when you type "vt100" at the command-line. "set term = vt100" is how you tell the Unix system what type of terminal you are emulating. Depending on the flavor of Unix that you are running, you may or may not need the "=" sign after "vt100".

Notice: There is an alias for "help": it is the Unix command "man" (manual).

Communicating with Other Users

You can communicate with other users within Unix by two methods: write and talk.

The write command communicates on the command-line. To finish communicating use "ctrl d".

The talk command splits the screen in two. Top of the screen is the destination's half of the conversation. The bottom is the source's half. To finish communicating use "ctrl d" or break.

To initiate either method, type the command, followed by the person's user name. For example:

talk blanchas

To stop users from communicating with you, type the following:

mesg -n

To allow access:

mesg -y

To see current status:

mesg

Creating Users and Groups

password file

User information is contained in a text file--called password-- that is normally located in the /etc directory. The file is often modified by a program called adduser, or something similar. The text file password consists of the following information for each user:

eblanchard:1v2B3uWaA.8iA:501:237:
Eugene Blanchard:/home/eblanchard:/bin/bash"eblanchard" is the user name.
"1v2B3uWaA.8iA" is the user's encrypted password."501" is the user's numerical ID
(must be unique)"237" is the group numerical ID that the user belongs to.
"Eugene Blanchard"is the full name of the user.
"/home/eblanchard" is the home directory of the user.
"/bin/bash" is the shell program that the user uses.

The password file is available to all users to view. This is because some programs require access to the password file.

Note: user names longer than 8 characters can cause problems in some programs. For example, the user name "eblanchard" cannot telnet into a Linux server from a Win95 telnet client. The user name is truncated to "eblancha" and the Linux server doesn't recognize it as a valid user name.

Changing your password

To change your password, you type the password command. Depending on the flavor of Unix that you are using, it may be one of the following:

The password command will prompt you for your existing (old) password, then ask you to type in your new password once (and then once again) to confirm it. Choose a password that is at least 8 characters long (use alphanumeric characters), and a combination of upper and lower case letters. Write down you password in a safe place!

group file

The group file is a text file that contains information about the groups that are allowed to use the system. It is located in the directory "/etc." Groups are added using a program called addgrp , or something similar. Each line in a group file contains these four fields:

instructors:NONE:237:eblanchard,dspurgeon,hsylvan
"instructors"is the group name"NONE"indicates that there is no password with this group.
It is limited to the listed users."237"is the numerical group ID" eblanchard,
is the list of group members which can be users or other groups

shadow file

Unix passwords are a maximum of 13 characters long, and can be encrypted in 4,096 different ways. There are hacking programs that are able to decode the passwords in the password file. In order to fix this security leak, Unix operating systems (that are System V compatible) have another file, called the shadow file, that works with the password file.

The password file does not contain the encrypted passwords. Instead, it points to the shadow file, which contains the encrypted login passwords. The password file will have a blank space where the encrypted password usually resides.

The password file is still available for all users to view but the shadow file is restricted so that only the system administrator can view it. In this manner, the security loop hole has been fixed.

If this section was helpful, why not donate to further development?

Donate $1.25.

Donate $2.50.

Donate $5.00.