Kennesaw State University



Chapter 2 Linux Command Line Basics

2.1 Overview

System administrators are responsible for server reliability, performance, and security. To fulfill these responsibilities, a system administrator must be able to monitor and control a computer system. System administrators control and monitor Linux servers by issuing commands. He or she can issue the commands directly using the command line interface or indirectly using a GUI interface. The command line interface is nearly identical in every Linux distribution. Linux distributions include one or more GUI desktop managers. The most common managers are GNOME and KDE. The appearance and behavior of the desktop environment varies considerably depending on which desktop manager is selected.

By default, when you start Fedora it opens to the GNOME interface (If you are not already logged into Fedora, you should login using an account without root privilege). The examples in this chapter primarily use the command line interface. To follow along with the examples you must access a command line prompt. At the top of the Gnome desktop is a menu bar. This menu bar has menus for Applications, Places, and System. The last entry in the applications menu is System Tools. When you place your mouse over this entry a submenu appears. In the System Tools submenu select “Terminal” to access the command line in Fedora.

The Linux command line interface provides users with an easy, quick, efficient way to manage and use the Linux machines. In fact, many Linux servers don’t even have a GUI installed, or GUI interfaces are not available for remote access and remote administration. In addition, the Linux GUI interface does not cover all system functions, and it may take many steps to implement a command line action. Linux administrators need to have a good understanding of Linux commands and know when and how to use them in the command line interface. Most Linux server administrators prefer the command line rather than GUI interface for most activities. This includes day to day operations and job automations such as script for backup and recovery, system initializations, configuration, and performance monitoring.

Linux provides a rich set of commands for system administrators to use. This chapter will only discuss the day to day basic operation commands. Commands specific to networking, process, system automation, and security will be discussed in other chapters.

When you launch a terminal window as described above, most of the window will be blank except for the prompt where you type in a command to be executed. In the example below the terminal window initially displays “[john@localhost ~]$”. The beginning is an indication of the username. In this case,” john”. After the @ is an indication of the machine currently connected to. In this case, “localhost” means the machine you are currently sitting at. If you had connected to a remote host, its name would be displayed after the @ sign. Following this is an indication of the current working directory. The tilde is a symbol indicating the user’s home directory. The $ sign indicates that we are logged in as an unprivileged user. A root user would have # sign for its prompt instead of a $.

To test the command line interface, type in a simple command: whoami followed by a return or enter. The computer should respond by displaying the username of whoever is logged in. Note that in Linux all commands are case sensitive; so if you enter Whoami or WHOAMI, you will receive a “command not found” notice.

[john@localhost ~]$ whoami

john

[john@localhost ~]$

Sometimes you will get a “command not found” error even when you have spelled a command correctly and used the right case. This happens when the command is not found in the search path. You can run these commands by specifying their location, if you know the location. To find the location of a command use the whereis command as shown below.

[john@localhost john]$ useradd

bash: useradd: command not found

[john@localhost john]$ whereis useradd

useradd: /usr/sbin/useradd /usr/share/man/man8/useradd.8.gz

[john@localhost john]$ /usr/sbin/useradd

The useradd command does require privilege to run. If you do not have the necessary privilege you will get a “Permission denied” error message.

To obtain privileges in a terminal session, you can switch to the root user by typing su at the shell prompt. The computer will prompt you to enter the password of the root account.

[john@localhost ~]$ su

Password:

After you provide the root password you become a root user. Note the “#” sign in the prompt. Verify that you are logged in as the root user by typing whoami at the prompt.

[root@localhost john]# whoami

root

As root you have the proper privileges to run useradd. However, if you try to run useradd by typing /usr/sbin/useradd at the prompt you will get a message about improper usage, because various options need to be specified when adding a user.

You do not need root privilege to do most of the commands in this chapter so logout of the root account by typing exit at the prompt.

[root@localhost john]# exit

exit

[john@localhost ~]$

To determine the proper usage and syntax of command use the man command. The man command will explain a command and the options that can be specified when running the command. Type man useradd at the shell prompt.

[john@localhost john]$ man useradd

Examining the explanation of useradd may be a little confusing. You can scroll down in the man page using the down arrow (moves one line) or the spacebar (moves down a screen at a time). Scroll up using the up arrow. When you have finished reading the man page type the letter q to quit. Useradd is a command that is probably best run using a script file or by using a graphical interface. You did this in the last chapter when you added bill and susan as users. For most people, it is easier and less error prone to click the add user button and fill out the form in the graphical interface, rather than specifying all the options on the useradd command.

Once an account is setup the user may want to change the password of the account. Users can change their passwords at anytime using the passwd command. Usage of the passwd command is illustrated below. Password values typed in are not echoed on the screen to prevent others from reading the value that the user types in.

[john@localhost ~]$ passwd

Changing password for user john.

Changing password for john.

(current) UNIX password:

New UNIX password:

BAD PASSWORD: it is too short

New UNIX password:

BAD PASSWORD: it is based on a dictionary word

New UNIX password:

Retype new UNIX password:

passwd: all authentication tokens updated successfully.

After the system validates the old password and confirms the new password the computer will update your password. This is done by changing the value stored in the etc/passwd file for the account.

2.2 Files and Directories

2.2.1 Listing, Making, and changing directory

Managing a Linux system consists primarily of managing files and processes. We will discuss process manipulation commands later in this chapter. This section looks at file system commands.

In Linux, all data files, program files, directories, and devices are called files and treated the same way by the operating system. The files and directories (which are also files) are organized in an inverted tree type hierarchical structure. The top of the hierarchy is called root (represented by a slash / ) . Here are the subdirectories under the / directory that you should have in your newly installed Fedora Linux system.

bin dev home mnt proc sbin srv tmp var boot etc lib media opt root selinux sys usr

A number of these directories are important. The /bin directory contains programs (in the form of executable files called binaries) that the system needs to operate such as the shells and some of the shell commands, such as ls and grep. Configuration files such as the passwd file are located in the /etc directory. The home directory is the parent directory of all user home directories except the root which has a dedicated home directory called “root”.

2.2.1.1 Listing and Changing Directories

ls (list) command lists the contents of your current working directory. When you login, your current working directory is your home directory. This directory will have the same name as the account used to login. A user’s home directory will be located in the /home directory of the file system. You should save your own files and any subdirectories that you create within your home directory.

cd (Change Directory) command is used to change the current working directory. The root user can freely move every where within the system but a regular user can’t move to another user’s home directory unless he is granted the permission.

You can verify that the root account can view your directory by the su command to become root and then issuing the ls command at the prompt. This is shown below to list the contents of John’s directory.

[john@localhost ~]$ su

Password:

[root@localhost john]# ls

Desktop Documents Download Music Pictures Public Templates Videos

If you want to find detailed information about the contents of John’s home directory, specify the –l option.

[root@localhost john]# ls -l

total 32

drwxr-xr-x 2 john john 4096 2008-05-20 08:50 Desktop

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Documents

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Download

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Music

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Pictures

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Public

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Templates

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Videos

The option “–l” shows a long listing including

Column 1: the permissions of the directory or file

Column 2: the number of links or directories within the directory; for a directory, the default amount of directories is 2 because of the . (current) and .. (parent) directories

Column 3: The owner of the file or directory who created it or is re-assigned as the owner.

Column 4: the group owner assigned to the file or directory

Column 5: size of the file or directory

Column 6: date of last modification

Column 7: time of last modification

Column 6: the name of the file or directory.

Switch to using John’s account by typing exit and reissue ls –l command to verify that you get the same results.

[root@localhost john]# exit

exit

[john@localhost ~]$ ls -la

total 32

drwxr-xr-x 2 john john 4096 2008-05-20 08:50 Desktop

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Documents

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Download

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Music

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Pictures

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Public

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Templates

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 Videos

The ls-l command does not list all the files in John’s home directory. To see the hidden files use –a option to request that all files be included in the listing. Below, both l & a are specified as options by entering ls –la at the command prompt. Only part of the resulting list is shown. When you enter it on your system, you may want to scroll back to review all the files listed in the directory.

[john@localhost ~]$ ls -la

total 152

drwx------ 26 john john 4096 2008-05-21 21:54 .

drwxr-xr-x 3 root root 4096 2008-05-15 04:55 ..

-rw------- 1 john john 53 2008-05-20 15:22 .bash_history

-rw-r--r-- 1 john john 18 2008-02-29 09:27 .bash_logout

-rw-r--r-- 1 john john 176 2008-02-29 09:27 .bash_profile

-rw-r--r-- 1 john john 124 2008-02-29 09:27 .bashrc

drwxr-xr-x 2 john john 4096 2008-05-15 12:00 .config

. . .

[john@localhost ~]$

There are additional options for the ls command. You can find out about the available options by typing man ls at the shell prompt.

It is useful to be able to switch the current working directory. Linux provides the cd command to change directories. Some common ways to change to a desired directory are given below.

cd / moves to the root directory.

cd or cd ~ without destination moves user to his home directory.

cd .. moves user to the parent directory of the current directory.

cd moves user to the target directory.

You can use cd ~/subdir1 to move you to the sub directory “subdir1” in your home directory from anywhere. After you move to a new directory, you can use pwd (Present Working Directory) command to check the directory you are currently in.

Here are some examples.

The root user moves from john’s home directory to his parent directory /home.

[john@localhost ~]$ su

Password:

[root@localhost john]#

[root@localhost john]# cd ..

[root@localhost home]# pwd

/home

The root then moves to susan’s home directory.

[root@localhost home]# ls

bill john susan

[root@localhost home]# cd susan

[root@localhost susan]# pwd

/home/susan

Any user home directories can be referred to by the tilde (~) character no matter where you are. If you want to list the files in the myApp subdirectory of your home directory, you could use this command.

ls ~/myApp

When listing files or doing other actions on a computer, it is sometime convenient to not fully specify the file name. Wildcard characters are used in these situations. Wildcards such as * or ? tell the computer it can substitute other characters into a particular position when matching the string. The wildcard character * says you can substitute any number of characters (including no characters) into in a file (or directory) name. The wildcard character * y itself would match all files and directory names in the current working directory.

The file command reports what type of file a specified file is. Some possible file type types are: directory, ASCCII text, and executable. To specify that you would like to know the file types of all files in a directory use the wildcard *. Here is the result of entering file * at the prompt.

root@localhost john]# file *

abc: empty

Desktop: directory

Documents: directory

Download: directory

file1: empty

file3: ASCII text

js: ASCII English text

Music: directory

mydir: directory

mytest.sh: ASCII text

Pictures: directory

Public: directory

Templates: directory

Videos: directory

Tying file D* will report the file type of all files in the current directory starting with an uppercase D*. If any files in the directory had a name of “D” with no additional letters, it would be included in the files reported on by the file command/

file *D

Would report on any files in the current directory ending with an uppercase “D” including any files named “D” with no preceding letters.

The ? wildcard character matches exactly one character. Thus abc? matches files like abc0 or abc1, but not abc without any letters after it of abc00 with two characters after abc.

2.2.1.2 Permissions

Linux is a multi-user operating system so it is important that the permissions be set on files and directories to allow appropriate users to share files, but prevent other users from accessing files. By default an unprivileged user cannot access files in another user’s home directory. Access is prevented by the use of access controls called permissions in Linux. Each file or directory is restricted by permission settings. Linux sets permission for three categories: user (owner), group (this category includes members of the same group as the owner), others (anybody else). The owner of a file has whatever permissions were granted to the user. Members of the same group as the owner have the access rights specified by the group permission settings. Any user who is not the owner or a member of the group have the access rights specified by the permissions assigned to others.

|File type |Definition |

|- |Regular file |

|d |Directory |

|l |Link |

|b |Block device |

|c |Character device |

Table 9.1 Valid file types

When you typed ls –l or ls –la earlier, you saw the permissions for each file or directory listed. Each entry for a file or directory began with something like “drwxr-xr-x” A “d” in the first column indicates that an entry is for a directory. If a line starts with the letter “l” that entry is a symbolic link (links will be discussed later in the text). If a line starts with a hyphen instead of a letter that entry is a regular file. The next 9 characters, are either the letters “r”, “w”, “x” or a hyphen. These characters indicate who can read, write, or execute a file. The first three permission characters indicate what the owner who created the file can do: r (read), w (write), and x (execute). A hyphen tells no permission in its category. The next three letters indicate the permissions for the group the file is assigned to. The last three letters are used for anybody else.

[pic]

Figure 9.1 A directory with permissions set to 766

When you create a file in your home directory, the file has a default permission of -rw-rw-r-- . The owner’s permissions are “rw-“, which means the owner can read and write the file but cannot execute it. If you want to be able to run the file, you must change the permissions of the file. Linux provides the chmod (change mode) to reset the permissions on a file.

To demonstrate the use of the chmod command we will create a short script and try to run it. Create mytest.sh shell using the cat command. The >mytest.sh after cat redirects what you type on the next few lines into a file called mytest.sh. Type the next two lines as shown below: ca shows the calendar and who | wc –l reports the number of login users on this server.

[john@localhost ~]$ cat>mytest.sh

cal

who | wc -l

After typing these two lines press the “Ctrl” key and the letter “D” at the same time (We will abbreviate this key combination as ^D). Entering a ^D indicates you have finished your entry and the text entered is saved to the mytest.sh.

Now try to execute the script as follows:

[john@localhost ~]$ ./mytest.sh

bash: ./mytest.sh: Permission denied

It should fail to execute and give you a “Permission denied” error message. Check the permission of this file as follows.

[john@localhost ~]$ ls -l mytest.sh

-rw-rw-r-- 1 john john 14 2008-05-24 16:37 mytest.sh

The permission indicates that no one has execute permission, not even the owner.

Now add the execute privilege for the owner using the chmod command. u indicates that you are changing permission for the owner (user) of the file and the +x that you want the owner to add execute on the specified file. Then check the permissions using ls –l.

[john@localhost ~]$ chmod u+x test.sh

[john@localhost ~]$ ls -l mytest.sh

-rwxrw-r-- 1 john john 14 2008-05-24 16:37 mytest.sh

Some system administrators prefer to use an octal number to specify the permissions assigned using the chmod command. The octal equivalent of rwxrw-r-- is 764. You obtain this by determining the binary and then converting to octal. To get the binary, set each letter to a 1 and any hyphens to zero. Thus rwxrw-r-- becomes 11110100. 11110100 in binary is equivalent to 764 in octal. Thus we could have set the permissions on mytest.sh, using this command: chmod 764 mytest.sh

|Number |Equivalent |Definition |

|0 |--- |No permissions |

|1 |--x |Execute only |

|2 |-w- |Write only |

|3 |-wx |Write and execute |

|4 |r-- |Read only |

|5 |r-x |Read and execute |

|6 |rw- |Read and write |

|7 |rwx |Read, write, execute |

Table 9.2 Numeric permissions

Once execute is granted to the owner using either of the chmod syntaxes given above, the owner should be able to run the script as follows:

[john@localhost ~]$ ./mytest.sh

May 2008

Su Mo Tu We Th Fr Sa

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30 31

3

Other unprivileged users should not be able to run the script. For instance, if you use su to login as susan and attempt to run the program it will fail to execute due to the lack of permission.

[john@localhost ~]$ su susan

Password:

[susan@localhost john]$ ./mytest.sh

bash: ./mytest.sh: Permission denied

[susan@localhost john]$ exit

To allow anyone to run the script, specify +x with the chmod command.

[john@localhost ~]$ chmod +x mytest.sh

[john@localhost ~]$ ls -l mytest.sh

-rwxrwxr-x 1 john john 14 2008-05-24 16:37 mytest.sh

This allows susan to run the script.

[john@localhost ~]$ su susan

Password:

[susan@localhost john]$ ./mytest.sh

In Linux, a directory is a special kind of file. Like other files you can set the permissions of a directory. The read access for a directory is the permission for browsing the directory and listing files in the directory. The write access of a directory is the permission for deleting or copying files in the directory. Execute access is for permission to move into the directory and read files if the read access right is granted for the file to be read. You must have execute permission on a directory hosting the file to be able to read a file.

With many files and directories on a system it can be a lot of work to reset permissions on each individually. Linux provides a recursive option to apply mode changes to all subdirectories in a directory tree. The recursive option of chmod is invoked by specifying –R on the command line.

The chmod command lets you change permissions for a group by specifying g when setting permissions (for instance: chmod g+rwx). Sometimes a file belongs to the wrong group. Linux provides a chgrp(change group) command for these situations. As shown below chgrp is used to change the group owner of mytest.sh to the group linux. Then chmod is used to give the group read, write, execute permissions to the group owner of mytest.sh.

chgrp linux mytest.sh

chmod g+rwx mytest.sh

If you need to change the owner of a file, use the chown(change owner) command to specify a new owner.

Let’s practice the commands you have learned so far. Here susan moves to the home parent directory and checks the permissions of the all user home directory. The user susan then fails to move to bill’s home directory due to the lack of the required permission. The su command is used to switch to the root user, which is able to move into bill’s directory because of privileges associated with the root account.

[susan@localhost john]$ cd ..

[susan@localhost home]$ ls -l

total 12

drwx------ 4 bill bill 4096 2008-05-23 22:07 bill

drwxrwx--- 30 john linux 4096 2008-05-24 18:34 john

drwx------ 4 susan susan 4096 2008-05-24 16:45 susan

[susan@localhost home]$ cd ..

[susan@localhost /]$ pwd

/

[susan@localhost /]$ cd

[susan@localhost ~]$ pwd

/home/susan

[susan@localhost home]$ cd bill

bash: cd: bill: Permission denied

[susan@localhost ~]$ su

Password:

[root@localhost susan]# pwd

/home/susan

[root@localhost susan]# cd /home/bill

[root@localhost bill]# pwd

/home/bill

[root@localhost bill]# cd ..

The root user changes the group owner of the bill directory from bill to linux and changes the bill home directory permission to executable for group owner. Since susan is a member of linux group, susan can move into the bill directory now.

[root@localhost home]# chgrp linux bill

[root@localhost home]# ls -l

drwx------ 4 bill linux 4096 2008-05-23 22:07 bill

drwxrwx--- 30 john linux 4096 2008-05-24 21:44 john

drwx------ 4 susan susan 4096 2008-05-24 21:45 susan

[root@localhost home]# chmod -g+x bill

[root@localhost home]# ls -l

drwx--x--- 4 bill linux 4096 2008-05-23 22:07 bill

. . .

[root@localhost home]# su susan

[susan@localhost home]$ cd bill

[susan@localhost bill]$

2.2.1.3 Create, Remove, and Copy File and Directory

1) Creating Directories and Files

Linux provides a number of commands that system administrators can use to create files and directories. These include mkdir, touch, editing a new file, and cat. Once you have created files on a system you may need to move or copy them to new locations. When files are no longer needed they should be deleted. Linux also provides commands for moving, copying and deleting files. These file manipulation commands are covered in this section.

To create a directory in Linux, use the mkdir (Make Directory) command. The syntax is mkdir followed by the name of the directory you want to create. For instance, if susan wanted to create a subdirectory in her home directory called dir1, she would navigate to her home directory using cd without any options specified and then use mkdir dir1 to create the desired subdirectory. She could then navigate to the new directory, which should be empty.

[susan@localhost bill]$ ls -l

total 0

[susan@localhost ~]$ mkdir dir1

[susan@localhost ~]$ ls -l

total 4

drwxrwxr-x 2 susan susan 4096 2008-05-29 12:15 dir1

[susan@localhost ~]$ cd dir1

[susan@localhost dir1]$ pwd

/home/susan/dir1

Creating a File

As noted above there are many ways to create files in Linux. One way is to use the touch command. If you specify a filename that does not exist Linux will create a new empty file with the specified filename. The intended use of touch was to change the timestamp of a file. If a file exists, it does not create a new file it only changes the timestamp associated with the file. Try using touch to create a file and then update its timestamp (you will have to wait a minute before issuing the second touch command to see the time change).

[susan@localhost dir1]$ ls -l

total 0

[susan@localhost dir1]$ touch file1

[susan@localhost dir1]$ ls -l

total 0

-rw-rw-r-- 1 susan susan 0 2008-05-29 12:34 file1

[susan@localhost dir1]$ touch file1

[susan@localhost dir1]$ ls -l

total 0

-rw-rw-r-- 1 susan susan 0 2008-05-29 12:35 file1

You can use any editor to create a file. Two very popular editor on UNIX systems are vi and GNU emacs (Editor MACroS). The vi is somewhat confusing for new users, but once you learn to use it vi is a powerful editing tool. To use vi to create a new file type vi myNewFile at the shell prompt. Type a or i to enter append or insert mode. Type in a few words while in the append or insert mode. When you finish typing the text press the esc key to exit from append or insert mode. Then type :wq to write your changes to the file and quit from vi editor. An online introduction to the vi editor for system administrators is available at

GNOME supplies its own editor gedit. It is the default editor so if you double click a text file on the GNOME desktop it will open in gedit. To invoke the editor from the command line type gedit followed by the name of the file you would like to edit. You can invoke the gedit editor from GNOME desktop menu by selecting Application -> Accessories -> text Editor.

Another technique frequently used in Linux to create files is the cat command. The cat command was originally used to concatenate multiple files and display them on the screen. It can be used to create a new file by the file redirection as we demonstrated in the previous section.

Linux allows users to redirect where input comes from and where error messages and output go. Most Linux commands write to the standard output (monitor screen) -stdout and take their input from the standard input (keyboard) -stdin, and the standard error - stderr is sent to the monitor screen by default.

The cat command normally takes what is in stdin and copies it to stdout. but you can redirect the stdout to a file by using the “>” symbol to redirect the stdout to the file named newFile as

[susan@localhost dir1]$ cat > newFile

Unix

Linux

Windows

Apple

When you finish typing the four words to be included in the file, use ^D (The ^ is used to indicate the control or ctrl key) to end the input.

To copy the contents of the file to the stdout (the monitor screen), so you can see it—type: cat newFile

If a file exists, using the “>” will erase the current contents of the specified file and replace it with the new text entered. If you want to append your data to the original contents instead of replacing it use “>>”. To add more items to the file newfile, type:

[susan@localhost dir1]$ cat >> newFile

Solaris

HP

Fedora

Remember to type ^D to terminate entry after you have typed Fedora. To read the contents of the expanded file, type: cat newFile

To combine the file you just created (newFile) with the file you created with the vi editor (myNewFile) and create a file called merge, you would use the cat command like this:

cat newFile myNewFile > merged

The “>” and “>>” symbols were used to redirect output, to redirect input the “ outfile

You can place multiple commands on a single line by separating them with a semicolon (;) as shown below.

; ;

Unlike with the pipe command, there is no connection between two consecutive commands separated by a “;” except the commands are executed in that sequence.

2.3 Linux File Systems and Storage

2.3.1 File Systems

In the installation section of Chapter 1, you briefly learned about the Linux file system and partitions. The advantage of partitioning is that it protects other partitions when corruption occurs. Each partition has its own file system. You can divide one large hard disk drive into multiple partitions so that you can have many partitions on one disk drive. You can also install multiple hard disk drives. Each device(CDROM, DVD, USB, etc.) is also a separate partition. In order to make any partition accessible each partition must have a file system and must be attached at a mount point by the mount command.

A mount point specifies the place of a partition in a file system. The root partition is mounted in the “/” mount point. All the other partitions are connected via the root partition. Once the partition is un-mounted from the file system by the umount command it will no longer be available.

There are two types of partitions: data partition and swap partition. The swap partition is used as an extension to the memory space and is accessible by system itself. The standard installation in Chapter 1 created three partitions: /(root), /boot,. and swap. This is a minimal default partition layout. The boot partition has all the necessary start up files. In case other partitions are damaged, this arrangement allows a computer system to still boot and startup. In custom installation, you can make many more partitions such as the home partition (mounted at /home) for user personal programs and data, the usr partition (mounted at /usr) for user application programs, the var partition( mounted at /var) for temporary data like mail queue and print queue, and the opt partition(mounted at /opt) for extra third party software.

During system startup, all partitions are mounted automatically as specified in the file system configuration file /etc/fstab. The partitions and their mount points can be viewed by df and other commands discussed in the next section.

Let’s take a look at the subdirectories in the root (/) directory in the file system.

   /bin     Common shared programs

   /boot     The startup files and the kernel,

   /dev     References to all the peripheral hardware devices

   /etc     Important system configuration files for system administrator to manage.

For example: /etc/fstab, etc/passwd

   /home     Home directories of the common users.

   /initrd     Information for booting. Do not remove! (on some distributions)

   /lib     Library files

   /lost+found Files restored after a system crash.

   /misc     For miscellaneous purposes.

   /mnt     Mount point for external file systems

   /net     Standard mount point for entire remote file systems

   /opt     Extra and third party software.

   /proc     System resources

   /root     The root administrator home directory which is different from /, the

/root is the home directory of the root user.

   /sbin     System executables and startup executables

   /tmp     Temporary space for use by the system and emptied when system restarts

   /usr     User related application programs, libraries, documentation

   /var     For all user created variable files and temporary files, log files, the mail

queue, the print spooler area, downloaded files

/media Mount point for media devices like usb

Among the list of subdirectories some of them are partitions and some others are simply subdirectory of / directory. The swap partition is not visible.

2.3.2 File System Commands

2.3.2.1 mount, df, du Commands

The mount command is used to mount a file system(partition) to a mount point and the umount command is to un-mount a partition from the mount point. The mount command without arguments just shows file system mounting status.

The syntax of mount is

mount

The syntax of umount is

umount

For example, you can mount a floppy drive and CDROM drive as follows:

mount /dev/fd0 /mnt/floppy

where mount point "/mnt/floppy" is set up in advance.

mount /dev/cdrom /mnt/cdrom

You can un-mount these drives as follows:

umount /mnt/floppy or umount /dev/fd0

umount /mnt/cdrom or umount /dev/cdrom

[root@localhost ~]# mount

/dev/sda5 on / type ext3 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

/dev/sda2 on /boot type ext3 (rw)

tmpfs on /dev/shm type tmpfs (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

fusectl on /sys/fs/fuse/connections type fusectl (rw)

gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)

/dev/sda1 on /media/disk type fuseblk (rw,nosuid,nodev,allow_other,blksize=4096)

/dev/sdb1 on /media/MINI-USB2BU type vfat (rw)

After the un-mount of the USB device partition from the mount point, the USB drive is no longer accessible.

[root@localhost ~]# umount /media/MINI-USB2BU

[root@localhost ~]# ls /media/MINI-USB2BU

[root@localhost ~]# mount

. . .

/dev/sda1 on /media/disk type fuseblk (rw,nosuid,nodev,allow_other,blksize=4096)

[root@localhost ~]# cd /media/MINI-USB2BU

[root@localhost MINI-USB2BU]# ls -l

total 0

[root@localhost MINI-USB2BU]# mount /dev/sdb1 /media/MINI-USB2UB

mount: mount point /media/MINI-USB2UB does not exist

Now, you can make a mount point for the USB drive and mount it again. The USB drive is available for access. Fedora can detect a newly inserted USB drive and take care of the mounting process automatically. This eliminated the need for a user to manually mount the drive in many situations.

bash: cd: /media/MINI-USB2UB: No such file or directory

[root@localhost /]# cd media

[root@localhost media]# mkdir /media/MINI-USB2UB

[root@localhost media]# mount /dev/sdb1 /media/MINI-USB2UB

[root@localhost media]# cd /media/MINI-USB2UB

[root@localhost /]#

df (Disk free)

The df command reports on the disk space left on the file system. Here is a report from running the df command where the –h option was specified for human being readable format.

[root@localhost MINI-USB2UB]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda5 18G 3.7G 13G 23% /

/dev/sda2 99M 12M 83M 13% /boot

tmpfs 501M 48K 501M 1% /dev/shm

gvfs-fuse-daemon 18G 3.7G 13G 23% /root/.gvfs

/dev/sda1 18G 2.0G 16G 11% /media/disk

/dev/sdb1 125M 101M 25M 81% /media/MINI-USB2UB

du (Disk Usage)

The du command outputs the disk space used by each subdirectory. It is useful when you want to find out which directory has the most files. To see the disk space usage on the root / directory, type:

[root@localhost MINI-USB2UB]# cd /

[root@localhost /]# du -sh *

6.0M bin

6.1M boot

124K dev

124M etc

11M home

87M lib

20K lost+found

2.0G media

4.0K mnt

0 proc

1.4M root

21M sbin

0 selinux

4.0K srv

0 sys

140K tmp

3.2G usr

74M var

The -s flag will display only a summary (total size) and the * is a wildcard indicating all files and directories should be included in the listing.

The free command displays information about free and used memory on the system including the swap partition usage.

[root@localhost /]# free

total used free shared buffers cached

Mem: 1025288 684156 341132 0 114116 156956

-/+ buffers/cache: 413084 612204

Swap: 2096472 0 2096472

mkfs (Make File System) command builds a Linux file system on devices such as a hard disk partition, CDROM, USB, etc.

mkfs [option]

where can be either the device name (e.g. /dev/hda1, /dev/sdb2) or the mount point (e.g. /, /usr, /home). This command should be run with a root privilege.

To avoid possible data loss--Do not actually perform the mkfs and fsck commands below!

The following command builds a file system for the hard disk drive based on the device name (hda1).

mkfs -t ext3 /dev/hda1

mkfs -t ext3 /dev/CDROM

This example creates an ext3 file system on CDROM drive.

fsck(File System ChecK) command is used to check and repair the file systems.

fsck [option]

where can be a device name (e.g. /dev/hdc1, /dev/sdb2), a mount point (e.g. /, /usr, /home), or UUID specifier. By default, fsck checks filesystems described in in /etc/fstab.

The fsck is called at boot time when the system detects that a file system is in an inconsistent state. You can run it manually for either interactively repairing damaged file systems or automatically deciding how to fix specific problems, or just for reviewing the problems.

2.3.2.2. User Disk Quota

In order to set up user disk quota you must configure and enable the file system for disk quota control. The following example show how to set up disk quota on /dev/sda3 mounted at /home so that each user will have its allocated disk quota.

Step 1) Edit /dev/fstab file system configuration file to enable the file system user quota support by adding “userquota” control to the /dev/hda3(/home) partition.

. . .

/dev/hda3 /home ext3 defaults, userguota 0 0

. . .

Make sure the quota record files ready. The quotacheck –cum check the file system usage in /home directory and create(-c) the user(-u) aquota.user quota record file without re-mount(-m).

[root@host1 john]# /sbin/quotacheck -cum /home

-rw------- 1 root root 7168 2008-05-30 09:03 aquota.user

Next, you remount the /home file system by the mount command with the remount option to mount it which is mounted already to make the quota control flag effective.

mount –o remount /home

You can check the /etc/mtab file to find out the updated quota control status.

[root@host1 john]# cat /etc/mtab

/dev/sda6 / ext3 rw 0 0

proc /proc proc rw 0 0

sysfs /sys sysfs rw 0 0

devpts /dev/pts devpts rw,gid=5,mode=620 0 0

/dev/sda3 /home ext3 rw,usrquota 0 0

/dev/sda2 /usr ext3 rw 0 0

/dev/sda1 /boot ext3 rw 0 0

. . .

Step 2) Use Quotacheck command to update the quota status

[root@host1 john]# /sbin/quotacheck –vguma

quotacheck: Scanning /dev/sda3 [/home] done

quotacheck: Old group file not found. Usage will not be substracted.

quotacheck: Checked 72 directories and 52 files

Step 3) Set the quota for user John by edquota command

Turn on the file system quota s

[root@host1 john]# quotaon –av

Check user John’s quota, it is not set yet

[root@host1 john]# quota -u john

Disk quotas for user john (uid 500): none

[root@host1 john]# PATH=$PATH:/sbin:/usr/sbin

[root@host1 john]# export PATH

[root@host1 john]# edquota -u john

Disk quotas for user john (uid 500):

Filesystem blocks soft hard inodes soft hard

/dev/sda3 700 200000 250000 114 0 0

You can edit this file to set soft limit(max blocks before warning when the grace period begins), hard limit(ultimate limit, and grace period.

Step 4) List: Check user john’s quota again.

[root@host1 john]# quota -u john

Disk quotas for user john (uid 500):

Filesystem blocks quota limit grace files quota limit

/dev/sda3 720* 200000 300000 7days 102 0 0

[root@host1 john]#

2.4 Process Basics

As was mentioned earlier, a Linux computer system consists largely of files and processes. A system administrator must know how to manage processes. A process is an executing program identified by a unique PID (Process IDentifier). The Linux system allows multiple processes to run concurrently sharing the CPU and other computer resources. Some processes run in the foreground and others run in the background. At any given time on a computer system, some processes may be temporarily suspended rather than running. To list processes on a Linux system and find their PID numbers, use the ps (Process Status) command.

The top command can also be used to check the resource consumption status of processes. The top command provides an ongoing look at processor activity in real time. It displays a listing of the most CPU-intensive tasks on the system, and can provide an interactive interface for manipulating processes. It can sort the tasks by CPU usage, memory usage and runtime. It pops up every 5 seconds. You can set the interval longer by specifying a command option at the command line. If you run top in interactive mode (the default), you can make adjustments while top is running by typing keys on the keyboard.

Some tasks may take a long time to complete, such as searching the entire file system to locate files with certain criteria using the find command. These tasks will hold up the terminal. Users have to sit and wait for the command to complete. Running a job in the background will bring the system prompt back immediately, and the user can resume working instead of waiting. To run a command in the background, include an & at the end of the command line. For example, to force the find command to be executed in the background, type:

find / -name linux &

[1] 4379

The user is notified of a job number (numbered from 1) enclosed in square brackets, together with a PID when the background command is issued. The user will be notified when a background process is finished. Backgrounding is useful for jobs that will take a long time to complete.

You can also background a current foreground process. You first suspend a process running in the foreground by typing ^Z, and then put it in the background by typing:

bg

Of course, a background job cannot interact with the user any-more. To see all running, backgrounded, and suspended jobs and their job numbers, type:

jobs

To bring a suspended process back to foreground, type:

fg

The command fg 1 will bring the find command suspended job to foreground. If you omit the job id number, the fg command defaults the last suspended process.

It is sometimes necessary to kill a process such as an infinite loop, non-responding, or a long time running process. In a GUI session there may be many foreground processes running at once. The GUI session might have a foreground process editing a file, another browsing the web, and still another for a game of blackjack. In a terminal session with a command line interface, there can only be one foreground process.

Now try out the process commands. For demonstration purposes we will use the sleep command. The sleep command does nothing but waits a specified number of seconds. Start two background processes that sleep for 60 seconds. Then start a foreground process for 60 seconds and suspended it with a ^Z. Then issue the bg command to resume the suspended process and bring it to the background. The jobs command should now show all three jobs running. Bring the second job to the foreground then suspend it with ^Z. Check on progress using the jobs command. Two jobs should be running and one is suspended (labeled stopped). Wait a few more seconds and reissue the jobs command. After the 60 seconds have elapsed the running jobs should finish.

[susan@localhost ~]$ sleep 60 &

[1] 17138

[susan@localhost ~]$ sleep 60 &

[2] 17139

[susan@localhost ~]$ sleep 60

^Z

[3]+ Stopped sleep 60

[susan@localhost ~]$ bg

[3]+ sleep 60 &

[susan@localhost ~]$ jobs

[1] Running sleep 60 &

[2]- Running sleep 60 &

[3]+ Running sleep 60 &

[susan@localhost ~]$ fg 2

sleep 60

^Z

[2]+ Stopped sleep 60

[susan@localhost ~]$ jobs

[1] Running sleep 60 &

[2]+ Stopped sleep 60

[3]- Running sleep 60 &

[susan@localhost ~]$ jobs

[2]+ Stopped sleep 60

[1] Done sleep 60

[3]- Done sleep 60

[susan@localhost ~]$ jobs

[2]+ Stopped sleep 60

[susan@localhost ~]$ kill %2

The suspended job will not finish. Thus, it remain on the system until it is resumed or killed. We can kill a suspended job typing kill %< job number>.

To kill a job running in the foreground, type ^C (control c). To kill a suspended or background process when you do not know the job id number, find its process’ PID and then kill it. To find a PID by its name use, pidof or search the list reported by ps –aux command and then type:

kill

If a process refuses to be killed, use kill -9 . The -9 specify that the kill is unconditional and no cleanup should be performed. A regular user can only kill their own processes, but root can kill processes of other users. Notice that many process management commands require root privileges.

|Control Key Shortcuts |Action |

|^a |Moves cursor to beginning of line |

|^c |Terminate current job |

|^d |Indicates end of file, can be used to indicate you have finished entering|

| |data, at the bash shell prompt it is the same as typing exit |

|^e |Moves cursor to end of command line |

|^k |Clears from cursor position to end of line |

|^q |Resumes scrolling |

|^s |Stops scrolling in a terminal window |

|^u |Clears from cursor position to beginning of line |

|^w |Clears word to the left |

|^z |Suspend current job |

Table 9.4 Some helpful keyboard shortcuts for command line interface

5. Summary

Using the command line is a powerful method to manage Linux servers. A large number of commands are available to monitor and configure servers. This chapter covered only a few of the commands that can be used to manage file, directories, file systems, and processes on a server.

Information on computers is stored in files. This chapter covered how to create the files and directories, and remove them when no longer needed. Systems administrators need to ensure that authorized people can find information quickly and without difficulties, but need to ensure that unauthorized users cannot find information. If a user already knows where files are located, the system administrator must prevent them from accessing or modifying these files. To make files findable on disk, the files are organized into directories and subdirectories. The find command can be used to locate files that meet certain criteria if you do not remember what directory a file is located in. The chmod command was used to set permissions to control access to files and directories. To protect data against corruption or loss it is important to backup it up using tar, GNU File Roller, Amanda, or some other backup utility.

Files and directories are stored on file systems that are created on disk partitions or other devices. Linux provides commands to mount and unmount file systems. The mkfs command is used to create a file system on a disk partition and fsck check is used to check and repair existing file systems. To determine file system utilization, the du and df commands are used.

Process management in Linux is important to ensure a high level of performance. To track processes in Linux, the operating system assigns each process a PID. To check the status of a process in Linux, use the ps command. The top command can be used to identify which processes on the system are consuming the most CPU time. Once a process is identified with a problem it can be suspended, sent to the background, or filled.

In next few chapters we will expand your knowledge of Linux commands. In the third chapter we will learn how to customize the Linux environment and automate routine system administration tasks. In the fourth chapter we will look at running system utilities from a graphical environment instead of a command line environment.

2.6 Self-Review Questions

1. Which Linux command can be used to create a new file?

a. cat

b. finger

c. gedit

d. touch

2. The top level directory in a Linux file system is called:

a. root and represented be a /

b. home and represented by %

c. dev and represented by #

d. fedora and represented by &

3. GNOME, as a component of GNU project, is primarily a command line environment

a. True

b. False

4. The whoami command is used to determine

a. the brand and model of a PC

b. the operating system version number currently installed

c. the username of whoever is logged in

d. the name of the CPU chip currently being used

5. If the command chmod 700 myFile.dat is issued, the myFile.dat can be accessed:

a. by the owner and users with sufficient privilege

b. by any member of the same group as the owner

c. by any user who can login

d. only by the owner

6. The command to switch to a user’s home directory, from any other directory is.

a. cd home

b. cd /usr

c. cd

d. cd ..

7. The rmdir deletes a directory and all the files contained in the dir.

a. True

b. False

8. The command used to search for files that meet a specified criteria is:

a. find

b. grep

c. search

d. hunt

9. The command used to unmount a device is.

a. remove

b. umount

c. unmount

d. none of the above

10. The command to stop a running process is.

a. remove process

b. delete process

c. stop

d. kill

11. The command to set the user disk quota limit.

a. quotacheck

b. edquota

c. quota

d. quotaon

12. Which file can tell the quota status.

a. fstab

b. mtab

c. both

d. none

13. The command to move dir1 of home directory from anywhere.

a. cd /dir1

b. cd dir1

c. cd ~/dir1

d. cd

14. The command to list hidden files in current working directory

a. ls –la .

b. ls -l

c. ls –l .

d. ls -la

15. The command to backup all files in current directory.

a. tar –cvf myarc.tar .

b. tar –tf myarc.tar .

c. tar –xvf myarc.tar

d. dump –cvf mytar.tar .

16. The command to restore all files in the myarc.tar to the current working directory.

a. tar –cvf myarc.tar .

b. tar –tf myarc.tar .

c. tar –xvf myarc.tar .

d. dump –cvf mytar.tar

17. The command to find all files which have not been modified

for more than one weeks.

a. find /type f –mtime +7

b. find /type f –mtime 7

c. find /type f –mtime -7

d. none

18. To copy the directory tree of dir1 to dir2.

a. . cp –r dir1 dir2

b. cp dir1 dir2

c. mv dir1 dir2

d. none

19.. The permission of rwxrwxrw- can be represented by numeric symble of

a. 776

b. 660 .

c. 554

d. 665

20. To dump a partition to a tape.

a. /sbin/dump -0u -f /dev/st0 /dev/sda1.

b. /sbin/restore -r -f /dev/st0

c. /sbin/dump -0u -f /dev/sda1 /dev/st0

d. dump –cvf /dev/st0 /dev/sda1

Keys to the self-Review Questions

1. D 2. A 3. B 4. C 5. A 6. C 7. B 8. A 9. B 10. D 11. B 12. C

13. C 14. A, D 15. A 16. C 17. A 18. A 19. A 20. A

2.7 Exercises

1. Login into Fedora using an unprivileged account. Start a terminal session as described in the chapter. At the shell prompt type: script ch2ex1.log

The script command is used to record a terminal session to the specified file (if no filename is specified the file will be called typescript. Now type the following commands:

who

whoami

whereis ls

whereis tar

whereis restore

weher is apachectl

where is mysql

where is php

cd /home/bill

su root

cd /home/bill

ls

whoami

exit

ls

exit

cal

Now examine the ch2ex1.log file in for ways. First be editing it with gedit, then by displaying to the screen using cat, then display on screen using the less command, finally display it to the screen using more. Which method worked better for reviewing the logfile?

If your instructor desires proof that you completed this exercise, print or email the logfile to the instructor. Your instructor may request that you create these logfiles for other assignments as well.

2. In a terminal session switch the user to susan using the su command. Then create three subdirectories: dir1, dir2, and dir3. Set the permission on dir1 to allow the owner read, write, execute access and no access for group or others. Set dir2 to allow full access for the owner; read, execute access for group; and no access for others. Set dir3 to allow full access for the owner; read, execute access for group; and no access for others. Change the group ownership of all files to linux

Create a file in each dir using cat. Switch to the bill user (The bill account should have been created in chapter 1 and assigned membership in the linux group) and then attempt to list the contents of each of the directories. Try to edit each of the files you created. Then attempt to create a file in each of the directories using the touch command. Use ls -la to determine who owns the created files and whether susan will be able to access them.

3. Create a link in bill’s home directory that references the file he created in /home/susan/dir2

4. In a terminal session, switch user to susan using the su command and navigate to her home directory using cd. Use the du command to obtain a disk utilization report. Disk utilization is reported in 512 byte blocks. Display disk utilization in bytes and then in “human readable” format. Use the man command to find out how to do this.

5. Create a new file in bill’s home directory using gedit. Type the following lines into the editor and be sure to press enter after each line:

The amazing combination of Linux, Apache, MySQL, and PHP (LAMP) is a powerful platform for Web development.

Apache is an amazing Web Server.

MySQL is an open-source database.

PHP can be used for scripting.

After you have entered these lines, save the file and return to your terminal session. Use head to display the first three line of the file. Use tail to display the last three lines of the file. Use sed to display lines 2 through 4 of the file. Use sed to display all the lines containing the word “Apache”. Use sed to replace all occurrences of the word “amazing” in the file with the word “astounding”. Use grep to display all the lines containing the word “astounding”.

6. Use the find command to locate all files owned by the mysql user in the /var directory and its subdirectories.

7. If your machine has a floppy drive, practice mounting and unmounting a floppy drive. If it does not have a floppy drive, use a USB drive.

8. Use the whereis command to determine the location of fsck. Issue the fsck command to repair the disk partition containing your home directory. Say no when asked if you want to continue.

9. Start several things running on your computer: a gedit session, a mozilla session, a sodoku (applications>>Games>>Soduko) session, three terminal sessions Use su so the terminal sessions are from different users. Use the following commands to examine processes running on the system:

ps

ps –a u

ps –A u |more

10. Use top to list processes on the system. Review the process list for the process running the top command. Kill the top process from another terminal session (session must either be from the same user or have root privilege). Then reissue the top command and. This time kill the top process by typing a k and the process id number in the session running top.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download