Linux - Q & A

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

LINUX

Linux is one of popular version of UNIX operating System. It is open


source as its source code is freely available. It is free to use. Linux was
designed considering UNIX compatibility. Its functionality list is quite
similar to that of UNIX.

Components of Linux System


Linux Operating System has primarily three components

 Kernel − Kernel is the core part of Linux. It is responsible for all major
activities of this operating system. It consists of various modules and it
interacts directly with the underlying hardware. Kernel provides the required
abstraction to hide low level hardware details to system or application
programs.

 System Library − System libraries are special functions or programs using


which application programs or system utilities accesses Kernel's features.
These libraries implement most of the functionalities of the operating system
and do not requires kernel module's code access rights.

 System Utility − System Utility programs are responsible to do specialized,


individual level tasks.

Kernel Mode vs User Mode


Kernel component code executes in a special privileged mode
called kernel mode with full access to all resources of the computer.
This code represents a single process, executes in single address space
and do not require any context switch and hence is very efficient and
fast. Kernel runs each processes and provides system services to
processes, provides protected access to hardware to processes.

Support code which is not required to run in kernel mode is in System


Library. User programs and other system programs works in User
Mode which has no access to system hardware and kernel code. User
programs/ utilities use System libraries to access Kernel functions to get
system's low level tasks.

Architecture
The following illustration shows the architecture of a Linux system −

The architecture of a Linux System consists of the following layers −

 Hardware layer − Hardware consists of all peripheral devices (RAM/ HDD/


CPU etc).

 Kernel − It is the core component of Operating System, interacts directly


with hardware, provides low level services to upper layer components.

 Shell − An interface to kernel, hiding complexity of kernel's functions from


users. The shell takes commands from the user and executes kernel's
functions.

 Utilities − Utility programs that provide the user most of the functionalities
of an operating systems.
1. What is kernel in Linux ?
The kernel is the core program that runs programs and manages hardware
devices such as disks and printers. It executes the commands which are
provided by the environment. Kernel provides the interface between the shell
and hardware.
A kernel is really nothing more than a resource manager. Whether the resource
being managed is a process, memory, or hardware device, the kernel manages
and arbitrates access to the resource between multiple competing users (both in
the kernel and in user space).

2. Define the features of Linux.


a) Multitasking : Linux supports true pre-emptive multitasking wherein all
processes run independently of each other. No process needs to be concerned
with making processor time available to other processes.
b) Multi-user access : Able to concurrently and independently execute several
applications belonging to 2 or more users.
c) Multiprocessing : Runs on multi-processor architectures that means OS can
distribute several applications across several processes.
d) Architectural independence(Portability) : Runs on several hardware platforms,
from the Amiga to the PC to DEC Alpha workstations. Such hardware
independence is achieved by no other operating systems.
e) Demand load executables : Only those parts of a program actually required for
execution are loaded into memory. When a new process is created using fork(),
memory is not requested immediately but instead the memory for the present
process is used jointly by both processes.
f) Paging : Despite the best efforts to use physical memory efficiently it can
happen that the available memory is fully taken up.
g) Dynamic cache for hard disk : Linux dynamically adjusts the size of cache
memory in use to suit the current memory usage situation.
h) Shared libraries; Libraries are collections of routines required by a program for
processing data. These are number of libraries used by one process at the same
time.
i) Memory protected mode: Linux uses the processor’s memory protection
mechanism to prevent the process from accessing memory allocated to the
system or other processor.
j) Support for national keyboards and fonts : Support wide range of keyboards
and sets. For example, Latin1 set defined by the International Organization for
Standardization (ISO) which also includes European special characters.
k) Different file systems : Linux provides a standard file structure in which system
files/ user files are arranged. Linux supports a variety of file systems. The most
commonly used file system at present is the 2 nd extended file system (EXT2).
This supports filenames of upto 255 characters and has a number of features
making it more secure than conventional Unix filesystems.

l) Shell : Linux provides a special interpreter program which can be used to


execute commands of the operating system. It can be used to do various types
of operations, call application programs. etc.
m)Security − Linux provides user security using authentication features like
password protection/ controlled access to specific files/ encryption of data.

3. Define file system structure in Linux.


A Linux system, just like UNIX, makes no difference between a file and a directory,
since a directory is just a file containing names of other files. Programs, services, texts,
images, and so forth, are all files. Input and output devices, and generally all devices, are
considered to be files, according to the system.

In order to manage all those files in an orderly fashion, it is arranged in an ordered tree-
like structure on the hard disk, as we know from MS-DOS (Disk Operating System) for
instance. The large branches contain more branches, and the branches at the end contain
the tree's leaves or normal files.

The tree of the file system starts at the trunk or slash, indicated by a forward slash (/).
This directory, containing all underlying directories and files, is also called the root
directory or "the root" of the file system.

Directories that are only one level below the root directory are often preceded by a slash,
to indicate their position and prevent confusion with other directories that could have the
same name.

Following is the file system layout in Linux :


Most files are just files, called regular files; they contain normal data, for example
text files, executable files or programs, input for or output from a program and so
on.

 Directories: files that are lists of other files.

 Special files: the mechanism used for input and output. Most special files are
in /dev

 Links: a system to make a file or directory visible in multiple parts of the


system's file tree.

 (Domain) sockets: a special file type, similar to TCP/IP sockets, providing


inter-process networking protected by the file system's access control.

 Named pipes: act more or less like sockets and form a way for processes to
communicate with each other, without using network socket semantics.

1. / – Root
 Every single file and directory starts from the root directory.
 Only root user has write privilege under this directory.
 Please note that /root is root user’s home directory, which is not same as /.
2. /bin – User Binaries
 Contains binary executables.
 Common linux commands you need to use in single-user modes are located
under this directory.
 Commands used by all the users of the system are located here.
 For example: ps, ls, ping, grep, cp.

3. /sbin – System Binaries


 Just like /bin, /sbin also contains binary executables.
 But, the linux commands located under this directory are used typically by
system aministrator, for system maintenance purpose.
 For example: iptables, reboot, fdisk, ifconfig, swapon

4. /etc – Configuration Files


 Contains configuration files required by all programs.
 This also contains startup and shutdown shell scripts used to start/stop individual
programs.
 For example: /etc/resolv.conf, /etc/logrotate.conf

5. /dev – Device Files


 Contains device files.
 These include terminal devices, usb, or any device attached to the system.
 For example: /dev/tty1, /dev/usbmon0

6. /proc – Process Information


 Contains information about system process.
 This is a pseudo filesystem contains information about running process. For
example: /proc/{pid} directory contains information about the process with that
particular pid.
 This is a virtual filesystem with text information about system resources. For
example: /proc/uptime

7. /var – Variable Files


 var stands for variable files.
 Content of the files that are expected to grow can be found under this directory.
 This includes — system log files (/var/log); packages and database files (/var/lib);
emails (/var/mail); print queues (/var/spool); lock files (/var/lock); temp files
needed across reboots (/var/tmp);

8. /tmp – Temporary Files


 Directory that contains temporary files created by system and users.
 Files under this directory are deleted when system is rebooted.
9. /usr – User Programs
 Contains binaries, libraries, documentation, and source-code for second level
programs.
 /usr/bin contains binary files for user programs. If you can’t find a user binary
under /bin, look under /usr/bin. For example: at, awk, cc, less, scp
 /usr/sbin contains binary files for system administrators. If you can’t find a system
binary under /sbin, look under /usr/sbin. For example: atd, cron, sshd, useradd,
userdel
 /usr/lib contains libraries for /usr/bin and /usr/sbin
 /usr/local contains users programs that you install from source. For example,
when you install apache from source, it goes under /usr/local/apache2

10. /home – Home Directories


 Home directories for all users to store their personal files.
 For example: /home/john, /home/nikita

11. /boot – Boot Loader Files


 Contains boot loader related files.
 Kernel initrd, vmlinux, grub files are located under /boot
 For example: initrd.img-2.6.32-24-generic, vmlinuz-2.6.32-24-generic

12. /lib – System Libraries


 Contains library files that supports the binaries located under /bin and /sbin
 Library filenames are either ld* or lib*.so.*
 For example: ld-2.11.1.so, libncurses.so.5.7

13. /opt – Optional add-on Applications


 opt stands for optional.
 Contains add-on applications from individual vendors.
 add-on applications should be installed under either /opt/ or /opt/ sub-directory.

14. /mnt – Mount Directory


 Temporary mount directory where sysadmins can mount filesystems.

15. /media – Removable Media Devices


 Temporary mount directory for removable devices.
 For examples, /media/cdrom for CD-ROM; /media/floppy for floppy drives;
/media/cd recorder for CD writer

16. /srv – Service Data


 srv stands for service.
 Contains server specific services related data.
 For example, /srv/cvs contains CVS related data. (cvs- concurrent version
control system)

4. Define Linux Kernel and its architecture.

Linux is a unix-like computer operating system assembled under the model of


free and open source software development and distribution. The defining
component of Linux is the Linux kernel, an operating system kernel first released
on 5th Oct 1991 by Linus Torvalds. Linux is the most popular open source
operating system with many advantages, such that that their internals are open
to all to view.

Below is the fundamental architecture of GNU/Linux operating system.

When Linux is running in the main memory it is divided into two parts – User space and
Kernel space.
At the top is the user, or application, space. This is where the user applications
are executed. Below the user space is the kernel space. Here, the Linux kernel
exists.

There is also the GNU C Library (glibc). This provides the system call interface
that connects to the kernel and provides the mechanism to transition between the
user-space application and the kernel. This is important because the kernel and
user application occupy different protected address spaces. And while each user-
space process occupies its own virtual address space, the kernel occupies a
single address space.

The Linux kernel can be further divided into three gross levels. At the top is the
system call interface, which implements the basic functions such as
read and write. Below the system call interface is the kernel code, which can be
more accurately defined as the architecture-independent kernel code. This code
is common to all of the processor architectures supported by Linux. Below this is
the architecture-dependent code, which forms what is more commonly called a
BSP (Board Support Package). This code serves as the processor and platform-
specific code for the given architecture.
At a high level, and at lower levels, the kernel is layered into a number of
distinct subsystems. Linux can also be considered monolithic because it
lumps all of the basic services into the kernel. This differs from a
microkernel architecture where the kernel provides basic services such as
communication, I/O, and memory and process management, and more
specific services are plugged in to the microkernel layer.

Major Components of Linux Kernel

System call interface

The SCI is a thin layer that provides the means to perform function calls from user
space into the kernel. As discussed previously, this interface can be architecture
dependent, even within the same processor family. The SCI is actually an interesting
function-call multiplexing and demultiplexing service. The SCI implementation can be
found in ./linux/kernel, as well as architecture-dependent portions in ./linux/arch.

Process management

Process management is focused on the execution of processes. In the kernel, these


are called threads and represent an individual virtualization of the processor (thread
code, data, stack, and CPU registers). In user space, the term process is typically
used, though the Linux implementation does not separate the two concepts
(processes and threads).

The kernel is in charge of creating and destroying processes and handling their
connection to the outside world(input and output). Communication among different
processes (through signals, pipes or inter process communication primitives is basic
to the overall system functionality and is also handled by the kernel. In addition, the
scheduler, which controls how processes share the CPU, is part of process
management. You can find the process management sources in ./linux/kernel
and architecture-dependent sources in ./linux/arch).

Memory management

Another important resource that's managed by the kernel is memory. For efficiency,
the hardware manages virtual memory, memory is managed in what are
called pages (4KB in size for most architectures). Linux includes the means to
manage the available memory, as well as the hardware mechanisms for physical
and virtual mappings. You can find the memory management sources in ./linux/mm

Virtual file system

The virtual file system (VFS) is an interesting aspect of the Linux kernel because it
provides a common interface abstraction for file systems. The VFS provides a
switching layer between the SCI and the file systems supported by the kernel

At the top of the VFS is a common API abstraction of functions such as open, close,
read, and write. At the bottom of the VFS are the file system abstractions that define
how the upper-layer functions are implemented. These are plug-ins for the given file
system (of which over 50 exist). You can find the file system sources in ./linux/fs.

Below the file system layer is the buffer cache, which provides a common set of
functions to the file system layer (independent of any particular file system). This
caching layer optimizes access to the physical devices by keeping data around for a
short time. Below the buffer cache are the device drivers, which implement the
interface for the particular physical device.
Network stack

The network stack, by design, follows a layered architecture modelled after the
protocols themselves. Above TCP is the sockets layer, which is invoked through the
SCI.
The sockets layer is the standard API to the networking subsystem and provides a
user interface to a variety of networking protocols. From raw frame access to IP
protocol data units (PDUs) and up to TCP and the User Datagram Protocol (UDP),
the sockets layer provides a standardized way to manage connections and move
data between endpoints. You can find the networking sources in the kernel at
./linux/net.

Device drivers
The vast majority of the source code in the Linux kernel exists in device drivers that
make a particular hardware device usable. The Linux source tree provides a drivers
subdirectory that is further divided by the various devices that are supported, such as
Bluetooth, I2C, serial, and so on. You can find the device driver sources in
./linux/drivers.

Architecture-dependent code

While much of Linux is independent of the architecture on which it runs, there are
elements that must consider the architecture for normal operation and for efficiency.
The ./linux/arch subdirectory defines the architecture-dependent portion of the kernel
source contained in a number of subdirectories that are specific to the architecture
(collectively forming the BSP. Each architecture subdirectory contains a number of
other subdirectories that focus on a particular aspect of the kernel, such as boot,
kernel, memory management, and others. You can find the architecture-dependent
code in ./linux/arch.

What is a Computing Environment?


Computing environment is the Platform(Platform = Operating
System+ Processor) where a user can run programs.
What is a Variable?
In computer science, a variable is a location for storing a
value which can be a filename, text, number or any other data.
It is usually referred to with its Symbolic name which is given to it
while creation. The value thus stored can be displayed, deleted,
edited and re-saved.

Variables play an important role in computer programming because


they enable programmers to write flexible programs. As they are
related to the Operating system that we work on, it is important to
know some of them and how we can influence them.
What are Environment variables?

 Dynamic values which affect the processes or programs on a


computer
 Exist in every operating system; types may vary
 Can be created, edited, saved and deleted
 Gives information about the system behavior
Environment variables can change the way a software/programs
behaves.

E.g. $LANG environment variable stores the value of the language


that the user understands. This value is read by an application such
that a Chinese user is shown a Mandarin interface while an
American user is shown an English interface.

Let's study some common environment variables -

Variab Description
le

PATH This variable contains a colon (:)-separated list of


directories in which your system looks for executable
files. When you enter a command on terminal, the
shell looks for the command in different directories
mentioned in the $PATH variable. If the command is
found , it executes. Otherwise, it returns with an error
'command not found'.

USER The username

HOME Default path to the user's home directory

EDITO Path to the program which edits the content of files


R

UID User's unique ID

TERM Default terminal emulator


SHELL Shell being used by the user

Accessing Variable values


In order to determine value of a variable , use the command

echo $VARIABLE

Variables are- Case Sensitive. Make sure that you type the variable
name in the right letter case otherwise you may not get the desired
results.

The 'env' command displays all the environment variables .


Creating New Variables
You can create your own user defined variable , with syntax

VARIABLE_NAME= variable_value

Again, bear in mind that variables are case-sensitive and usually


they are created in upper case.

Environment variables govern behaviour of programs in your


Operating System.

Command Description

echo $VARIABLE To display value of a variable

env Displays all environment variables

VARIABLE_NAME= Create a new variable


variable_value

unset Remove a variable

export Variable=value To set value of an environment


variable

File Compression and Archiving


It is useful to store a group of files in one file for easy backup, for transfer to another
directory, or for transfer to another computer. It is also useful to compress large files;
compressed files take up less disk space and download faster via the Internet.
It is important to understand the distinction between an archive file and
a compressed file. An archive file is a collection of files and directories stored in one
file. The archive file is not compressed — it uses the same amount of disk space as
all the individual files and directories combined. A compressed file is a collection of
files and directories that are stored in one file and stored in a way that uses less disk
space than all the individual files and directories combined. If disk space is a
concern, compress rarely-used files, or place all such files in a single archive file and
compress it.
Compressing Files at the Shell Prompt
Red Hat Enterprise Linux provides the bzip2, gzip, and zip tools for compression
from a shell prompt. The bzip2 compression tool is recommended because it
provides the most compression and is found on most UNIX-like operating systems.
The gzip compression tool can also be found on most UNIX-like operating systems.
To transfer files between Linux and other operating system such as MS Windows,
use zip because it is more compatible with the compression utilities available for
Windows.

Compression Tool File Extension Decompression


Tool
bzip2 .bz2 bunzip2
gzip .gz gunzip
zip .zip unzip
By convention, files compressed with bzip2 are given the extension .bz2, files
compressed with gzip are given the extension .gz, and files compressed
with zip are given the extension .zip.
Files compressed with bzip2 are uncompressed with bunzip2, files compressed
with gzip are uncompressed with gunzip, and files compressed with zip are
uncompressed withunzip.

Bzip2 and Bunzip2


To use bzip2 to compress a file, enter the following command at a shell prompt:

bzip2 filename

The file is compressed and saved as filename.bz2.


To expand the compressed file, enter the following command:

bunzip2 filename.bz2

The filename.bz2 compressed file is deleted and replaced with filename.


You can use bzip2 to compress multiple files and directories at the same time by
listing them with a space between each one:

bzip2 filename.bz2 file1 file2 file3 /usr/work/school


The above command compresses file1, file2, file3, and the contents of
the /usr/work/school/ directory (assuming this directory exists) and places them in
a file named filename.bz2.

Gzip and Gunzip


To use gzip to compress a file, enter the following command at a shell prompt:

gzip filename

The file is compressed and saved as filename.gz.


To expand the compressed file, enter the following command:

gunzip filename.gz

The filename.gz compressed file is deleted and replaced with filename.


You can use gzip to compress multiple files and directories at the same time by
listing them with a space between each one:

gzip -r filename.gz file1 file2 file3 /usr/work/school

The above command compresses file1, file2, file3, and the contents of
the /usr/work/school/ directory (assuming this directory exists) and places them in
a file named filename.gz.

Zip and Unzip


To compress a file with zip, enter the following command:

zip -r filename.zip filesdir

In this example, filename.zip represents the file you are creating


and filesdir represents the directory you want to put in the new zip file. The -
r option specifies that you want to include all files contained in
the filesdir directory recursively.
To extract the contents of a zip file, enter the following command:

unzip filename.zip

You can use zip to compress multiple files and directories at the same time by
listing them with a space between each one:

zip -r filename.zip file1 file2 file3 /usr/work/school

The above command compresses file1, file2, file3, and the contents of
the /usr/work/school/ directory (assuming this directory exists) and places them in
a file named filename.zip.
Archiving Files at the Shell Prompt
A tar file is a collection of several files and/or directories in one file. This is a good
way to create backups and archives.
Some of tar's options include:
 -c — create a new archive
 -f — when used with the -c option, use the filename specified for the creation
of the tar file; when used with the -x option, unarchive the specified file
 -t — show the list of files in the tar file
 -v — show the progress of the files being archived
 -x — extract files from an archive
 -z — compress the tar file with gzip
 -j — compress the tar file with bzip2
To create a tar file, enter:

tar -cvf filename.tar directory/file

In this example, filename.tar represents the file you are creating


and directory/file represents the directory and file you want to put in the archived
file.
You can tar multiple files and directories at the same time by listing them with a
space between each one:

tar -cvf filename.tar /home/mine/work /home/mine/school

The above command places all the files in the work and the school subdirectories
of /home/mine in a new file called filename.tar in the current directory.
To list the contents of a tar file, enter:

tar -tvf filename.tar

To extract the contents of a tar file, enter:

tar -xvf filename.tar

This command does not remove the tar file, but it places copies of its unarchived
contents in the current working directory, preserving any directory structure that the
archive file used. For example, if the tarfile contains a file called bar.txt within a
directory called foo/, then extracting the archive file results in the creation of the
directory foo/ in your current working directory with the file bar.txt inside of it.
Remember, the tar command does not compress the files by default. To create a
tarred and bzipped compressed file, use the -j option:

tar -cjvf filename.tbz file

tar files compressed with bzip2 are conventionally given the extension .tbz;
however, sometimes users archive their files using the tar.bz2 extension.
The above command creates an archive file and then compresses it as the
file filename.tbz. If you uncompress the filename.tbz file with
the bunzip2 command, thefilename.tbz file is removed and replaced
with filename.tar.
You can also expand and unarchive a bzip tar file in one command:

tar -xjvf filename.tbz

To create a tarred and gzipped compressed file, use the -z option:

tar -czvf filename.tgz file

tar files compressed with gzip are conventionally given the extension .tgz.
This command creates the archive file filename.tar and compresses it as the
file filename.tgz. (The file filename.tar is not saved.) If you uncompress
the filename.tgz file with the gunzip command, the filename.tgz file is removed
and replaced with filename.tar.
You can expand a gzip tar file in one command:

tar -xzvf filename.tgz

What are different Run levels in Linux and how to switch between them?
Run levels:-Run levels define what processes or services to run automatically while
the system boots up. This is defined in /etc/inittab file.

The init process is the last step in the boot procedure and has pid of ‘1’.’ “init” is
responsible for starting system processes as per defined in the /etc/inittab file.

“init” process checks which default run level is defined in /etc/inittab and starts the
system in that run level which means all the services defined for that run level gets
executed.

There are 7 different run levels present (run level 0-6) in Linux system for different
purpose. The descriptions are given below.
0: Halt System (To shutdown the system)
1: Single user mode
2: Basic multi user mode without NFS
3: Full multi user mode (text based)
4: unused
5: Multi user mode with Graphical User Interface
6: Reboot System

Most desktop Linux distributions boot into run level 5, which starts up the Graphical
Login Prompt. This allows the user to use the system with X-Windows server
enabled. Most servers boot into run level 3, which starts the text based login prompt
as it is advisable not to install graphical windows in a server as lots of space goes
waste and also it takes lot of resource to run.

Each run level is defined inside its own directory structure. These directories are
located in the /etc/rc.d/ directory, under which you have rc1.d, rc2.d… rc6.d
directories where the number from 0 to 6 corresponds to the specific run level. Inside
each directory symbolic links are defined to a to master initscripts found in /etc/init.d
or /etc/rc.d/init.d.

Switching or Changing between different runlevels:-

Method-1: Changing run level temporarily without reboot.


We can use init command to change rune levels without rebooting the system.
Ex:-if we are currently in run level 3 and want to go to run level 1, just we need to
execute
# init 1
Or if you want to shutdown a machine you can take help of run level ‘0’ .Just you
need to execute
#init 0
Remember this change is not permanent and on next reboot you will get your default
runlevel.
Method-2: Changing run level permanently
If you want to change your default run level then
Open the file /etc/inittab and edit entry initdefault:
# vi /etc/inittab
Let’s set initdefault to 5, so that you can boot to X next time when Linux comes up:
id:5:initdefault:
Method-3:-Change run level at boot time
You can also change the run level at boot time. If your system uses LILO as the boot
manager, you can append the run level to the boot command:
LILO: linux 3 or
LILO: linux 5

If your system uses GRUB, you can change the boot runlevel by pressing the `e’ key
to edit the boot configuration. Append the run level (in our case 5) to the end of the
boot command as shown:
kernel /vmlinuz-2.6.18-164.el5 ro root=LABEL=/ rhgb quiet 5

You might also like