Linux Unit 3
Linux Unit 3
Que. 1: Explain the file system and file permissions in Linux (6)
Or Write short note on file permissions in Linux (3/4)
Answer:
Linux File System: The Linux file system contains the following sections:
A) The root directory (/)
B) A partition or logical volume having a particular file system.
C) A specific data storage format (EXT3, EXT4, BTRFS, XFS and so on)
A) Root Directory:- Linux file system considers everything as a file in Linux; whether it is text file images,
partitions, compiled programs, directories, or hardware devices. If it is not a file, then it must be a
process. Linux file system has a hierarchal file structure as it contains a root directory and its
subdirectories. All other directories can be accessed from the root directory. To manage the data, it
forms a tree structure.
B) Partitions:- A Linux file system is a structured collection of files on a disk drive or a partition. A
partition is a segment of memory and contains some specific data. In our machine, there can be various
partitions of the memory. Generally, every partition contains a file system.
The general-purpose computer system needs to store data systematically so that we can easily
access the files in less time. It stores the data on hard disks (HDD) or some equivalent storage type.
C) Data storage format:- When we install the Linux operating system, Linux offers many file systems
such as Ext, Ext2, Ext3, Ext4, JFS, ReiserFS, XFS, btrfs, and swap.
Linux files are case sensitive, so test.txt and Test.txt will be considered as two different files.
There are multiple ways to create a file in Linux. Some conventional methods are as follows:
o using cat command
o using touch command
o using redirect '>' symbol
o using echo command
o using printf command
o using a different text editor like vim, nano, vi
Apart from all of the above methods, we can also create a file from the desktop file manager.
Working with Linux file permissions:
File ownership provides a secure method for storing files. Every file has the following attributes −
Owner permissions − The owner's permissions determine what actions the owner of the file can
perform on the file.
Group permissions − The group's permissions determine what actions can be performed by all users of
that group.
Other (world) permissions − The permissions for others indicate what action all other users can
perform on the file.
All the above three owners in the Linux system have three types of permissions defined. Nine characters
denotes the three types of permissions.
1. Read (r) : The read permission allows you to open and read the content of a file. But you can't do any
editing or modification in the file.
2. Write (w) : The write permission allows you to edit, remove or rename a file. For instance, if a file is
present in a directory, and write permission is set on the file but not on the directory, then you can edit
the content of the file but can't remove, or rename it.
3. Execute (x): In Linux type system, you can't run or execute a program unless execute permission is
set.But in Windows, there is no such permission available.
Permissions are listed below:
Permission For a File For a Directory
r (read) read file content (cat) read directory content (ls)
w (write) change file content (vi) create file in directory (touch)
x (execute) execute the file enter the directory (cd)
Que. 3: How the permissions are set to file or directory? Explain with example. (6)
Answer:
File/Directory Access Modes: The permissions of a file are the first line of defense in the security of a
Linux system. The basic building blocks of file permissions are the read, write, and execute, which have
been described below −
Read: Grants the capability to read, i.e., view the contents of the file.
Write: Grants the capability to modify, or remove the content of the file.
Execute: User with execute permissions can run a file as a program.
Changing Permissions
To change the file or the directory permissions, you use the chmod (change mode) command. There are two
ways to use chmod — the symbolic mode and the absolute mode.
Syntax:
chmod <groupName>+/-[=]<permissionName> <fileName>
Using chmod in Symbolic Mode: The easiest way for a beginner to modify file or directory permissions
is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the permission
set you want by using the operators in the following table.
Chmod operator Description
+ Adds the designated permission(s) to a file or directory.
- Removes the designated permission(s) from a file or directory.
= Sets the designated permission(s).
Examples:
$chmod o +wx testfile
$chmod u-x testfile
$chmod g=rx testfile
$chmod o =rw testfile
$chmod u =rwx,g=rw,o=r testfile
$chmod o +wx,u-x,g = rx testfile
Using chmod in Absolute/ Numeric Mode: There are three user categories: User (the owner of the
file), Group (the security group you are in), and Other (for the world to see). Each category has three
permissions that can be set: r, w, and x to read, write, and execute a file, respectively. Permissions
consist of three numbers: 4 for Read, 2 for Write, and 1 for Execute access. By adding these numbers
together, you form the permissions that make up one digit. This table can be used as a quick reference:
Permission User Group Other
Read = 4 x x X
Write = 2 x
Execute = 1 x x X
Totals (4+2+1) = 7 (4 + 1) = 5 (4 + 1) = 5
For example, 4 + 2 + 1 = 7, which grants read, write, and execute permissions; 4 + 1 = 5, which grants only
read and execute permissions. Thus, 755 grants 7 (read, write, execute) to the owner of the file, and 5 (read
and execute) to the group the file is in and 5 (read and execute) to the world.
Examples:
1. Read by owner only $ chmod 400 sample.txt
2. Read by group only $ chmod 040 sample.txt
3. Read by anyone $ chmod 004 sample.txt
4. Write by owner only $ chmod 200 sample.txt
5. Write by group only $ chmod 020 sample.txt
6. Write by anyone $ chmod 002 sample.txt
7. Execute by owner only $ chmod 100 sample.txt
8. Execute by group only $ chmod 010 sample.txt
9. Execute by anyone $ chmod 001 sample.txt
10. Allow read permission to owner and group and anyone. $ chmod 444 sample.txt
11. Allow everyone to read, write, and execute file. $ chmod 777 sample.txt
12. Deny execute permission to everyone. $ chmod a-x sample.txt
13. Allow read permission to everyone. $ chmod a+r sample.txt
14. Make a file readable and writable by the group and others. $ chmod go+rw sample.txt
15. Make a shell script executable by the user/owner. $ chmod u+x samplescript.sh
Que. 4: Write short note on –
a) Changing the default permissions (4)
b) Changing the ownership of the files. (4)
Answer:
A) Changing the default permission: By default, on Linux systems, the default creation permissions
are 666 for files, which gives read and write permission to user, group, and others, and to 777 for
directories, which means read, write and execute permission to user, group, and others. Linux does not
allow a file to be created with execute permissions.
The umask command in Linux is used to set default permissions for files or directories. The
syntax is similar to that of chmod, but use the = operator to set the default permissions.
Syntax:
umask <permissions>
Example:
umask u=rwx,g=rwx,o=rx
The above command will give set read, write and execute permission to owner and group members. At the
same time it will set only read and execute permission to others.
The umask command can be only used on new files i.e. while creating new files, any files created prior
to using the umask command will have no effect.
The chmod command must be used on files that are already present, it is used to change the access
permissions of files that have been created earlier.
Some common umask settings are as follows -