Unit 2
Unit 2
Your username must be unique on the system where your account exists since it is
the means by which you are identified on the system.
Your password
When your account is created, a password is assigned. The first thing you should
do is change your password, using the passwd utility. To change your password,
type the command
passwd
After you have logged in. The system will ask for your old password, to prevent
someone else from sneaking up, and changing your password. Then it will ask for
your new password. You will be asked to confirm your new password, to make
sure that you didn't mistype. It is very important that you choose a good password,
so that someone else cannot guess it. Here are some rules for selecting a good
password:
Do not use any part of your name, your spouse's name, your child's name,
your pet's name, or anybody's name. Do not use any backward spellings of
any name, either.
Do not use an easily-guessable number, like your phone number, your social
security number, your address, license plate number, etc.
Do not use any word that can be found in an English or foreign-language
dictionary.
1
Unit-2: Overview of Unix OS
Do not use all the same letter, or a simple sequence of keys on the keyboard,
like qwerty.
Do use a mix of upper-case and lower-case letters, numbers, and control
characters.
Do use at least six characters.
logout
When you finish using the shell, type the exit or logout command. This will
either display a new login prompt or close the terminal emulator window. If you
are logged in remotely to a server, this will break your connection. If you are
logged in physically on a large UNIX system, this will prevent someone else from
abusing your account.
When you finish using the shell, type the exit or logout command. This will
either display a new login prompt or close the terminal emulator window.
Some of the possible variations of this command include: bye, exit, lo, logout,
and quit.
Typing CtrlL-D (holding down the CONTROL key and the D key at the same
time) will also log you out of most shells. The Ctrl-D tells the shell it has reached
the end-of-file (EOF). Because the shell is a filter and filters terminate when EOF
is reached, the shell terminates and you log off the system or return to the parent
process.
While it might not be a big deal to skip this step on a single user computer that
you never leave unattended, forgetting to logout from a remote server is a serious
security hole. If you leave your computer or terminal unattended, someone else can
sit down and gain access to all of your files, including the ability to read, modify,
or delete them. The imposter can send spam emails from your user ID. The
imposter can even use your computer or account to attempt to hack or damage or
break into any system in the world from your user ID. If this occurs from a school
or business account, you will be responsible for any damage done.
2
Unit-2: Overview of Unix OS
3
Unit-2: Overview of Unix OS
A file system relies on data structures about the files, beside the file content. The
former is called metadata—data that describes data. Each file is associated with an
inode, which is identified by an integer number, often referred to as an i-number or
inode number.
Inodes store information about files and directories (folders), such as file
ownership, access mode (read, write, execute permissions), and file type. On many
types of file system implementations, the maximum number of inodes is fixed at
file system creation, limiting the maximum number of files the file system can
hold. A typical allocation heuristic for inodes in a file system is one percent of total
size.
Regular files must have the following attributes:
• The size of the file in bytes.
• Device ID (this identifies the device containing the file).
• The User ID of the file's owner.
• The Group ID of the file.
• The file mode which determines the file type and how the file's owner, its group,
and others can access the file.
• Additional system and user flags to further protect the file (limit its use and
modification).
• Timestamps telling when the inode itself was last modified (ctime, inode change
time), the file content last modified (mtime, modification time), and last accessed
(atime, access time).
• A link count telling how many hard links point to the inode.
• Pointers to the disk blocks that store the file's contents The stat system call
retrieves a file's inode number and some of the information in the inode.
4
Unit-2: Overview of Unix OS
5
Unit-2: Overview of Unix OS
FILE SECURITY
UNIX has 3 levels of security.
1st level username and password for login.
2nd level is read, write and execute permission for the file.
At 3rd level user can encrypt the file.
Task: Encrypt the original file called my.txt
Type the command as follows: $ cat my.txt | crypt > my.cpy Output:
Enter key:123
This will create an encrypted form of my.txt file, and store it in the file my.cpy.
Supported on BSD systems only.
6
Unit-2: Overview of Unix OS
7
Unit-2: Overview of Unix OS
Device or special files are used for device I/O on UNIX and Linux systems. They
appear in a file system just like an ordinary file or a directory. On UNIX systems
there are two flavors of special files for each device, character special files and
block special files. Linux systems only provide one special file for each device.
When a character special file is used for device I/O, data is transferred one
character at a time. This type of access is called raw device access. When a block
special file is used for device I/O, data is transferred in large fixed-size blocks.
This type of access is called block device access. Links A link is a tool used for
having multiple filenames that reference a single file on a physical disk. They
appear in a file system just like an ordinary file or a directory. Like special files,
links also come in two different flavors. There are hard links and symbolic links.
Hard links do not actually link to the original file. Instead they maintain their own
copy of the original file's attributes (i.e. location on disk, file access permissions,
etc.). If the original file is deleted, its data can still be accessed using the hard link.
On the other hand, symbolic links contain a pointer, or pathname, to the original
file. If the original file is deleted, its data can no longer be accessed using the
symbolic link, and the link is then considered to be a stale link. Named Pipes
Named pipes are tools that allow two or more system processes to communicate
with each other using a file that acts as a pipe between them. This type of
communication is known as interprocess communication or IPC for short.
8
Unit-2: Overview of Unix OS
9
Unit-2: Overview of Unix OS
Sockets: Sockets are also tools used for interprocess communication. The
difference between sockets and pipes is that sockets will facilitate communication
between processes running on different systems, or over the network. With so
many different types of files, it's often wise to identify a file's type before
performing any operation with it. The ls -l command and the file command are
useful for determining file types. Consider the long listing of the livefirelabs1 file:
-rw-rw-r-- 1 student1 student1 0 Jun 27 18:55 livefirelabs1
The first character of the first field indicates the file type. In this example, the first
character is a - (hyphen) indicating that livefirelabs1 is an ordinary or regular file.
Consider the long listing of the live1 file:
lrwxrwxrwx 1 student1 student1 13 Jun 27 17:57 live1 -> livefirelabs1
The first character of the first field is the letter l indicating live1 is a symbolic link.
The following is a table listing what characters represent what types of files:
- Ordinary or Regular File d Directory c Character special file b Block special file l
Symbolic link p Named pipe s Socket
The file command is also helpful for determining file types. The syntax for this
command is:
$ file filename
File System
In the original Unix file system, Unix divided physical disks into logical disks
called partitions. Each partition is a standalone file system. We will use the term
``file system'' when referring to a single partition.
Each disk device is given its own major device number, and each partition has an
associated minor device number which the device driver uses to access the raw file
system.
The major/minor device number combination serves as a handle into the device
switch table. That is, the major number acts as an index, and the minor number is
passed as an argument to the driver routines so that they can recognize the specific
instance of a device.
Each filesystem contains:
1 In the original Unix file system, Unix divided physical disks into logical disks
called partitions. Each partition is a standalone file system. We will use the term ``file
system'' when referring to a single partition.
Each disk device is given its own major device number, and each partition has an
associated minor device number which the device driver uses to access the raw file
system.
10
Unit-2: Overview of Unix OS
The major/minor device number combination serves as a handle into the device switch
table. That is, the major number acts as an index, and the minor number is passed as
an argument to the driver routines so that they can recognize the specific instance of a
device.
1.
a boot block located in the first few sectors of a file system. The boot block
contains the initial bootstrap program used to load the operating system.
Typically, the first sector contains a bootstrap program that reads in a larger
bootstrap program from the next few sectors, and so forth.
2.
a super block describes the state of the file system: the total size of the
partition, the block size, pointers to a list of free blocks, the inode number of
the root directory, magic number, etc.
3.
a linear array of inodes (short for ``index nodes''). There is a one to one
mapping of files to inodes and vice versa. An inode is identified by its ``inode
number'', which contains the information needed to find the inode itself on the
disk
Thus, while users think of files in terms of file names, Unix thinks of files in
terms of inodes.
Thus, while users think of files in terms of file names, Unix thinks of files in terms
of inodes.
4.
data blocks: blocks containing the actual contents of files
---------------------------------------------------------------
---------------------------------------------------------------
An inode is the ``handle'' to a file and contains the following information:
• • file ownership indication
• • file type (e.g., regular, directory, special device, pipes, etc.)
• • file access permissions. May have setuid (sticky) bit set.
• • time of last access, and modification
11
Unit-2: Overview of Unix OS
12
Unit-2: Overview of Unix OS
/usr/bin : This directory stores all binary programs distributed with the
operating system not residing in /bin, /sbin or (rarely) /etc.
/usr/include : Stores the development headers used throughout the
system. Header files are mostly used by the #include directive in
C/C++ programming language.
/usr/lib : Stores the required libraries and data files for programs stored
within /usr or elsewhere.
/var : A short for “variable.” A place for files that may change often –
especially in size, for example e-mail sent to users on the system, or
process-ID lock files.
/var/log : Contains system log files.
/var/mail : The place where all the incoming mails are stored. Users
(other than root) can access their own mail only. Often, this directory is
a symbolic link to /var/spool/mail.
/var/spool : Spool directory. Contains print jobs, mail spools and other
queued tasks.
/var/tmp : A place for temporary files which should be preserved
between system reboots.
1.
hardware
2.
operating system (OS) loader
3.
kernel
4.
root user-space process (init and inittab)
5.
boot scripts
14
Unit-2: Overview of Unix OS
Hardware
After power-on or hard reset, control is given to a program stored in read-only
memory (normally PROM); for historical reasons involving the personal computer,
this program is often called "the BIOS".
This program normally performs a basic self-test of the machine and accesses
nonvolatile memory to read further parameters. This memory in the PC is battery-
backed CMOS memory, so most people refer to it as "the CMOS"; outside of the
PC world, it is usually called "the NVRAM" (nonvolatile RAM).
The parameters stored in the NVRAM vary among systems, but as a minimum,
they should specify which device can supply an OS loader, or at least which
devices may be probed for one; such a device is known as "the boot device". The
hardware boot stage loads the OS loader from a fixed position on the boot device,
and then transfers control to it.
Note:
The device from which the OS loader is read may be attached via a network,
in which case the details of booting are further specified by protocols such as
DHCP, TFTP, PXE, Etherboot, etc.
OS loader
The main job of the OS loader is to locate the kernel on some device, load it,
and run it. Most OS loaders allow interactive use, in order to enable
specification of an alternative kernel (maybe a backup in case the one last
compiled isn't functioning) and to pass optional parameters to the kernel.
In a traditional PC, the OS loader is located in the initial 512-byte block of the
boot device; this block is known as "the MBR" (Master Boot Record).
In most systems, the OS loader is very limited due to various constraints. Even
on non-PC systems, there are some limitations on the size and complexity of
this loader, but the size limitation of the PC MBR (512 bytes, including the
partition table) makes it almost impossible to squeeze much functionality into
it.
Therefore, most systems split the role of loading the OS between a primary OS
loader and a secondary OS loader; this secondary OS loader may be located
within a larger portion of persistent storage, such as a disk partition.
15
Unit-2: Overview of Unix OS
Kernel
When the kernel is loaded, it initializes various components of the computer
and operating system; each portion of software responsible for such a task is
usually consider "a driver" for the applicable component. The kernel starts the
virtual memory swapper (it is a kernel process, called "kswapd" in a modern
Linux kernel), and mounts some filesystem at the root path, /.
Some of the parameters that may be passed to the kernel relate to these
activities (for example, the default root filesystem can be overridden); for
further information on Linux kernel parameters.
Only then does the kernel create the initial userland process, which is given the
number 1 as its PID (process ID). Traditionally, this process executes the
program /sbin/init, to which are passed the parameters that haven't already been
handled by the kernel.
When /sbin/init starts, it reads /etc/inittab for further instructions. This file
defines what should be run when the/sbin/init program is instructed to enter
a particular run-level, giving the administrator an easy way to establish an
environment for some usage; each run-level is associated with a set of
services (for example, run-level S is single-user mode, and run-
level 2 entails running most network services).
The administrator may change the current run-level via init (1), and query
the current run-level via runlevel(8).
16
Unit-2: Overview of Unix OS
Boot scripts
Note:
The following description applies to an OS based on UNIX System V
Release 4. However, a number of widely used systems (Slack ware Linux,
FreeBSD, OpenBSD) have a somewhat different scheme for boot scripts.
For each managed service (mail, nfs server, cron, etc.), there is a single
startup script located in a specific directory (/etc/init.d in most versions of
Linux). Each of these scripts accepts as a single argument the word
"start" (causing it to start the service) or the word "stop" (causing it to
stop the service). The script may optionally accept other "convenience"
parameters (e.g., "restart" to stop and then start, "status" to display the
service status, etc.). Running the script without parameters displays the
possible arguments.
Sequencing directories
To make specific scripts start/stop at specific run-levels and in a specific
order, there are sequencing directories, normally of the form /etc/rc[0-
6S].d. In each of these directories, there are links (usually symbolic) to
the scripts in the /etc/init.d directory.
A primary script (usually /etc/rc) is called from inittab (5); this primary
script calls each service's script via a link in the relevant sequencing
directory. Each link whose name begins with 'S' is called with the
argument "start" (thereby starting the service). Each link whose name
begins with 'K' is called with the argument "stop" (thereby stopping the
service).
To define the starting or stopping order within the same run-level, the
name of a link contains an order-number. Also, for clarity, the name of
a link usually ends with the name of the service to which it refers. For
example, the link /etc/rc2.d/S80sendmail starts the sendmail service on
runlevel 2. This happens after /etc/rc2.d/S12syslog is run but
before /etc/rc2.d/S90xfs is run.
To manage these links is to manage the boot order and run-levels; under
many systems, there are tools to help with this task (e.g., chkconfig (8)).
17
Unit-2: Overview of Unix OS
Boot configuration
A program that provides a service is often called a "daemon". Usually, a
daemon may receive various command-line options and parameters. To
allow a system administrator to change these inputs without editing an
entire boot script, some separate configuration file is used, and is located
in a specific directory where an associated boot script may find it
(/etc/sysconfig on older Red Hat systems).
In older UNIX systems, such a file contained the actual command line
options for a daemon, but in modern Linux systems (and also in HP-UX),
it just contains shell variables. A boot script in /etc/init.d reads and
includes its configuration file (that is, it "sources" its configuration file)
and then uses the variable values.
Init process
The kernel, once it is loaded, finds init in sbin and executes it.
When init starts, it becomes the parent or grandparent of all of the processes that
start up automatically on your Linux system. The first thing init does, is reading its
initialization file, /etc/inittab. This instructs init to read an initial configuration
script for the environment, which sets the path, starts swapping, checks the file
systems, and so on. Basically, this step takes care of everything that your system
needs to have done at system initialization: setting the clock, initializing serial
ports and so forth.
Then init continues to read the /etc/inittab file, which describes how the system
should be set up in each run level and sets the default run level. A run level is a
configuration of processes. All UNIX-like systems can be run in different process
configurations, such as the single user mode, which is referred to as
run level 1 or run level S (or s). In this mode, only the system administrator can
connect to the system. It is used to perform maintenance tasks without risks of
damaging the system or user data. Naturally, in this configuration we don't need to
offer user services, so they will all be disabled.
Another run level is the reboot run level, or run level 6, which shuts down all
running services according to the appropriate procedures and then restarts the
system.
#
# inittab This file describes how the INIT process should set up
# the system in a certain run-level.
18
Unit-2: Overview of Unix OS
a=all
$ls -l b2
-rw-r--r--
$chmod ug+x b2
$chmod g+w b2
-rwxrw-r-x.
$chmod a-x b2
-rw-rw-r--
$chmod u=x b2
above command will assign execute permission to owner but will revoke
every other
20
Unit-2: Overview of Unix OS
---xrw-r--
$chmod go=x b2
above command will assign execute permission to group and others but
every other
permission from group and others will be revoked. Now file's permission
will be
---x--x--x
$chmod a=r b2
-r--r--r--
Umask command
umask, as the man page says, stands for User file creation mask which is used for
determining the default permission for a new file creation. The new file creation
could either be a file creation through a normal process or a file copy. umask
command is a shell built-in meaning it is an internal command.
The three file permission attributes are read, write and execute. These 3 are
mapped to octal values as shown below:
read -4
write -2
21
Unit-2: Overview of Unix OS
execute - 1
In UNIX, the default file creation value is 666. 6 is 4+2(read + write). Permission
666 means 6 for the User, 6 for the group and 6 for others. Hence, a new file
creation by default is meant to have read and write permission for User, group and
others. This is the place where the umask comes into the picture. It is a kind of
filter wherein we can choose to retain or block some of the default permissions
from being applied on the file.
Say, the umask value is 0022. umask is by default displayed in Octal form, and
hence the first 0 in the umask value is the indication for octal value. So, the actual
umask is 022. This value together with the default file value(666) decides the final
permission to be given to the file.
Assume we create a file say "file1". The permissions given for this file will be the
result coming from the substraction of the umask from the default value :
Default: 666
umask : 022
---------------
Result : 644
644 is the permission to be given on the file "file1". 644 means read and write for
the User(644), read only for the group(644) and others(644).
$ touch file1
$ ls -l file1
Default:777
Umask:022
_________________
Result: 755
That means owner will have read, write, execute rights, group and others will
have read, execute permission on newly created directories.
1. What is umask?
umask is a number which defines the default permissions which are not to be
given on a file. A umask of 022 means not to give the write permission to the
group(022) and others(022) by default.
$ umask
0022
$ umask -S
u=rwx,g=rx,o=rx
This means umask, at the max, allows all permissions for the user, read and
execute alone for the group and others.
23
Unit-2: Overview of Unix OS
$ umask 033
$ umask u=rwx,g=r,o=r
6. Can we retain the permission of the source file while copying a file?
Yes, we can retain the permission of the source file using the cp command. As
shown in the example 4 of one of our earlier articles on cp command, the mode of
the source file can be retained.
A umask value of 002 is commonly used by people who are working on group
projects. If you create a file with your umask set to 002, anyone in the file's group
will be able to read or modify the file. Everybody else will only be allowed to read
it:
24
Unit-2: Overview of Unix OS
(0002) umask
0775 resultant mode, default permissions
Read+write+execute for user, read+write +execute for group, read+execute for
others.
25