Unix Self Notes
Unix Self Notes
Unix Self Notes
role of kernel:
Functions of a Kernel
Following are the functions of a Kernel:
The following are the 6 high level stages of a typical Linux boot process.
1. BIOS
BIOS stands for Basic Input/Output System
Performs some system integrity checks
Searches, loads, and executes the boot loader program.
It looks for boot loader in floppy, cd-rom, or hard drive. You can press a
key (typically F12 of F2, but it depends on your system) during the BIOS
startup to change the boot sequence.
Once the boot loader program is detected and loaded into the memory,
BIOS gives the control to it.
So, in simple terms BIOS loads and executes the MBR boot loader.
2. MBR
MBR stands for Master Boot Record.
It is located in the 1st sector of the bootable disk. Typically /dev/hda,
or /dev/sda
MBR is less than 512 bytes in size. This has three components 1) primary
boot loader info in 1st 446 bytes 2) partition table info in next 64 bytes 3)
mbr validation check in last 2 bytes.
It contains information about GRUB (or LILO in old systems).
So, in simple terms MBR loads and executes the GRUB boot loader.
3. GRUB
GRUB stands for Grand Unified Bootloader.
If you have multiple kernel images installed on your system, you can
choose which one to be executed.
GRUB displays a splash screen, waits for few seconds, if you don’t enter
anything, it loads the default kernel image as specified in the grub
configuration file.
GRUB has the knowledge of the filesystem (the older Linux loader LILO
didn’t understand filesystem).
4. Kernel
Mounts the root file system as specified in the “root=” in grub.conf
Kernel executes the /sbin/init program
Since init was the 1st program to be executed by Linux Kernel, it has the
process id (PID) of 1. Do a ‘ps -ef | grep init’ and check the pid.
initrd stands for Initial RAM Disk.
initrd is used by kernel as temporary root file system until kernel is
booted and the real root file system is mounted. It also contains
necessary drivers compiled inside, which helps it to access the hard drive
partitions, and other hardware.
5. Init
Looks at the /etc/inittab file to decide the Linux run level.
Following are the available run levels
0 – halt
1 – Single user mode
2 – Multiuser, without NFS
3 – Full multiuser mode
4 – unused
5 – X11
6 – reboot
Init identifies the default initlevel from /etc/inittab and uses that to load
all appropriate program.
Execute ‘grep initdefault /etc/inittab’ on your system to identify the
default run level
If you want to get into trouble, you can set the default run level to 0 or 6.
Since you know what 0 and 6 means, probably you might not do that.
Typically you would set the default run level to either 3 or 5.
6. Runlevel programs
When the Linux system is booting up, you might see various services
getting started. For example, it might say “starting sendmail …. OK”.
Those are the runlevel programs, executed from the run level directory
as defined by your run level.
Depending on your default init level setting, the system will execute the
programs from one of the following directories.
Run level 0 – /etc/rc.d/rc0.d/
Run level 1 – /etc/rc.d/rc1.d/
Run level 2 – /etc/rc.d/rc2.d/
Run level 3 – /etc/rc.d/rc3.d/
Run level 4 – /etc/rc.d/rc4.d/
Run level 5 – /etc/rc.d/rc5.d/
Run level 6 – /etc/rc.d/rc6.d/
Please note that there are also symbolic links available for these
directory under /etc directly. So, /etc/rc0.d is linked to /etc/rc.d/rc0.d.
Under the /etc/rc.d/rc*.d/ directories, you would see programs that start
with S and K.
Programs starts with S are used during startup. S for startup.
Programs starts with K are used during shutdown. K for kill.
There are numbers right next to S and K in the program names. Those
are the sequence number in which the programs should be started or
killed.
For example, S12syslog is to start the syslog deamon, which has the
sequence number of 12. S80sendmail is to start the sendmail daemon,
which has the sequence number of 80. So, syslog program will be started
before sendmail.
System calls
A system call is a procedure that provides the interface between a process and
the operating system. It is the way by which a computer program requests a
service from the kernel of the operating system.
Different operating systems execute different system calls.
In Linux, making a system call involves transferring control from unprivileged
user mode to privileged kernel mode; the details of this transfer vary from
architecture to architecture. The libraries take care of collecting the system-call
arguments and, if necessary, arranging those arguments in the special form
necessary to make the system call.
System calls are divided into 5 categories mainly :
Process Control
File Management
Device Management
Information Maintenance
Communication
Process Control :
This system calls perform the task of process creation, process termination, etc.
The Linux System calls under this are fork() , exit() , exec().
fork()
A new process is created by the fork() system call.
A new process may be created with fork() without a new
program being run-the new sub-process simply continues to
execute exactly the same program that the first (parent)
process was running.
It is one of the most widely used system calls under process
management.
exit()
The exit() system call is used by a program to terminate its
execution.
The operating system reclaims resources that were used by
the process after the exit() system call.
exec()
A new program will start executing after a call to exec()
Running a new program does not require that a new process
be created first: any process may call exec() at any time. The
currently running program is immediately terminated, and the
new program starts executing in the context of the existing
process.
File Management :
File management system calls handle file manipulation jobs like creating a file,
reading, and writing, etc. The Linux System calls under this are open(), read(),
write(), close().
open():
It is the system call to open a file.
This system call just opens the file, to perform operations
such as read and write, we need to execute different system
call to perform the operations.
read():
This system call opens the file in reading mode
We can not edit the files with this system call.
Multiple processes can execute the read() system call on the
same file simultaneously.
write():
This system call opens the file in writing mode
We can edit the files with this system call.
Multiple processes can not execute the write() system call on
the same file simultaneously.
close():
This system call closes the opened file.
Device Management :
Device management does the job of device manipulation like reading from
device buffers, writing into device buffers, etc. The Linux System calls under
this is ioctl().
ioctl():
ioctl() is referred to as Input and Output Control.
ioctl is a system call for device-specific input/output
operations and other operations which cannot be expressed
by regular system calls.
Information Maintenance:
It handles information and its transfer between the OS and the user program. In
addition, OS keeps the information about all its processes and system calls are
used to access this information. The System calls under this are getpid(),
alarm(), sleep().
getpid():
getpid stands for Get the Process ID.
The getpid() function shall return the process ID of the calling
process.
The getpid() function shall always be successful and no
return value is reserved to indicate an error.
alarm():
This system call sets an alarm clock for the delivery of a
signal that when it has to be reached.
It arranges for a signal to be delivered to the calling process.
sleep():
This System call suspends the execution of the currently
running process for some interval of time
Meanwhile, during this interval, another process is given
chance to execute
Communication :
These types of system calls are specially used for inter-process
communications.
Two models are used for inter-process communication
1. Message Passing(processes exchange messages with one another)
2. Shared memory(processes share memory region to communicate)
The system calls under this are pipe() , shmget() ,mmap().
pipe():
The pipe() system call is used to communicate between
different Linux processes.
It is mainly used for inter-process communication.
The pipe() system function is used to open file descriptors.
shmget():
shmget stands for shared memory segment.
It is mainly used for Shared memory communication.
This system call is used to access the shared memory and
access the messages in order to communicate with the
process.
mmap():
This function call is used to map or unmap files or devices
into memory.
The mmap() system call is responsible for mapping the
content of the file to the virtual memory space of the process.
Dictionary structure
The base of the Linux/Unix file system hierarchy begins at the root and
everything starts with the root directory.
These are the common top-level directories associated with the root
directory:
/bin – binary or executable programs.
/etc – system configuration files.
/home – home directory. It is the default current directory.
/opt – optional or third-party software.
/tmp – temporary space, typically cleared on reboot.
/usr – User related programs.
/var – log files.
/boot- It contains all the boot-related information files and folders such
as conf, grub, etc.
/dev – It is the location of the device files such as dev/sda1, dev/sda2,
etc.
/lib – It contains kernel modules and a shared library.
/lost+found – It is used to find recovered bits of corrupted files.
/media – It contains subdirectories where removal media devices
inserted.
/mnt – It contains temporary mount directories for mounting the file
system.
/proc – It is a virtual and pseudo-file system to contains info about the
running processes with a specific process ID or PID.
/run – It stores volatile runtime data.
/sbin – binary executable programs for an administrator.
/srv – It contains server-specific and server-related files.
/sys – It is a virtual filesystem for modern Linux distributions to store
and allows modification of the devices connected to the system.
System Administration
1. pwd command
Use the pwd command to find out the path of the current working directory (folder)
you’re in. The command will return an absolute (full) path, which is basically a path
of all the directories that starts with a forward slash (/). An example of an absolute
path is /home/username.
2. cd command
To navigate through the Linux files and directories, use the cd command. It requires
either the full path or the name of the directory, depending on the current working
command: cd Photos.
3. ls command
If you want to see the content of other directories, type ls and then the directory’s
of Documents.
ls -al will list the files and directories with detailed information like the
4. cat command
cat (short for concatenate) is one of the most frequently used commands in Linux. It
is used to list the contents of a file on the standard output (sdout). To run this
command, type cat followed by the file’s name and its extension. For instance: cat
file.txt.
cat filename1 filename2>filename3 joins two files (1 and 2) and stores the
>output.txt
5. cp command
Use the cp command to copy files from the current directory to a different directory.
6. mv command
The primary use of the mv command is to move files, although it can also be used
to rename files.
The arguments in mv are similar to the cp command. You need to type mv, the file’s
/home/username/Documents.
7. mkdir command
Use mkdir command to make a new directory — if you type mkdir Music it will
To generate a new directory inside another directory, use this Linux basic
“2020” file.
8. rmdir command
If you need to delete a directory, use the rmdir command. However, rmdir only
9. rm command
The rm command is used to delete directories and the contents within them. If you
Note: Be very careful with this command and double-check which directory you are
The touch command allows you to create a blank new file through the Linux
You can use this command to locate a file, just like the search command in
Windows. What’s more, using the -i argument along with this command will make it
case-insensitive, so you can search for a file even if you don’t remember its exact
name.
To search for a file that contains two or more words, use an asterisk (*). For
example, locate -i school*note command will search for any file that contains the
Similar to the locate command, using find also searches for files and directories.
The difference is, you use the find command to locate files within a given directory.
As an example, find /home/ -name notes.txt command will search for a file
Another basic Linux command that is undoubtedly helpful for everyday use is grep.
Short for “SuperUser Do”, this command enables you to perform tasks that require
for daily use because it might be easy for an error to occur if you did something
wrong.
15. df command
Use df command to get a report on the system’s disk space usage, shown in
percentage and KBs. If you want to see the report in megabytes, type df -m.
16. du command
If you want to check how much space a file or a directory takes, the du (Disk
Usage) command is the answer. However, the disk usage summary will show disk
block numbers instead of the usual size format. If you want to see it in bytes,
The head command is used to view the first lines of any text file. By default, it will
show the first ten lines, but you can change this number to your liking. For example,
if you only want to show the first five lines, type head -n 5 filename.ext.
This one has a similar function to the head command, but instead of showing the
first lines, the tail command will display the last ten lines of a text file. For
Short for difference, the diff command compares the contents of two files line by
line. After analyzing the files, it will output the lines that do not match. Programmers
often use this command when they need to make program alterations instead of
The tar command is the most used command to archive multiple files into
a tarball — a common Linux file format that is similar to zip format, with
This command is quite complex with a long list of functions such as adding new files
into an existing archive, listing the content of an archive, extracting the content from
an archive, and many more. Check out some practical examples to know more
chmod is another Linux command, used to change the read, write, and execute
permissions of files and directories. As this command is rather complicated, you can
In Linux, all files are owned by a specific user. The chown command enables you to
the file.ext.
23. jobs command
jobs command will display all current jobs along with their statuses. A job is
the kill command. It will send a certain signal to the misbehaving app and instructs
There is a total of sixty-four signals that you can use, but people usually only use
two signals:
SIGTERM (15) — requests a program to stop running and gives it some time
to save all of its progress. If you don’t specify the signal when entering the kill
be lost.
Besides knowing the signals, you also need to know the process identification
number (PID) of the program you want to kill. If you don’t know the PID, simply run
After knowing what signal you want to use and the PID of the program, enter the
following syntax:
Use the ping command to check your connectivity status to a server. For example,
by simply entering ping google.com, the command will check whether you’re able
The Linux command line is super useful — you can even download files from the
internet with the help of the wget command. To do so, simply type wget followed by
The uname command, short for Unix Name, will print detailed information about
your Linux system like the machine name, operating system, kernel, and so on.
display a list of running processes and how much CPU each process uses. It’s very
useful to monitor system resource usage, especially knowing which process needs
When you’ve been using Linux for a certain period of time, you’ll quickly notice that
you can run hundreds of commands every day. As such, running history command
is particularly useful if you want to review the commands you’ve entered before.
30. man command
Confused about the function of certain Linux commands? Don’t worry, you can
easily learn how to use them right from Linux’s shell by using the man command.
For instance, entering man tail will show the manual instruction of the tail
command.
This command is used to move some data into a file. For example, if you want to
add the text, “Hello, my name is John” into a file called name.txt, you would
Use the zip command to compress your files into a zip archive, and use
the unzip command to extract the zipped files from a zip archive.
If you want to know the name of your host/network simply type hostname. Adding
Since Linux is a multi-user system, this means more than one person can interact
with the same system at the same time. useradd is used to create a new user,
while passwd is adding a password to that user’s account. To add a new person
named John type, useradd John and then to add his password type, passwd
123456789.
To remove a user is very similar to adding a new user. To delete the users account