Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Module II Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

St.

Teresa’s College (Autonomous)


BCA SEMESTER – I
LINUX ADMINISTRATION

MODULE – II
UNIX file system: Unix File basics Types of files, structure of a regular files, Directory structure of a UNIX file
system, Navigating the File Systems, Creating and Managing File Systems, File System Backup, File Permission and
Access, blocks, Inodes, Superblock the PATH variable, Allocation of disk: – Directories - Inode assignment to a
new file - allocation of disk blocks. System calls for the file System: Open – Read - Write - Lseek – Close - File
creation - Creation of special files - Changing directory and root - changing owner and mode – stat and fstat - pipes –
Dup Input and Output Redirection: Input Redirection, Output Redirection, Error Redirection, Filter.

Types of files in Linux

The types of files available in UNIX are :

1. Regular files ('-')

2. Directory files ('d')s

3. Special files

a. Block file('b')

b. Character device file('c')

c. Pipe file('p') (FIFO files)

d. Symbolic link file('l')

e. Socket file('s')

4. Hidden files
1. Regular files
Regular files are the most common files and are used to contain data. Regular files are in the form
of text files or binary files:
Text files : Text files are regular files that contain information stored in ASCII format text
and are readable by the user. They contain printable characters.
Binary files : Binary files are regular files that contain information readable by the
computer. Commands and programs are stored in executable, binary files. Eg. Audio, video
files
2. Directory files
Directory files contain information that the system needs to access all types of files, but directory
files do not contain the actual file data. As a result, directories occupy less space than a regular file.
Each directory entry represents either a file or a subdirectory.
3. Special files

a. Block file('b') – Devices that read/write one block at a time. Eg Hard Disk
b. Character device file('c') – Devices that read/write character by character. Eg. Serial
modems

c. Named pipe file or just a pipe file('p') - FIFO files are also called pipes. Pipes are
created by one process to temporarily allow communication with another process.

d. Symbolic link file('l') - Point or mirror other files

e. Socket file('s') - Provide inter-process communication


4. Hidden files
Hidden files in Linux are the files that are not listed when the user runs ls command. The
name of a hidden file starts with a. dot(.)

Structure of a Regular File


inode
An inode is a data structure that keeps track of all the files and directories within a
Linux or UNIX-based filesystem. “inode” is the abbreviation for “index node”. Every file and
directory in a filesystem is allocated an inode, which is identified by an integer known as “inode
number”. These unique identifiers store metadata about each file and directory. All inodes
within the same filesystem are unique. A file's inode number can be found using the ls -i
command. The ls -i command prints the i- node number in the first column of the report.
File = inode(header)+data

Fig : inode representation

INODE REPRESENTATION:
➢ Two versions of the inode :

• Disk copy : store the inode information when file is not in use

• In-core copy : records the information about active files.


Disk inodes consist of the following fields:
1. File ownership information

2. File type. Files may be of type regular, directory, character or block special, or
FIFO (pipes).

3. File access permissions : read, write and execute

4. File access time : last accessed/modified time

5. Number of links to the file

6. Table of contents for the disk addresses of data in a file.

7. File size.

● The Structure of file is used to represent information and description of the file in Linux.
● It gives detailed information of the file so that it enables the user to perform operations
based on it.
● The file structure handles the file using the inodes.
● In UNIX, the data in files is not stored sequentially on disk. inode stores the disk block
numbers on which the data is present. But for such strategy, if a file had data across 1000
blocks, the inode would need to store the numbers of 1000 blocks and the size of the inode
would differ according to the size of the file.
To be able to have constant size and yet allow large files, indirect addressing is used.
The inodes have array of size 13 which for storing the block numbers, although, the number of
elements in array is independent of the storage strategy. The first 10 members of the array are
"direct addresses", meaning that they store the block numbers of actual data. The 11th member
is "single indirect", it stores the block number of the block which has "direct addresses". The
12th member is "double indirect", it stores block number of a "single indirect" block. And the
13th member is "triple indirect", it stores block number of a "double indirect" block. This
strategy can be extended to "quadruple" or "quintuple" indirect addressing.

Inode Assignment to a new file

Allocation of disk blocks


Physical disk is divided into partitions or logical disks. Each logical disk is divided into fixed sized
blocks. A file system has the following structure :

• The boot block occupies the beginning of a file system, typically the first sector, and
contains the bootstrap code that is read into the machine to boot, or initialize, the operating
system.
• The super block describes the state of a file system - how large it is, how many files it can
store, where to find free space on the file system, and other information.
• The inode list is a list of inode’s that follows the super block in the file system.
• The data blocks start at the end of the inode list and contain file data and administrative
data. An allocated data block can belong to one and only one file in the file system.
• free list – a list of unused blocks

Algorithm for allocating Disk blocks

Input : file system number


Output : buffer for new block
{
while(super block locked) {
read block just taken from free list (algorithm bread);
copy block numbers in block into super block;
release block buffer (algorithm brelse);
unlock super block;
wake up processes ; }
}

SUPER BLOCK:

The superblock is part of various file systems of the operating system UNIX and its
derivatives. It typically includes the following management information of the file system:

➢ Size of the file system

➢ The number of free blocks in the file system

➢ A list of free blocks available on the file system - Pointer to free list

➢ Pointer to the first free block in the free list

➢ Size of the inode list

➢ Number of free inodes

➢ Pointer to list of free inodes

➢ Pointer to the next free inode in the list of free inodes

➢ Display whether superblock is changed/modified (dirty flag)

The kernel periodically writes the super block to disk if it had been modified so that it is
consistent with the data in the file system.

Algorithms : inode management

• ialloc algorithm – inode assignment to a new file is done by ialloc.

• iget – Kernel uses iget algorithm to allocate known inode whose inode number was
determined previously.

• ifree – release the inodes.

• iput – put inode in the free list

Conversion of Pathname to an inode (namei algorithm)

if(component matches an entry in directory (working inode))


{
get inode number for matched component;
release working inode (algorithm iput);
working inode=inode of matched component(algorithm iget);
}
else /* component not in directory
return (no inode);
}
return (working inode);

• Consider path : /etc/passwd

• Kernel starts parsing pathname

• working inode=root

• Then search root for the directory etc

• Kernel access data in root block by block till it founds etc

• Now working inode= etc(iget)

• Search etc block by block to find passwd

• Release an inode of etc(iput), set working inode=passwd(iget)

• Since no more component in pathname return current working inode.

For the path : /home/ankit/abc.txt

Assume inode numbers are:

/ (root) : inode = 2

home : inode = 5

ankit : inode = 31

abc.txt : inode = 12

Then the inode trace for /home/ankit/abc.txt is #2->#5->#31->#12


SYSTEM CALLS FOR FILE MANAGEMENT:

A system call is the programmatic way in which a computer program requests a service
from the kernel of the operating system it is executed on. This may include hardware-related
services for example, accessing a hard disk drive, creation and execution of new processes,
and communication between them.

open()

The open system call allows us to open a file for reading, writing etc. Its syntax is :

fd = open(pathname, flags, modes);


where pathname is a file name, flags indicate the type of open (such as for reading or
writing), and modes give the file permissions if the file is being created.

The open system call returns an integer called the user file descriptor.

read()

read() system call is used to read the file contents.The syntax of the read system call is:

number = read(fd, buffer, count);

where fd is the file descriptor returned by open, buffer is the address of a data structure
in the user process that will contain the read data on successful completion of the call, count is
the number of bytes the user wants to read, and number is the number of bytes actually read.

write()

The syntax for the write system call is:

number = write(fd, buffer, count);

where the meaning of the variables fd, buffer, count, and number are the same as they are
for the read system call.
close()

A process closes an open file when it no longer wants to access it. The syntax for the
close system call is:
close(fd);

where fd is the file descriptor for the open file.

lseek:

The ordinary use of read and write system calls provides sequential access to a file,
but processes can use the lseek system call to position the I/O and allow random access to a file.

The syntax for the lseek() system call is:

position = lseek(fd, offset, reference);

where fd is the file descriptor identifying the file, offset is a byte offset, and reference
indicates whether offset should be considered from the beginning of the file, from the current
position of the read/write offset, or from the end of the file. The return value ie. position, is the
byte offset where the next read or write will start.

FILE STATUS – stat() and fstat()

The system calls stat and fstat allow processes to query the status of files, returning
information such as the file type, file owner, access permissions, file size, number of links,
inode number, and file access times. The syntax for the system calls is:
stat(pathname,statbuffer);
fstat(fd, statbuffer);

where pathname is a file name, fd is a file descriptor returned by a previous open call,
and statbuffer is the address of a data structure in the user process that will contain the status
information of the file on completion of the call. The system calls simply write the fields of the
inodeinto statbuffer.
stat() – returns the information in the inode for the file named by a string
fstat() - returns the information in the inode for the file named by a file descriptor

CHANNEL DUPLICATION – dup() and dup2()

dup()
The dup() system call copies a file descriptor into the first free slot of the user file
descriptor table, returning the new file descriptor to the user. It works for all file types. The
syntax of the system call is :
newfd = dup(fd);
where fd is the file descriptor being duped and newfd is the new file descriptor that
references the file.

dup2:

The dup2 is a system call similar to dup in that it duplicates one file descriptor, making
them aliases, and then deleting the old file descriptor. Syntax for the dup2 system call is:

dup2(oldfd, newfd);
The oldfd is the source file descriptor that remains open after the call to dup2 and the
newfd is the destination file descriptor that will points to the same file as oldfd after this call
returns. It returns the value of the newfd up on success. A negative value will be returned when
error occurs.

pipe() System call :


Conceptually, a pipe is a connection between two processes, such that the standard output
from one process becomes the standard input of the other process. In UNIX Operating System,
Pipes are useful for communication between related processes(inter-process communication).
Syntax is :
int pipe(int fds[2])
where fd[0] will be the file descriptor for the read end of pipe, fd[1] will be the file descriptor for
the write end of pipe.
FILE CREATION - creat()
int creat (char *filename, int mode);
where filename: name of the file which you want to create ; mode: indicates permissions of the
new file. mode is usually specified as an octal number such as 0666, that would mean read/write
permission for owner, group and others.

CREATION OF SPECIAL FILE – mknod()


mknod command is used to create a special file.
mknod Name { b | c } Major Minor
mknod Name { p } // Creates a FIFO (pipe) file
The mknod command makes a directory entry and corresponding i-node for a special file. The first
parameter is the name of the device. The mknod command has two forms that have different flags.
The first form of the mknod command can be executed by root or a member of the system
group. In the first form, the b or c flag is used. The b flag indicates that the special file is a block-
oriented device (disk, diskette, or tape). The c flag indicates that the special file is a character-
oriented device (other devices).
The last two parameters of the first form are numbers that specify the Major device and the
Minor device. The major number is to identify the corresponding driver. Many devices may use the
same major number. The device driver uses the minor number <minor> to distinguish individual
physical or logical devices.

Input/Output Redirection and Error redirection


• Data is entered into the computer via stdin (usually the keyboard), and the resulting output
goes to stdout (usually the shell). These pathways are called streams. However, it's possible
to alter these input and output locations, causing the computer to get information from
somewhere other than stdin or send the results somewhere other than stdout. This
functionality is referred to as redirection.
Standard Streams In I/O Redirection
The bash shell has three standard streams in I/O redirection:
➢ standard input (stdin) : The stdin stream is numbered as stdin (0). The bash shell takes input
from stdin. By default, keyboard is used as input.
➢ standard output (stdout) : The stdout stream is numbered as stdout (1). The bash shell sends
output to stdout. Output goes to display.
➢ standard error (stderr) : The stderr stream is numbered as stderr (2). The bash shell sends
error message to stderr. Error message goes to display.
1. Output Redirection

Regular output > redirection

Output redirection is a method in which the standard output of a command can be redirected to files
or as standard input for another command. The “>” sign is used for output redirection. The terminal
does not show the output; instead, it is written to a file or redirected as input to another command.
> operator overwrites the content of files.
date > specifications.txt
cat specificatons.txt
Here, the date command’s output is redirected to specifications.txt.

Regular output append >> redirection


This redirection method redirects the standard output of a command or a file to another file. The
difference is while using “>>” redirection, if the file already exists, the data will be appended to
the file; hence the file will not be overwritten.

date >> specifications.txt


hostname >> specifications.txt
uname >> specifications.txt

2. Regular input redirection <


Input redirection is a method in which the standard input of a command is redirected from a file
or a standard output of another command. The “<” sign is used for input redirection. It is mostly
the default action of a command. For example
Ubuntu@Ubuntu:~$ less < /etc/passwd
is the same as this.
Ubuntu@Ubuntu:~$ less /etc/passwd
It takes input from the file /etc/passwd instead of a keyboard.
3. Standard Error Redirection “2>”

With the error redirection method, the standard errors can be redirected and written to a file. For
example.

Ubuntu@Ubuntu:~$ somerandomcommand 2> error

If any error occurs, it will not show on the terminal window; rather, it will be stored in an error file.
If the error file already exists, then it will be overwritten.

Pipe | in Linux

• A pipe is a form of redirection (transfer of standard output to some other destination) that is
used in Linux to send the output of one command to another command for further
processing.
• The pipe is used to combine two or more commands, and in this, the output of one
command acts as input to another command, and this command’s output may act as input to
the next command, and so on.

Eg. sort record.txt | uniq

This will sort the given file and print the unique values only.

Filter commands in Linux

A filter is a program that takes plain text (stored in a file or generated by another program) as
standard input, converts it to a meaningful format, and then returns it as standard output. Some of
the most commonly uses filters are described below.
1. cat : Display the text of the file line by line. Syntax: cat filename
2. head: Shows the first n lines of the specified text file. If no number of lines is specified, the first
10 lines are printed by default. Syntax: head -number_of_lines_to_print filename
3. tail: Works the same as the head, but in reverse order. tail prints the lines from bottom to top.
Syntax: tail -number_of_lines_to_print filename
4. sort: Sorts the rows alphabetically by default. Syntax : sort filename
5. uniq: Remove duplicate lines. Syntax: uniq [options] [filename]
6. wc: wc command gives the number of lines, words and characters in the data.
Syntax: wc -lwc filename
7. grep : It is a pattern or expression matching command. It searches for a pattern or regular
expression that matches in files or directories and then prints found matches. Syntax: grep [options]
"pattern to be matched" filename
8. sed : For filtering and transforming text data, sed is a very powerful stream editor utility. Here
sed replaces the word ‘is’ if any, with the string ‘was’ in the file.
sed 's/is/was/' sample.txt
9. nl : nl is used to number the lines of a file. Syntax: nl filename
10. less : It is used to read the contents of a text file one page (one screen) at a time. It has faster
access because if a file is large, it doesn’t access the complete file, but accesses it page by page.
Syntax : less filename
11. more : It reads files and displays the text one screen at a time. The more command also allows
the user do scroll up and down through the page. Syntax : more filename
12. awk : This command can scan files line by line, split each input line into fields, compare input
lines and fields to patterns and perform specified actions on matching lines

Directory Structure of a UNIX File system


Operating system uses certain methods and the data structure to keep track of the files
which are stored in the disk. This is known as file system. A file system is the way in which the
files are organised in the disk. It is arranged in a hierarchical directory structure and organised in a
tree like pattern directories. The root directory has files and subdirectories.
• /(rootdirectory)
The root directory is the top-level directory in the Linux file system. All other directories
and files are contained within the root directory.
• /bin: /bin stands for binaries. Contains the executable files of many basic shell commands
like ls, cp, cd, more, tar etc…
• /boot: Contains the boot loader files and kernel images needed to start the system. Files such
as ‘vmlinuz‘ represent the compressed image of the Linux kernel.
• /dev: Contains device files that represent hardware devices and virtual devices such as
terminals, printers, and disks.
• /etc: Contains system configuration files that are used by various applications and services
on the system such as list of username and passwords, ethernets, hosts etc..
• /home: Contains the home directories of users on the system. Each user has their own
subdirectory within /home where they can store their personal files and settings.
• /lib: Contains shared library files that are needed by various programs on the system. Eg.
gcc, javac, jvm etc...
• /media: It is used to mount removable media such as CDs, DVDs, and USB drives.
• /mnt: It is used to mount file systems temporarily, such as network file systems or disk
images.
• /opt: It is used to store additional software packages that are not part of the core system.
• /proc: It provides information about running processes and system resources. Created on the
fly upon startup and destroyed once the system is powered off.
• /run: It contains temporary files that are created by system services and daemons.
• /sbin: It contains system binaries and administrative tools that are required for system
maintenance. Commands such as mount ,shutdown, umount, fdisk, iptables reside here.
• /sys: It is a virtual file system that provides information about the system's hardware and
devices.
• /tmp: It contains temporary files that are created by applications and services running on the
system.
• /usr: It contains user-level programs, libraries, documentation and shared data files.
• /var: It contains variable data files that change frequently, such as log & print files and
system databases.

Navigating the File Systems


Absolute Path v/s Relative Path in Linux
A path is a unique location to a file or folder in a file system of an OS. Linux supports two
types of paths: absolute path and relative path.
• An absolute path is a complete path to a file or directory from the root directory. The root
directory is the top-level directory of the file system and is represented by a forward slash
(/). Absolute paths always start with the root directory and provide the full path to the file or
directory. For example, the absolute path to the home directory of a user named "tom"
would be "/home/tom"
• A relative path is a path to a file or directory that is relative to the current directory. It
specifies the location of the file or directory in relation to the current directory. Relative
paths do not start with the root directory and are usually shorter than absolute paths.
• For example, if we are currently in the home directory of "tom" and want to access a file
named "example.txt" in a subdirectory called "documents", the relative path would be
"documents/example.txt".
Commands to navigate a file system.
1 cat filename : Displays a filename
2 cd dirname : Moves you to the identified directory
3 cp file1 file2 : Copies one file/directory to the specified location
4 file filename : Identifies the file type (binary, text, etc)
5 find filename dir : Finds a file/directory
6 head filename : Shows the beginning of a file (default first 10 lines)
7 less filename : Browses through a file from the end or the beginning, displays page by page.
8 ls dirname : Shows the contents of the directory specified
9 mkdir dirname : Creates the specified directory
10 more filename : Browses through a file from the beginning to the end, displays page by
page.
11 mv file1 file2 : Moves the location of, or renames a file/directory
12 pwd : Shows the current directory the user is in
13 rm filename : Removes a file
14 rmdir dirname : Removes a directory (default last 10 lines)
15 tail filename : Shows the end of a file
16 touch filename : Creates a blank file or modifies an existing file or its attributes
17 whereis filename : Shows the location of a file

Rules for naming a file


The string that is used to identify a file is called file name.
• The permitted characters are upper and lowercase letters, numbers, dot and underscore
symbols.
• Special character such as blank space should be avoided.
• Filename can consist of any character except / and null character.
• Extension in a filename is not necessary as Linux determines the file type based on the
nature of data.
• File names are case sensitive
• The file name should have only 14 characters.
• A filename must be unique inside its directory.

File system Backup


➢ Backup and recovery describe the process of creating and storing copies of data that can be
used to protect organizations against data loss. Recovery from a backup typically involves
restoring the data to the original location, or to an alternate location where it can be used in
place of the lost or damaged data.
➢ The purpose of the backup is to create a copy of data that can be recovered in the event of a
primary data failure. Primary data failures can be the result of hardware or software failure,
natural calamities, data corruption, or a human-caused event, such as a malicious attack
(virus or malware), or accidental deletion of data
I. Types of backup
1. Full Backup
A full backup is a backup where every single file is written to the backup media. If the data being
backed up never changes, every full backup being created will be the same. That similarity is due to
the fact that a full backup does not check to see if a file has changed since the last backup; it blindly
writes everything to the backup media whether it has been modified or not. This is the reason why
full backups are not done all the time -- every file is written to the backup media. This means that a
great deal of backup media is used even if nothing has changed.

2. Incremental backup
Unlike full backups, incremental backups first look to see whether a file's modification time is more
recent than its last backup time. If it is not, the file has not been modified since the last backup and
can be skipped this time. On the other hand, if the modification date is more recent than the last
backup date, the file has been modified and should be backed up.
Incremental backups are used in conjunction with a regularly-occurring full backup (for example, a
weekly full backup, with daily incrementals).

3. Differential backup
Differential backups are similar to incremental backups in that both backup only modified files.
However, differential backups are cumulative -- in other words, with a differential backup, once a
file has been modified it continues to be included in all subsequent differential backups (until the
next, full backup).
This means that each differential backup contains all the files modified since the last full backup,
making it possible to perform a complete restoration with only the last full backup and the last
differential backup.
Like the backup strategy used with incremental backups, differential backups normally follow the
same approach: a single periodic full backup followed by more frequent differential backups.
II. Backup Media
1. Tape
Tape was the first widely-used removable data storage medium. It has the benefits of low media
cost and reasonably-good storage capacity. However, tape has some disadvantages -- it is subject to
wear, and data access on tape is sequential in nature. On the other hand, tape is one of the most
inexpensive mass storage media available, and it has a long history of reliability.
2. Disk
The primary reason for using disk drives as a backup medium would be speed. There is no faster
mass storage medium available. But disk storage is not the ideal backup medium, for a number of
reasons:
• Disk drives are not normally removable.
• Disk drives are expensive
• Disk drives are fragile.
3. Network
By itself, a network cannot act as backup media. But combined with mass storage technologies, it
can serve quite well. For instance, by combining a high-speed network link to a remote data center
containing large amounts of disk storage, the disadvantages of backing up to disks can be
overcome.
Important aspects of Backup devices :
1. Cost
2. Reliability
3. Availability
4. Speed
5. Usability
III. Backup commands in Linux
a. tar command
It is always beneficial to compress files before backup. The two most popular tools for
compression of regular files on Linux are gzip/gunzip and bzip2/bunzip2. gzip results
into a file with .gz extension and bzip2 results into a file with .bz2 extension.
tar - It is short for Tape Archive and is used to create and extract archive files. An
archive file is a compressed file that contains one or more files bundled together for
more accessible storage and portability.
The tar command can also be used to compress an archive using gzip or bzip2
compression. To create a compressed file, the -z or -j option can be used in conjunction
with the -c option.
$ tar -zcvf archive.tar.gz files_to_compress - This command creates a “.tar.gz” archive
of the specified files.
$ tar -jcvf archive.tar.bz2 files_to_compress - This command creates a “.tar.bz2” archive
of the specified files.
To unzip an archive :
To decompress a ‘.tar.gz’ file − $ tar -zxvf archive.tar.gz
To decompress a ‘.tar.bz2’ file − $ tar -jxvf archive.tar.bz2
b. cpio command (Copy Input and Output)
The cpio command is a tool for creating and extracting archives, or copying files from
one place to another. It is used for processing the archive files like *.cpio or *.tar. This
command can copy files to and from archives.
1. Copy-out Mode: Copy files named in name-list to the archive
$ls
file1 file2 file3
$ls | cpio -ov > /home/Tom/backup.cpio
2. Copy-in Mode: Extract files from the archive
cpio -iv < /home/Tom/backup.cpio

-i, –extract: Extract files from an archive and it runs only in copy-in mode.
-o, –create: Create the archive and it runs only in copy-out mode.
c. dd command (Disk/Data Duplicator)
The dd is a command-line utility for Linux whose primary purpose is to convert and copy
files.

• To backup the entire hard disk: To back up an entire copy of a hard disk to another
hard disk connected to the same system, execute the dd command as shown. In this
dd command example, the UNIX device name of the source hard disk is /dev/hda,
and device name of the target hard disk is /dev/hdb.

$ dd if = /dev/sda of = /dev/sdb

“if” represents inputfile, and “of” represents output file. So the exact copy of
/dev/sda will be available in /dev/sdb.

d. dump and restore command


• ext2 ext3 ext4 backup and restore can be easily done with a dump utility which allows
you to take full and incremental file system backup. Backup can be taken on a tape , file
or a remote system and restore full or selective files.
• Dump is available at sourceforge.Net ; http://dump.Sourceforge.Net/
• Here are the complete steps to get started with complete Linux file system backup and
restore:

1. Install dump package


Dump package contains dump and restore utilities and it also installs tape device package rmt.

[root@localhost ~]# yum install dump



installed:
dump.X86_64 1:0.4-0.24.B44.Fc20
dependency installed:
rmt.X86_64 2:1.5.2-9.Fc20
complete!

2. Full file system backup


After installation , dump can be used to take a file system backup. In this example we will take full
backup of /dev/sda9
Numerical argument as 0 is full backup and subsequent number represent the incremental backup
corresponding to full backup.
Syntax is : dump – < level number > -f < source filesystem device >, where -u update the file
/etc/dumpdates

[root@localhost ~]# /sbin/dump -0u -f /dev/st0 /dev/sda9


DUMP: date of this level 0 dump: wed feb 8 22:10:13 2017
DUMP: dumping /dev/sda9 (/boot) to /dev/st0

DUMP: writing 10 kilobyte records
DUMP: mapping (pass I) [regular files]

DUMP: volume 1 started with block 1 at: wed feb 8 22:10:13 2017
DUMP: dumping (pass III) [directories]

3. Incremental backup
• Numerical argument as 2 is incremental backup and subsequent number represent the
incremental backup corresponding to full back.
• -u updates the /etc/dumpdates files
[root@localhost ~]# /sbin/dump -2u -f /dev/st0 /dev/sda9
dump: date of this level 2 dump: wed feb 8 22:14:13 2017
dump: date of last level 1 dump: wed feb 8 22:13:06 2017
dump: dumping /dev/sda9 (/boot) to /dev/st0

4. Backup History
Back up history can be viewed in the file /etc/dumpdates.
• [root@localhost ~]# cat /etc/dumpdates
/dev/sda9 0 wed feb 8 22:10:13 2017 -0800
/dev/sda9 1 wed feb 8 22:13:06 2017 -0800
/dev/sda9 2 wed feb 8 22:14:13 2017 -0800
/dev/sda9 3 wed feb 8 22:15:27 2017 -0800
/dev/sda9 4 wed feb 8 22:15:43 2017 -0800
/dev/sda9 5 wed feb 8 22:15:34 2017 -0800

5. Exit status
Dump exits with zero status on success. Startup errors are indicated with an exit code of 1;
abnormal termination is indicated with an exit code of 3.

restore command
restore command in Linux system is used for restoring files from a backup created using dump.
Eg. restore -rf /dev/st0
The above command will restore a file system that is backed up at /dev/st0

File Permissions and Access


Linux File Ownership :
Every file and directory on Unix/Linux system is assigned 3 types of owner as given below.
1. User : A user is the owner of the file. By default, the person who created a file becomes its
owner. Hence, a user is also sometimes called an owner.
2. Group : A user- group can contain multiple users. All users belonging to a group will have
the same Linux group permission access to the file. Suppose if we have a project where a
number of people require access to a file. Instead of manually assigning permissions to each
user, we can add all users to a group, and assign group permission to file such that only this
group members and no one else can read or modify the files.
3. Other : Any other user who has access to a file. This person has neither created the file, nor
he belongs to a user group who could own the file. Practically, it means everybody else.
Hence, when we set the permission for others, it is also referred as set permissions for the
world.
Permission Types
Each file or directory has three basic permission types:
1. read – The read permission refers to a user’s capability to read the contents of the file.
2. write – The write permissions refer to a user’s capability to write or modify a file or
directory.
3. execute – The execute permission affects a user’s capability to execute a file or view the
contents of a directory.
Viewing the permissions

While using the command ls –l , each file listed in the output has ten characters before the file
name. The first character specifies the type of the file.
Understanding file permissions

File permissions can be defined in two modes, each divided in 3 user categories:

1. Symbolic mode : In this mode each user category [u, g, o] and permission/right [r, w, x] is
defined using a combination of letters, where r stands for read rights, w for write rights and x for
execution rights.
User Denotations
u user/owner
g group
o other
a all

Operator Description

+ Adds a permission to a file or directory


– Removes the permission
= Sets the permission and overrides the permissions set earlier.

2.Absolute/Numeric/Octal mode : In this mode the rights are defined using a three-digit octal
number, where each digit represents the rights of a certain user category. The table below gives
numbers for all for permissions types.
Number File Permission Type Symbol
0 No permission
1 Execute --x
2 Write -w-
3 Write and Execute -wx
4 Read r--
5 Read and Execute r-x
6 Read and Write rw-
7 Read, Write and Execute rwx

Changing file/directory permissions in Linux Using ‘chmod’ command


‘chmod’ command stands for ‘change mode’. Using this command, we can set permissions (read,
write, execute) on a file/directory for the owner, group and the world.
Syntax: chmod permissions filename
In octal/numeric mode chmod can be used as :

In the above-given terminal window, we have changed the permissions of the file ‘sample to ‘764’.

‘764’ absolute code says the following:


• Owner can read, write and execute
• Usergroup can read and write
• World can only read

This is shown as ‘-rwxrw-r–


In Symbolic Mode chmod can be used as : In the symbolic mode, we can modify permissions of a
specific owner. It makes use of mathematical symbols to modify the Unix file permissions.
The various owners are represented as –

umask command in Linux


The term umask refers to two things:
1. The Linux umask command is a Linux command that lets us to set up default permissions for
newly created files and folders.
2. A user-defined permissions ‘mask’. A user can choose how to restrict permissions by using a
permissions mask. A permission mask interacts with the default system permissions and changes
them. The umask command is used to apply this mask.
The umask Command Syntax

Using the umask command


without additional command
options returns the current
mask as output:

The umask command uses the following syntax:

umask [-p] [-S] [mask]

where:
• [mask]: The new permissions mask you are applying. By default, the mask is presented as a
numeric (octal) value.
• [-S]: Displays the current mask as a symbolic value.
• [-p]: Displays the current mask along with the umask command, allowing it to be copied and
pasted as a future input.

How to calculate umask values ?

Linux uses the following default mask and permission values:

• The system default permission values are 777 (rwxrwxrwx) for folders and 666 (rw-rw-rw-)
for files.
• The default mask for a non-root user is 002, changing the folder permissions to 775
(rwxrwxr-x), and file permissions to 664 (rw-rw-r--).
• The default mask for a root user us 022, changing the folder permissions to 755 (rwxr-xr-x),
and file permissions to 644 (rw-r--r--).

This shows us that the final permission value is the result of subtracting the umask value form the
default permission value (777 or 666).

For example, if you want to change the folder permission value from 777 (read, write, and execute
for all) to 444 (read for all), you need to apply a umask value of 333, since:

777 - 444 = 333

How to set and update the default umask value ?

Use the following syntax for applying a new umask value:

umask [mask]

where [mask]: The mask you want to apply, as either a symbolic or numeric value.

Setting up a symbolic umask value

Set a new umask value by using symbolic values with the following syntax:
umask u= #,g= #,o= #
Eg : umask u+rw,g+w,o-r

where:

• u: Indicates user permissions.


• g: Indicates group permissions.
• o: Indicates other permissions.
• #: The symbolic permission value you want to apply

Note: Never use space after commas when setting up a symbolic mask value.

There are also other operators you can use:

• = creates specified file permissions and prohibits unspecified permissions.


• + creates specified permissions, but does not change unspecified permissions.
• - prohibits specified permissions, but does not change unspecified permissions.

Setting up a numeric umask value

Once you calculate the required umask numeric value, set it up by using:

umask [mask]
Eg. umask 242

where [mask]: The numeric value of the mask you want to apply.

Changing directory and root

Changing directory ‘cd’

➢ The cd command allows us to change directories. When you open a terminal, you will be in
your home directory. To move around the file system, you will use cd. Examples:
➢ To navigate into the root directory, use "cd /"
➢ To navigate to your home directory, use "cd" or "cd ~"
➢ To navigate up one directory level, use "cd .."
➢ To navigate to the previous directory (or back), use "cd -"
➢ To navigate through multiple levels of directory at once, specify the full directory path that
you want to go to. For example, use, "cd /var/www" to go directly to the /www subdirectory
of /var/. As another example, "cd ~/Desktop" will move you to the Desktop subdirectory
inside your home directory.
Changing root ‘chroot’
It is used to change the root directory to a new directory in the Linux/Unix operating
system. The chroot command can be used only by a user operating with root user authority.
Syntax : chroot / path / to / new / root command

To run the ls command with the /tmp directory as the root file system, enter:

mkdir /tmp/bin
cp /bin/ls /tmp/bin
chroot /tmp ls
Change ownership : chown command

chown is used to change the ownership of a file. Its syntax is :

chown owner_name file_name

Eg. chown Alice abc.txt // owner of abc.txt is Alice now

You might also like