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

Unix Self Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

UNIT-7 UNIX OR LINUX OPERATING SYSTEM

 Design principles of Unix/Linux


 role and function of kernel

role of kernel:

The Kernel is responsible for low-level tasks such as disk


management, memory management, task management,
etc. It provides an interface between the user and the
hardware components of the system. When a process
makes a request to the Kernel, then it is called System
Call.

Functions of a Kernel
Following are the functions of a Kernel:

 Access Computer resource: A Kernel can access


various computer resources like the CPU, I/O devices
and other resources. It acts as a bridge between the
user and the resources of the system.
 Resource Management: It is the duty of a Kernel to
share the resources between various process in such a
way that there is uniform access to the resources by
every process.
 Memory Management: Every process needs some
memory space. So, memory must be allocated and
deallocated for its execution. All these memory
management is done by a Kernel.
 Device Management: The peripheral devices
connected in the system are used by the processes. So,
the allocation of these devices is managed by the
Kernel.
 Linux booting process

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

In Linux/Unix operating system everything is a file even directories are files,


files are files, and devices like mouse, keyboard, printer, etc are also files. Here
we are going to see the Directory Structure in Linux.
Types of files in the Linux system.
1. General Files – It is also called ordinary files. It may be an image,
video, program, or simple text files. These types of files can be in
ASCII or Binary format. It is the most commonly used file in the Linux
system.
2. Directory Files – These types of files are a warehouse for other file
types. It may be a directory file within a directory (subdirectory).
3. Device Files – In a Windows-like operating system, devices like CD-
ROM, and hard drives are represented as drive letters like F: G: H
whereas in the Linux system device are represented as files. As for
example, /dev/sda1, /dev/sda2 and so on.
We know that Windows-like operating system, files are stored in different
folders on different data drives like C: D: E: whereas in the Linux/Unix operating
system files are stored in a tree-like structure starting with the root directory as
shown in the below diagram.

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.

Some other directories in the Linux system:

 /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

Linux system administration

Linux system administration is a process of setting up,


configuring, and managing a computer system in
a Linux environment. System administration involves creating a
user account, taking reports, performing backup, updating
configuration files, documentation, and performing recovery
actions. The user who manages the server, fixes configuration
issues, recommends new software updates, and updates document
is the system administrator.
 Elementary linux command

Here is a list of basic Linux commands:

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

directory that you’re in.

Let’s say you’re in /home/username/Documents and you want to go to Photos, a

subdirectory of Documents. To do so, simply type the following

command: cd Photos.

Another scenario is if you want to switch to a completely new directory, for

example,/home/username/Movies. In this case, you have to type cd followed by

the directory’s absolute path: cd /home/username/Movies.

There are some shortcuts to help you navigate quickly:

 cd .. (with two dots) to move one directory up

 cd to go straight to the home folder

 cd- (with a hyphen) to move to your previous directory


On a side note, Linux’s shell is case sensitive. So, you have to type the name’s

directory exactly as it is.

3. ls command

The ls command is used to view the contents of a directory. By default, this

command will display the contents of your current working directory.

If you want to see the content of other directories, type ls and then the directory’s

path. For example, enter ls /home/username/Documents to view the content

of Documents.

There are variations you can use with the ls command:

 ls -R will list all the files in the sub-directories as well

 ls -a will show the hidden files

 ls -al will list the files and directories with detailed information like the

permissions, size, owner, etc.

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.

Here are other ways to use the cat command:

 cat > filename creates a new file

 cat filename1 filename2>filename3 joins two files (1 and 2) and stores the

output of them in a new file (3)


 to convert a file to upper or lower case use, cat filename | tr a-z A-Z

>output.txt

5. cp command

Use the cp command to copy files from the current directory to a different directory.

For instance, the command cp scenery.jpg /home/username/Pictures would

create a copy of scenery.jpg (from your current directory) into

the Pictures 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

name, and the destination’s directory. For example: mv file.txt

/home/username/Documents.

To rename files, the Linux command is mv oldname.ext newname.ext

7. mkdir command

Use mkdir command to make a new directory — if you type mkdir Music it will

create a directory called Music.

There are extra mkdir commands as well:

 To generate a new directory inside another directory, use this Linux basic

command mkdir Music/Newfile


 use the p (parents) option to create a directory in between two existing

directories. For example, mkdir -p Music/2020/Newfile will create the new

“2020” file.

8. rmdir command

If you need to delete a directory, use the rmdir command. However, rmdir only

allows you to delete empty directories.

9. rm command

The rm command is used to delete directories and the contents within them. If you

only want to delete the directory — as an alternative to rmdir — use rm -r.

Note: Be very careful with this command and double-check which directory you are

in. This will delete everything and there is no undo.

10. touch command

The touch command allows you to create a blank new file through the Linux

command line. As an example, enter


touch /home/username/Documents/Web.html to create an HTML file

entitled Web under the Documents directory.

11. locate command

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

word “school” and “note”, whether it is uppercase or lowercase.

12. find command

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

called notes.txt within the home directory and its subdirectories.

Other variations when using the find are:

 To find files in the current directory use, find . -name notes.txt

 To look for directories use, / -type d -name notes. txt

13. grep command

Another basic Linux command that is undoubtedly helpful for everyday use is grep.

It lets you search through all the text in a given file.


To illustrate, grep blue notepad.txt will search for the word blue in the notepad file.

Lines that contain the searched word will be displayed fully.

14. sudo command

Short for “SuperUser Do”, this command enables you to perform tasks that require

administrative or root permissions. However, it is not advisable to use this command

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,

kilobytes, and megabytes, add the -h argument to the command line.

17. head command

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.

18. tail command

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

example, tail -n filename.ext.

19. diff command

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

rewriting the entire source code.

The simplest form of this command is diff file1.ext file2.ext

20. tar command

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

compression being optional.

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

about other functions.

21. chmod command

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

read the full tutorial in order to execute it properly.

22. chown command

In Linux, all files are owned by a specific user. The chown command enables you to

change or transfer the ownership of a file to the specified username. For

instance, chown linuxuser2 file.ext will make linuxuser2 as the owner of

the file.ext.
23. jobs command

jobs command will display all current jobs along with their statuses. A job is

basically a process that is started by the shell.

24. kill command

If you have an unresponsive program, you can terminate it manually by using

the kill command. It will send a certain signal to the misbehaving app and instructs

the app to terminate itself.

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

command, this signal will be used.

 SIGKILL (9) — forces programs to stop immediately. Unsaved progress will

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

the command ps ux.

After knowing what signal you want to use and the PID of the program, enter the

following syntax:

kill [signal option] PID.


25. ping command

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

to connect to Google and also measure the response time.

26. wget command

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 download link.

27. uname command

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.

28. top command

As a terminal equivalent to Task Manager in Windows, the top command will

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

to be terminated because it consumes too many resources.

29. history command

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.

31. echo 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

type echo Hello, my name is John >> name.txt

32. zip, unzip command

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.

33. hostname command

If you want to know the name of your host/network simply type hostname. Adding

a -i to the end will display the IP address of your network.

34. useradd, userdel command

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

type, userdel UserName

You might also like