Mount Unmount
Mount Unmount
Mount Unmount
Your hard drive can have various partitions which usually contain only one file system, such
as one file system housing the /file system or another containing the /home file system.
One file system per partition allows for the logical maintenance and management of
differing file systems.
Directory Structure
Unix uses a hierarchical file system structure, much like an upside-down tree, with root (/) at
the base of the file system and all other directories spreading from there.
A Unix filesystem is a collection of files and directories that has the following properties −
It has a root directory (/) that contains other files and directories.
Each file or directory is uniquely identified by its name, the directory in which it
resides, and a unique identifier, typically called an inode.
The directories have specific purposes and generally hold the same types of information for
easily locating files. Following are the directories that exist on the major versions of Unix −
1 /
This is the root directory which should contain only the directories needed at the
top level of the file structure
2 /bin
This is where the executable files are located. These files are available to all
users
3 /dev
4
/etc
5 /lib
6 /boot
7 /home
8 /mnt
Used to mount other temporary file systems, such as cdrom and floppy for
the CD-ROM drive and floppy diskette drive, respectively
9 /proc
Contains all processes marked as a file by process number or other information
that is dynamic to the system
10 /tmp
11 /usr
Used for miscellaneous purposes, and can be used by many users. Includes
administrative commands, shared files, library files, and others
12
/var
Typically contains variable-length files such as log and print files and any other
type of file that may contain a variable amount of data
13 /sbin
14 /kernel
Now that you understand the basics of the file system, you can begin navigating to the files
you need. The following commands are used to navigate the system −
1 cat filename
Displays a filename
2 cd dirname
3 cp file1 file2
4 file filename
Finds a file/directory
6 head filename
7 less filename
8 ls dirname
9 mkdir dirname
10 more filename
Browses through a file from the beginning to the end
11 mv file1 file2
12 pwd
13 rm filename
Removes a file
14 rmdir dirname
Removes a directory
15 tail filename
16 touch filename
17 whereis filename
18 which filename
You can use Manpage Help to check complete syntax for each command mentioned here.
The df Command
The first way to manage your partition space is with the df (disk free) command. The
command df -k (disk free) displays the disk space usage in kilobytes, as shown below −
$df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vzfs 10485760 7836644 2649116 75% /
/devices 0 0 0 0% /devices
$
Some of the directories, such as /devices, shows 0 in the kbytes, used, and avail columns as
well as 0% for capacity. These are special (or virtual) file systems, and although they reside
on the disk under /, by themselves they do not consume disk space.
The df -k output is generally the same on all Unix systems. Here's what it usually includes −
1 Filesystem
2 kbytes
3 used
4 avail
5 capacity
You can use the -h (human readable) option to display the output in a format that shows
the size in easier-to-understand notation.
The du Command
The du (disk usage) command enables you to specify directories to show disk space usage
on a particular directory.
This command is helpful if you want to determine how much space a particular directory is
taking. The following command displays number of blocks consumed by each directory. A
single block may take either 512 Bytes or 1 Kilo Byte depending on your system.
$du /etc
10 /etc/cron.d
126 /etc/default
6 /etc/dfs
...
$
$du -h /etc
5k /etc/cron.d
63k /etc/default
3k /etc/dfs
...
$
A file system must be mounted in order to be usable by the system. To see what is currently
mounted (available for use) on your system, use the following command −
$ mount
/dev/vzfs on / type reiserfs (rw,usrquota,grpquota)
proc on /proc type proc (rw,nodiratime)
devpts on /dev/pts type devpts (rw)
$
The /mnt directory, by the Unix convention, is where temporary mounts (such as CDROM
drives, remote network drives, and floppy drives) are located. If you need to mount a file
system, you can use the mount command with the following syntax −
For example, if you want to mount a CD-ROM to the directory /mnt/cdrom, you can type −
This assumes that your CD-ROM device is called /dev/cdrom and that you want to mount it
to /mnt/cdrom. Refer to the mount man page for more specific information or type mount -
h at the command line for help information.
After mounting, you can use the cd command to navigate the newly available file system
through the mount point you just made.
To unmount (remove) the file system from your system, use the umount command by
identifying the mount point or device.
$ umount /dev/cdrom
The mount command enables you to access your file systems, but on most modern Unix
systems, the automount function makes this process invisible to the user and requires no
intervention.
The user and group quotas provide the mechanisms by which the amount of space used by a
single user or all users within a specific group can be limited to a value defined by the
administrator.
Quotas operate around two limits that allow the user to take some action if the amount of
space or number of disk blocks start to exceed the administrator defined limits −
Soft Limit − If the user exceeds the limit defined, there is a grace period that allows
the user to free up some space.
Hard Limit − When the hard limit is reached, regardless of the grace period, no
further files or blocks can be allocated.
1 quota
2 edquota
This is a quota editor. Users or Groups quota can be edited using this command
3 quotacheck
Scans a filesystem for disk usage, creates, checks and repairs quota files
4 setquota
5 quotaon
This announces to the system that disk quotas should be enabled on one or more
filesystems
6 quotaoff
This announces to the system that disk quotas should be disabled for one or more
filesystems
7 repquota
This prints a summary of the disc usage and quotas for the specified file systems
You can use Manpage Help to check complete syntax for each command mentioned here.
Root account
This is also called superuser and would have complete and unfettered control of the system.
A superuser can run any commands without any restriction. This user should be assumed as
a system administrator.
System accounts
System accounts are those needed for the operation of system-specific components for
example mail accounts and the sshd accounts. These accounts are usually needed for some
specific function on your system, and any modifications to them could adversely affect the
system.
User accounts
User accounts provide interactive access to the system for users and groups of users. General
users are typically assigned to these accounts and usually have limited access to critical
system files and directories.
Unix supports a concept of Group Account which logically groups a number of accounts.
Every account would be a part of another group account. A Unix group plays important role
in handling file permissions and process management.
Managing Users and Groups
/etc/passwd − Keeps the user account and password information. This file holds the
majority of information about accounts on the Unix system.
/etc/shadow − Holds the encrypted password of the corresponding account. Not all
the systems support this file.
/etc/group − This file contains the group information for each account.
The following table lists out commands that are available on majority of Unix systems to
create and manage accounts and groups −
1 useradd
2 usermod
3 userdel
4 groupadd
5 groupmod
Modifies group attributes
6 groupdel
You can use Manpage Help to check complete syntax for each command mentioned here.
Create a Group
We will now understand how to create a group. For this, we need to create groups before
creating any account otherwise, we can make use of the existing groups in our system. We
have all the groups listed in /etc/groups file.
All the default groups are system account specific groups and it is not recommended to use
them for ordinary accounts. So, following is the syntax to create a new group account −
1 -g GID
2 -o
3 -r
This option causes to just exit with success status, if the specified group already exists.
With -g, if the specified GID already exists, other (unique) GID is chosen
5 groupname
If you do not specify any parameter, then the system makes use of the default values.
Following example creates a developers group with default values, which is very much
acceptable for most of the administrators.
$ groupadd developers
Modify a Group
Delete a Group
We will now understand how to delete a group. To delete an existing group, all you need is
the groupdel command and the group name. To delete the financial group, the command
is −
$ groupdel developer
This removes only the group, not the files associated with that group. The files are still
accessible by their owners.
Create an Account
Let us see how to create a new account on your Unix system. Following is the syntax to
create a user's account −
1 -d homedir
2 -g groupname
3 -m
4 -s shell
5 -u userid
6 accountname
If you do not specify any parameter, then the system makes use of the default values.
The useradd command modifies the /etc/passwd, /etc/shadow, and /etc/group files and
creates a home directory.
Following is the example that creates an account mcmohd, setting its home directory
to /home/mcmohd and the group as developers. This user would have Korn Shell assigned
to it.
Before issuing the above command, make sure you already have the developers group
created using the groupadd command.
Once an account is created you can set its password using the passwd command as follows
−
$ passwd mcmohd20
Changing password for user mcmohd20.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
When you type passwd accountname, it gives you an option to change the password,
provided you are a superuser. Otherwise, you can change just your password using the same
command but without specifying your account name.
Modify an Account
The usermod command enables you to make changes to an existing account from the
command line. It uses the same arguments as the useradd command, plus the -l argument,
which allows you to change the account name.
For example, to change the account name mcmohd to mcmohd20 and to change home
directory accordingly, you will need to issue the following command −
Delete an Account
The userdel command can be used to delete an existing user. This is a very dangerous
command if not used with caution.
There is only one argument or option available for the command .r, for removing the
account's home directory and mail file.
For example, to remove account mcmohd20, issue the following command −
$ userdel -r mcmohd20
If you want to keep the home directory for backup purposes, omit the -r option. You can
remove the home directory as needed at a later time.
In this chapter, we will discuss in detail about the system performance in Unix.
We will introduce you to a few free tools that are available to monitor and manage
performance on Unix systems. These tools also provide guidelines on how to diagnose and
fix performance problems in the Unix environment.
Unix has following major resource types that need to be monitored and tuned −
CPU
Memory
Disk space
Communications lines
I/O Time
Network Time
Applications programs
Performance Components
The following table lists out five major components which take up the system time −
The actual amount of time the CPU spends running the users’ program in the user state. It
includes the time spent executing library calls, but does not include the time spent in the
kernel on its behalf
This is the amount of time the CPU spends in the system state on behalf of this program.
All I/O routines require kernel services. The programmer can affect this value by blocking
I/O transfers
This is the amount of time spent moving data and servicing I/O requests
4
Virtual Memory Performance
5 Application Program
Time spent running other programs - when the system is not servicing this application
because another application currently has the CPU
Performance Tools
Unix provides following important tools to measure and fine tune Unix system performance
−
1 nice/renice
2 netstat
Prints network connections, routing tables, interface statistics, masquerade connections,
and multicast memberships
3 time
4 uptime
5 ps
6 vmstat
7 gprof
8 prof
9 top
You can use Manpage Help to check complete syntax for each command mentioned here.
Many versions of Unix provide a general-purpose logging facility called syslog. Individual
programs that need to have information logged, send the information to syslog.
Unix syslog is a host-configurable, uniform system logging facility. The system uses a
centralized system logging process that runs the program /etc/syslogd or /etc/syslog.
The operation of the system logger is quite straightforward. Programs send their log entries
to syslogd, which consults the configuration file /etc/syslogd.conf or /etc/syslog and, when a
match is found, writes the log message to the desired log file.
There are four basic syslog terms that you should understand −
1 Facility
The identifier used to describe the application or process that submitted the log message.
For example, mail, kernel, and ftp.
2 Priority
An indicator of the importance of the message. Levels are defined within syslog as
guidelines, from debugging information to critical events.
3 Selector
A combination of one or more facilities and levels. When an incoming event matches a
selector, an action is performed.
4 Action
What happens to an incoming message that matches a selector — Actions can write the
message to a log file, echo the message to a console or other device, write the message to a
logged in user, or send the message along to another syslog server.
Syslog Facilities
We will now understand about the syslog facilities. Here are the available facilities for the
selector. Not all facilities are present on all versions of Unix.
Facility Description
1 auth
2 authpriv
Same as auth but logged to a file that can only be read by selected users
3 console
Used to capture messages that are generally directed to the system console
4 cron
5 daemon
6 ftp
7 kern
Kernel messages
8 local0.local7
9 lpr
10 mail
11 mark
12 news
13 ntp
14 user
15 uucp
UUCP subsystem
Syslog Priorities
The syslog priorities are summarized in the following table −
1 emerg
Emergency condition, such as an imminent system crash, usually broadcast to all users
2 alert
3 crit
4 err
Ordinary error
5 Warning
Warning
6 notice
Condition that is not an error, but possibly should be handled in a special way
7 info
Informational message
8 debug
The combination of facilities and levels enables you to be discerning about what is logged
and where that information goes.
As each program sends its messages dutifully to the system logger, the logger makes
decisions on what to keep track of and what to discard based on the levels defined in the
selector.
When you specify a level, the system will keep track of everything at that level and higher.
The /etc/syslog.conf file controls where messages are logged. A typical syslog.conf file
might look like this −
*.err;kern.debug;auth.notice /dev/console
daemon,auth.notice /var/log/messages
lpr.info /var/log/lpr.log
mail.* /var/log/mail.log
ftp.* /var/log/ftp.log
auth.* @prep.ai.mit.edu
auth.* root,amrood
netinfo.err /var/log/netinfo.log
install.* /var/log/install.log
*.emerg *
*.alert |program_name
mark.* /dev/console
A message selector that specifies which kind of messages to log. For example, all
error messages or all debugging messages from the kernel.
An action field that says what should be done with the message. For example, put it
in a file or send the message to a user's terminal.
Following are the notable points for the above configuration −
Message selector kern.debug selects all priorities that are greater than debug.
An asterisk in place of either the facility or the priority indicates "all". For
example, *.debug means all debug messages, while kern.* means all messages
generated by the kernel.
You can also use commas to specify multiple facilities. Two or more selectors can be
grouped together by using a semicolon.
Logging Actions
Send a message to a user. You can specify multiple usernames by separating them
with commas; for example, root, amrood.
Send a message to all users. In this case, the action field consists of an asterisk; for
example, *.
Pipe the message to a program. In this case, the program is specified after the Unix
pipe symbol (|).
Send the message to the syslog on another host. In this case, the action field consists
of a hostname, preceded by an at sign; for example, @tutorialspoint.com.
Unix provides the logger command, which is an extremely useful command to deal with
system logging. The logger command sends logging messages to the syslogd daemon, and
consequently provokes system logging.
This means we can check from the command line at any time the syslogd daemon and its
configuration. The logger command provides a method for adding one-line entries to the
system log file from the command line.
1 -f filename
2 -i
3 -p priority
Enters the message with the specified priority (specified selector entry); the message
priority can be specified numerically, or as a facility.priority pair. The default priority is
user.notice.
4 -t tag
Marks each line added to the log with the specified tag.
5 message
The string arguments whose contents are concatenated together in the specified order,
separated by the space.
You can use Manpage Help to check complete syntax for this command.
Log Rotation
Log files have the propensity to grow very fast and consume large amounts of disk space. To
enable log rotations, most distributions use tools such as newsyslog or logrotate.
These tools should be called on a frequent time interval using the cron daemon. Check the
man pages for newsyslog or logrotate for more details.
All the system applications create their log files in /var/log and its sub-directories. Here are
few important applications and their corresponding log directories −
Application Directory
httpd /var/log/httpd
samba /var/log/samba
cron /var/log/
mail /var/log/
mysql /var/log/
In this chapter, we will discuss in detail about Signals and Traps in Unix.
Signals are software interrupts sent to a program to indicate that an important event has
occurred. The events can vary from user requests to illegal memory access errors. Some
signals, such as the interrupt signal, indicate that a user has asked the program to do
something that is not in the usual flow of control.
The following table lists out common signals you might encounter and want to use in your
programs −
Signal
Signal Name Description
Number
If a process gets this signal it must quit immediately and will not perform
SIGKILL 9
any clean-up operations
List of Signals
There is an easy way to list down all the signals supported by your system. Just issue the kill
-l command and it would display all the supported signals −
$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT
17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN
35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4
39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6
59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
The actual list of signals varies between Solaris, HP-UX, and Linux.
Default Actions
Every signal has a default action associated with it. The default action for a signal is the
action that a script or program performs when it receives a signal.
Dump core. This creates a file called core containing the memory image of the
process when it received the signal.
Sending Signals
There are several methods of delivering signals to a program or script. One of the most
common is for a user to type CONTROL-C or the INTERRUPT key while a script is
executing.
When you press the Ctrl+C key, a SIGINT is sent to the script and as per defined default
action script terminates.
The other common method for delivering signals is to use the kill command, the syntax of
which is as follows −
$ kill -signal pid
Here signal is either the number or name of the signal to deliver and pid is the process ID
that the signal should be sent to. For Example −
$ kill -1 1001
The above command sends the HUP or hang-up signal to the program that is running
with process ID 1001. To send a kill signal to the same process, use the following command
−
$ kill -9 1001
Trapping Signals
When you press the Ctrl+C or Break key at your terminal during execution of a shell
program, normally that program is immediately terminated, and your command prompt
returns. This may not always be desirable. For instance, you may end up leaving a bunch of
temporary files that won't get cleaned up.
Trapping these signals is quite easy, and the trap command has the following syntax −
Here command can be any valid Unix command, or even a user-defined function, and signal
can be a list of any number of signals you want to trap.
Ignore signals
As an example of the trap command, the following shows how you can remove some files
and then exit if someone tries to abort the program from the terminal −
Hence, if the user interrupts the execution of the program after this trap is executed, you can
be assured that these two files will be cleaned up. The exit command that follows the rm is
necessary because without it, the execution would continue in the program at the point that it
left off when the signal was received.
Signal number 1 is generated for hangup. Either someone intentionally hangs up the line or
the line gets accidentally disconnected.
You can modify the preceding trap to also remove the two specified files in this case by
adding signal number 1 to the list of signals −
Now these files will be removed if the line gets hung up or if the Ctrl+C key gets pressed.
The commands specified to trap must be enclosed in quotes, if they contain more than one
command. Also note that the shell scans the command line at the time that the trap command
gets executed and also when one of the listed signals is received.
Thus, in the preceding example, the value of WORKDIR and $$ will be substituted at the
time that the trap command is executed. If you wanted this substitution to occur at the time
that either signal 1 or 2 was received, you can put the commands inside single quotes −
Ignoring Signals
If the command listed for trap is null, the specified signal will be ignored when received. For
example, the command −
$ trap '' 2
This specifies that the interrupt signal is to be ignored. You might want to ignore certain
signals when performing an operation that you don't want to be interrupted. You can specify
multiple signals to be ignored as follows −
$ trap '' 1 2 3 15
Note that the first argument must be specified for a signal to be ignored and is not equivalent
to writing the following, which has a separate meaning of its own −
$ trap 2
If you ignore a signal, all subshells also ignore that signal. However, if you specify an action
to be taken on the receipt of a signal, all subshells will still take the default action on receipt
of that signal.
Resetting Traps
After you've changed the default action to be taken on receipt of a signal, you can change it
back again with the trap if you simply omit the first argument; so −
$ trap 1 2
This resets the action to be taken on the receipt of signals 1 or 2 back to the default.