0% found this document useful (0 votes)
19 views

Linux Notes

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Linux Notes

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Linux File System

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. There may be below
reasons for maintaining the file system:
o Primarily the computer saves data to the RAM
storage; it may lose the data if it gets turned off.
However, there is non-volatile RAM (Flash RAM
and SSD) that is available to maintain the data
after the power interruption.
o Data storage is preferred on hard drives as
compared to standard RAM as RAM costs more
than disk space. The hard disks costs are dropping
gradually comparatively the RAM.
The Linux file system contains the following sections:
o The root directory (/)
o A specific data storage format (EXT3, EXT4, BTRFS,
XFS and so on)
o A partition or logical volume having a particular file
system.
What is the Linux File System?
Linux file system is generally a built-in layer of a Linux
operating system used to handle the data management
of the storage. It helps to arrange the file on the disk
storage. It manages the file name, file size, creation
date, and much more information about a file.
If we have an unsupported file format in our file
system, we can download software to deal with it.
Linux File System Structure
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. A partition usually has only one file system,
but it may have more than one file system.
All file systems required a namespace that is a naming
and organizational methodology. The namespace
defines the naming process, length of the file name, or
a subset of characters that can be used for the file
name. It also defines the logical structure of files on a
memory segment, such as the use of directories for
organizing the specific files.
The data structure needs to support a hierarchical
directory structure; this structure is used to describe
the available and used disk space for a particular
block. It also has the other details about the files such
as file size, date & time of creation, update, and last
modified. Also, it stores advanced information about
the section of the disk, such as partitions and volumes.
Linux file system contains two-part file system software
implementation architecture. Consider the below
image:

The file system requires an API (Application


programming interface) to access the function to
interact with file system components like files and
directories. API facilitates tasks such as creating,
deleting, and copying the files.
The first two parts of the given file system together
called a Linux virtual file system. It provides a single
set of commands for the kernel and developers to
access the file system. This virtual file system requires
the specific system driver to give an interface to the
file system.
Directory Structure
The directories help us to store the files and locate
them when we need them. Also, directories are called
folders as they can be assumed of as folders where
files reside in the form of a physical desktop analogy.
Directories can be organized in a tree-like hierarchy in
Linux and several other operating systems.
The directory structure of Linux is well-documented
and defined in the Linux FHS (Filesystem Hierarchy
Standard). Referencing those directories if accessing
them is achieved via the sequentially deeper names of
the directory linked by '/' forward slash like
/var/spool/mail and /var/log. These are known as
paths.
The below table gives a very short standard, defined,
and well-known top-level Linux directory list and their
purposes:
o / (root filesystem): It is the top-level filesystem
directory. It must include every file needed to boot
the Linux system before another filesystem is
mounted. Every other filesystem is mounted on a
well-defined and standard mount point because of
the root filesystem directories after the system is
started.
o /boot: It includes the static kernel and bootloader
configuration and executable files needed to start
a Linux computer.
o /bin: This directory includes user executable files.
o /dev: It includes the device file for all hardware
devices connected to the system. These aren't
device drivers; instead, they are files that indicate
all devices on the system and provide access to
these devices.
o /etc: It includes the local system configuration files
for the host system.
o /lib: It includes shared library files that are needed
to start the system.
o /home: The home directory storage is available for
user files. All users have a subdirectory inside
/home.
o /mnt: It is a temporary mount point for basic
filesystems that can be used at the time when the
administrator is working or repairing a filesystem.
o /media: A place for mounting external removable
media devices like USB thumb drives that might be
linked to the host.
o /opt: It contains optional files like vendor supplied
application programs that must be placed here.
o /root: It's the home directory for a root user. Keep
in mind that it's not the '/' (root) file system.
o /tmp: It is a temporary directory used by the OS
and several programs for storing temporary files.
Also, users may temporarily store files here.
Remember that files may be removed without
prior notice at any time in this directory.
o /sbin: These are system binary files. They are
executables utilized for system administration.
o /usr: They are read-only and shareable files,
including executable libraries and binaries, man
files, and several documentation types.
o /var: Here, variable data files are saved. It can
contain things such as MySQL, log files, other
database files, email inboxes, web server data
files, and much more.
Linux File System Features
In Linux, the file system creates a tree structure. All the
files are arranged as a tree and its branches. The
topmost directory called the root (/) directory. All
other directories in Linux can be accessed from the
root directory.
Some key features of linux file system are as following:
o Specifying paths: Linux does not use the backslash
(\) to separate the components; it uses forward
slash (/) as an alternative. For example, as in
Windows, the data may be stored in C:\ My
Documents\ Work, whereas, in Linux, it would be
stored in /home/ My Document/ Work.
o Partition, Directories, and Drives: Linux does not
use drive letters to organize the drive as Windows
does. In Linux, we cannot tell whether we are
addressing a partition, a network device, or an
"ordinary" directory and a Drive.
o Case Sensitivity: Linux file system is case sensitive.
It distinguishes between lowercase and uppercase
file names. Such as, there is a difference between
test.txt and Test.txt in Linux. This rule is also
applied for directories and Linux commands.
o File Extensions: In Linux, a file may have the
extension '.txt,' but it is not necessary that a file
should have a file extension. While working with
Shell, it creates some problems for the beginners
to differentiate between files and directories. If we
use the graphical file manager, it symbolizes the
files and folders.
o Hidden files: Linux distinguishes between standard
files and hidden files, mostly the configuration files
are hidden in Linux OS. Usually, we don't need to
access or read the hidden files. The hidden files in
Linux are represented by a dot (.) before the file
name (e.g., .ignore). To access the files, we need
to change the view in the file manager or need to
use a specific command in the shell.
Types of Linux File System
When we install the Linux operating system, Linux
offers many file systems such as Ext, Ext2, Ext3, Ext4,
JFS, ReiserFS, XFS, btrfs, and swap.

Let's understand each of these file systems in detail:


1. Ext, Ext2, Ext3 and Ext4 file system
The file system Ext stands for Extended File System. It
was primarily developed for MINIX OS. The Ext file
system is an older version, and is no longer used due to
some limitations.
Ext2 is the first Linux file system that allows managing
two terabytes of data. Ext3 is developed through Ext2;
it is an upgraded version of Ext2 and contains
backward compatibility. The major drawback of Ext3 is
that it does not support servers because this file
system does not support file recovery and disk
snapshot.
Ext4 file system is the faster file system among all the
Ext file systems. It is a very compatible option for the
SSD (solid-state drive) disks, and it is the default file
system in Linux distribution.
2. JFS File System
JFS stands for Journaled File System, and it is
developed by IBM for AIX Unix. It is an alternative to
the Ext file system. It can also be used in place of Ext4,
where stability is needed with few resources. It is a
handy file system when CPU power is limited.
3. ReiserFS File System
ReiserFS is an alternative to the Ext3 file system. It has
improved performance and advanced features. In the
earlier time, the ReiserFS was used as the default file
system in SUSE Linux, but later it has changed some
policies, so SUSE returned to Ext3. This file system
dynamically supports the file extension, but it has
some drawbacks in performance.
4. XFS File System
XFS file system was considered as high-speed JFS,
which is developed for parallel I/O processing. NASA
still using this file system with its high storage server
(300+ Terabyte server).
5. Btrfs File System
Btrfs stands for the B tree file system. It is used for
fault tolerance, repair system, fun administration,
extensive storage configuration, and more. It is not a
good suit for the production system.
6. Swap File System
The swap file system is used for memory paging in
Linux operating system during the system hibernation.
A system that never goes in hibernate state is required
to have swap space equal to its RAM size.
What is Mounting in the Linux Filesystem?
In Linux, the "to mount", a filesystem term, refers to
the initial days of computing when a removable disk or
tape pack would physically need to be mounted on a
correct drive device. On the disk pack, the filesystem
would logically be mounted by the OS to make
contents available to access by application programs,
OS, and users after being located on the drive
physically.
Simply, a mount point is a directory that's made as a
component of the filesystem. For instance, the home
filesystem is placed on the /home directory.
Filesystems can be placed on mount points on many
non-root filesystems, but it's less common.
o The root filesystem of Linux is mounted on the /
directory (root directory) very early inside the boot
sequence.
o Several filesystems are later mounted by the start-
up programs of Linux, either rc upon SystemV or
via systemd in new Linux versions.
o Filesystem mounting during startup is handled by
the configuration file, i.e., /etc/fstab.
o An easy way to understand that is fstab is short
for "file system table", and it's a filesystem list
that is to be mounted, their options, and
designated mount points that may be required for
particular filesystems.
Filesystems can be mounted on an available mount
point/directory with the help of the mount command.
In other words, any directory that's applied as a mount
point shouldn't have other files in it and should be
empty. Linux will not avoid users from mounting a
filesystem on one that's already available or on a
directory that includes files. The actual contents will be
covered, and just the freshly mounted filesystem
content will be visible if we mount any filesystem on
any existing filesystem or directory.
Linux File System Structure
A file system mainly consists of 3 layers. From top to
bottom:
1. Logical File System:
The Logical File System acts as the interface between
the user applications and the file system itself. It
facilitates essential operations such as opening,
reading, and closing files. Essentially, it serves as the
user-friendly front-end, ensuring that applications can
interact with the file system in a way that aligns with
user expectations.
2. Virtual File System:
The Virtual File System (VFS) is a crucial layer that
enables the concurrent operation of multiple
instances of physical file systems. It provides a
standardized interface, allowing different file systems
to coexist and operate simultaneously. This layer
abstracts the underlying complexities, ensuring
compatibility and cohesion between various file
system implementations.
3. Physical File System:
The Physical File System is responsible for the tangible
management and storage of physical memory blocks
on the disk. It handles the low-level details of storing
and retrieving data, interacting directly with the
hardware components. This layer ensures the efficient
allocation and utilization of physical storage resources,
contributing to the overall performance and reliability
of the file system.

Disk Partitioning in Linux


Disk Partitioning is the process of dividing a disk into


one or more logical areas, often known as partitions,
on which the user can work separately. It is one step of
disk formatting. If a partition is created, the disk will
store the information about the location and size of
partitions in the partition table. With the partition
table, each partition can appear to the operating
system as a logical disk, and users can read and write
data on those disks. The main advantage of disk
partitioning is that each partition can be managed
separately.
Why do we need it?
 To upgrade Hard Disk (to incorporate a new

Hard Disk into the system)


 Dual Booting (Multiple Operating Systems on

the same system)


 Efficient disk management

 Ensure backup and security.

 Work with different File Systems using the same

system.

How to Partition Disks in Linux?

In order to successfully partition a disk and to make it


useful, we need to ensure that we have completed the
below four steps, regardless of the Operating system
and Hardware of the system.
1. Attach the disk to the proper port.
2. Create partitions in the disk.
3. Create a file system on the partition.
4. Mounting the file systems
Create Partitions in the Disk
Power on the system and log in to the system. Ensure
that you are logged in as root (or any user of the sudo
group). Once the system UI comes up, open
the Terminal. To view the available Hard Disks in our
system, use the command lsblk or cat
/proc/partitions. Both commands will display the
same results, but in different ways.
We can find that the system has two disks -
> sda and sdb. sda is our older Hard Disk. We can say
so, as that disk is already partitioned as sda1 and sda2.
We can partition the disk using CLI as well as GUI. We
will demonstrate CLI based partitioning in this article.
fdisk -l
We found that the Hard Disk that we are going to
partition can be found at /dev/sdb. Use the command.
fdisk /dev/sdb
How Linux Kernel Boots?

The Linux bootstrap process, often referred to as the


boot process, is a fascinating journey that unfolds when
you power on your computer. It’s the intricate
sequence of events responsible for loading the Linux
operating system into memory, transforming your
hardware into a functional computing
powerhouse. Many processes are running in the
background when we press the system’s power button.
It is very important to learn the booting process to
understand the working of any operating system
Stages of Linux Boot Process:
1. The machine’s BIOS (Basic Input/Output
System) or boot microcode hundreds and runs a
boot loader.
2. Boot loader finds the kernel image on the disk
and loads it into memory, to start the system.
3. The kernel initializes the devices and their
drivers.
4. The kernel mounts the basis filesystem.
5. The kernel starts a program referred to
as init with a method ID zero
6. init sets the remainder of the system processes
in motion.
7. For some purpose, init starts a method
permitting you to log in, typically at the top or
close to the top of the boot sequence.
Booting Process

Bootloaders have become significantly advanced since


the origin of the Linux kernel, with options like
command-line history and menu systems, however, a
basic want has perpetually been flexibility in kernel
image and parameter choice. One stunning
development is that some wants to have really
diminished. As an example, as a result of being able to
perform associate emergency or recovery boot from a
USB device, you seldom need to worry about manually
getting into kernel parameters or going into single-
user mode. Current bootloaders supply a lot of power
than ever, which may be notably handy if you’re
building custom kernels or simply wish to tweak
parameters.
init
It is the first process executed by the kernel during the
booting of a system. It is a daemon process which runs
till the system is shutdown. That is why, it is the parent
of all the processes. First of all, init reads the script
stored in the file /etc/inittab. Command init reads the
initial configuration script which basically take care of
everything that a system do at the time of system
initialization like setting the clock, initializing the serial
port and so on.
Boot process, Init and shutdown
Introduction
One of the most powerful aspects of Linux concerns its
open method of starting and stopping the operating
system, where it loads specified programs using their
particular configurations, permits you to change those
configurations to control the boot process, and shuts
down in a graceful and organized way.
Beyond the question of controlling the boot or
shutdown process, the open nature of Linux makes it
much easier to determine the exact source of most
problems associated with starting up or shutting down
your system. A basic understanding of this process is
quite beneficial to everybody who uses a Linux system.
A lot of Linux systems use lilo, the LInux LOader for
booting operating systems. We will only discuss GRUB,
however, which is easier to use and more flexible.
Should you need information about lilo, refer to the
man pages and HOWTOs. Both systems support dual
boot installations, we refer to the HOWTOs on this
subject for practical examples and background
information.
The boot process
When an x86 computer is booted, the processor looks
at the end of the system memory for the BIOS (Basic
Input/Output System) and runs it. The BIOS program is
written into permanent read-only memory and is
always available for use. The BIOS provides the lowest
level interface to peripheral devices and controls the
first step of the boot process.
The BIOS tests the system, looks for and checks
peripherals, and then looks for a drive to use to boot
the system. Usually it checks the floppy drive (or CD-
ROM drive on many newer systems) for bootable
media, if present, and then it looks to the hard drive.
The order of the drives used for booting is usually
controlled by a particular BIOS setting on the system.
Once Linux is installed on the hard drive of a system,
the BIOS looks for a Master Boot Record (MBR) starting
at the first sector on the first hard drive, loads its
contents into memory, then passes control to it.
This MBR contains instructions on how to load the
GRUB (or LILO) boot-loader, using a pre-selected
operating system. The MBR then loads the boot-loader,
which takes over the process (if the boot-loader is
installed in the MBR). In the default Red Hat Linux
configuration, GRUB uses the settings in the MBR to
display boot options in a menu. Once GRUB has
received the correct instructions for the operating
system to start, either from its command line or
configuration file, it finds the necessary boot file and
hands off control of the machine to that operating
system.
Init
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.
Shutdown
UNIX was not made to be shut down, but if you really
must, use the shutdown command. After completing
the shutdown procedure, the -h option will halt the
system, while -r will reboot it.
The reboot and halt commands are now able to
invoke shutdown if run when the system is in run
levels 1-5, and thus ensure proper shutdown of the
system, but it is a bad habit to get into, as not all
UNIX/Linux versions have this feature.
If your computer does not power itself down, you
should not turn off the computer until you see a
message indicating that the system is halted or finished
shutting down, in order to give the system the time to
unmount all partitions. Being impatient may cause data
loss.
What are Users in Linux?
In a Linux system, users refer to individuals or entities
that interact with the operating system by logging in
and performing various tasks. User management plays
a crucial role in ensuring secure access control,
resource allocation, and system administration.

A user in Linux is associated with a user account, which


consists of several properties defining their identity
and privileges within the system. These properties are
a username, UID (User ID), GID (Group ID), home
directory, default shell, and password.

Each user account possesses these unique properties


listed above.

Type of Users in Linux


Linux supports two types of users: system users and
regular users.
System users are created by the system during
installation and are used to run system services and
applications.
Regular users are created by the administrator and can
access the system and its resources based on their
permissions.
Let's meet CTechCo's diverse workforce, consisting of
individuals who interact with the Linux system through
user accounts. Meet John, a developer; Lisa, a system
administrator; and Sarah, a marketing manager. They
each have unique usernames such as "johndoe,"
"lisasmith," and "sarahsmith," respectively. These
usernames act as their identification within the Linux
system.

How to Create Users


To create users we use the useradd command. For
example, to create John's account, Alex executes the
command below:
useradd -u 1002 -d /home/john -s /bin/bash john
This command creates John's account with uid (-u) as
1002, the home directory (-d) as /home/john and sets
(-s) /bin/bash as his default shell.
User Account Properties
Within CTechCo's Linux environment, user accounts
possess various properties that define their
characteristics and access privileges. Let's explore
these properties in the context of our use case.
1. Username: Each user is assigned a unique
username that serves as their identifier within the
Linux system. For example, John's username is
"john".
2. UID (User ID) and GID (Group ID): Every user
account is associated with a UID and GID. The UID
is a numerical value assigned to the user, while the
GID represents the primary group to which the
user belongs. For instance, John's UID may
be 1002, and his primary group's GID is 1002 as
well.
3. Home Directory: Each user has a designated home
directory where their personal files and settings
reside. John's home directory is /home/john.
4. Default Shell: The default shell determines the
command interpreter used when a user logs in. It
defines the user's interactive environment. In our
case, John's default shell is set to /bin/bash, which
is a popular shell in Linux.
5. Password: User accounts require passwords to
authenticate and access the system. CTechCo's
users, including John, must create strong
passwords to ensure security.
6. Group: The group membership determines which
system resources the user can access, as well as
which users can access the user's files.
Alex could take a look at the users on their Linux by
running the cat /etc/passwd command. The users will
be displayed in this format:
john:x:1002:1002:,,,:/home/john:/bin/bash
Here's what each of the fields in the format above
represents:

 john: This is the username of the user account.


 x: This field contains the encrypted password of
the user. It is replaced with an 'x' character to
indicate that the password is stored in
the /etc/shadow file for security reasons.
 1002: This is the UID (User ID) of the user account,
which is a unique numerical identifier assigned to
the user by the system.
 1002: This is the GID (Group ID) of the user
account, which represents the primary group
membership of the user.
 ,,,: This is the GECOS field, which stands for
"General Electric Comprehensive Operating
System". This field is used to store additional
information about the user, such as their full name
or contact information. In this case, the field is
empty, as no additional information was provided
while creating the user account.
 /home/john: This is the home directory of the user
account, which is the location where the user's
files and personal data are stored.
 /bin/bash: This is the default shell for the user
account, which is the command interpreter used
to process commands entered by the user in the
terminal. In this case, the default shell is Bash,
which is the most commonly used shell in Linux.
How to Delete Users
Let's assume that Lisa has left CTechCo. To remove her
account and associated files, Alex has to utilize
the userdel command. For instance, to delete Lisa's
account, Alex runs:
sudo userdel lisa
This will delete the user account for lisa, along with
their home directory and any files or directories owned
by the user.
Understanding Linux administration and Linux user
management
user management is a fundamental part of Linux
administration. Proper user management allows
administrators to provide a structured user
environment, control access to resources and maintain
system security. Linux user management involves tasks
such as creating and managing user accounts, setting
up user permissions and access control, and
monitoring activity to identify security breaches.
As a Linux administrator, you should have a good grasp
of the Linux CLI so you can execute commands that are
crucial for system administration and user
management.
Useful Linux commands for user management
Linux user management commands are
straightforward and allow administrators to perform
basic and advanced operations such as creating,
modifying, and deleting user accounts and groups and
managing account properties. Some useful Linux
commands that administrators use regularly include:

Comman Description
d

useradd Create new Linux user accounts and customize them by specifying various
options such as username, home directory, and user ID.

passwd Set or change the password for a user account.

usermod Modify the attributes of a user account, such as the username, home directory,
user ID or group ID associated with a particular user.

userdel Delete a user account that is no longer needed from the system, ensuring that
the user no longer has access to the system resources.

groupadd Add a new group in Linux and specify group options.

groupmod Modify or change an existing group.

groupdel Delete an existing group and all the files associated with that group.

chown Transfer the ownership of a file or directory to a different user or group.


chmod Change the permissions of a file or directory.

chgrp Change the group ownership of a file or directory with this command.

Linux administration: Creating and managing user


accounts
To create a new user account in Linux:
1. Open the command line and type the “useradd”
command followed by the username.
useradd [options] USERNAME
This command creates a new user account with default
settings, such as a home directory and user ID. You can
also specify different options to customize these
settings.
2. After creating the account, you can set or change the
password by typing the “passwd” command.
passwd [options] USERNAME
3. Modify various attributes of a user account using the
“usermod” command, including changing the
username, home directory, user ID or group ID.
usermod [options] USERNAME
4. To delete a user account, type the “userdel”
command followed by the username. This removes the
user account from the system, ensuring that the user
no longer has access to system resources.
userdel [options] USERNAME
5. Use the “chown” command to change the ownership
of a file or directory and the “chmod” command to
modify the permissions.
chown newowner file(s)
chmod [options] file
Linux user management: Setting up user permissions
and access control
Properly configuring user permissions and access
control in Linux ensures that users have appropriate
access to system resources while maintaining the
security and integrity of the system.
In Linux, each file and directory is associated with three
types of permissions: read, write, and execute
(designated by r, w and e).
These permissions can be granted or restricted for the
owner, group and others. By default, the owner has
read, write and execute permissions, while the group
and others have only read and execute permissions.
To modify permissions, you can use the chmod
command followed by the desired options. For
example, to grant read and write permissions to the
owner, you can use the command:
chmod u+rw file.txt
To remove read and write permissions for the group,
use the command:
chmod g-rw file.txt
Linux also supports access control lists (ACLs) which
provide more fine-grained control over user access.
ACLs allow you to specify access permissions for
individual users or groups, overriding the default
permissions.

You might also like