Linux Interview Questions

Updated: Jul 26, 2020



1) What is Linux?

Linux is an operating system, which is based on Linux Kernel. It is an open-source operating system where it can run on different hardware platforms. It provides a free and low-cost operating system for users. It is a user-friendly environment where they can easily modify and create variations in the source code.


2)What is Linux Kernel?

The Linux kernel is the main component of a Linux operating system (OS) and is the core interface between a computer’s hardware and its processes. It communicates between the 2, managing resources as efficiently as possible.

What the kernel does

The kernel has 4 jobs:

Memory management: Keep track of how much memory is used to store what, and where

Process management: Determine which processes can use the central processing unit (CPU), when, and for how long

Device drivers: Act as mediator/interpreter between the hardware and processes

System calls and security: Receive requests for service from the processes.


3) What is Shell?

Shell is a computer program which acts as an interface between the user and the kernel. Users can communicate with the kernel by writing programs, commands and scripts on the shell. It accepts human-readable commands and converts them into the kernel understandable language.


4)What are the basic components of Linux?

Basic components of Linux

  1. Kernel: It is the core component of the Linux, it acts as an interface between software and hardware.

  2. Shell: It acts as an interface between the user and the Kernel.

  3. GUI: It stands for Graphic User Interface, which is another way for the user to interact with the system. But it is unlike images, buttons, text boxes for interaction.

  4. System Utilities: These are the software functions that allow users to manage the computer.

  5. Application Programs: Set of functions designed to perform a set of tasks.

5) Differentiate between LINUX and UNIX?



6) How can you determine the total memory used by LINUX?

It is always required to keep a check on the memory usage in order to find out whether the user is able to access the server or the resources adequately. There are roughly 5 methods that determine the total memory used by Linux.

This is explained as below:

  • Free command: This is the most simple command to check memory usage. For Example, ‘$ free –m’, the option ‘m' displays all the data in MBs.

  • /proc/meminfo: The next way to determine memory usage is to read /proc/meminfo file. For Example, ‘$ cat /proc/meminfo’

  • Vmstat: This command basically lays out the memory usage statistics. For Example, ‘$ vmstat –s’

  • Top command: This command determines the total memory usage as well as also monitors the RAM usage.

  • Htop: This command also displays memory usage along with other details.

7) What are the default ports used for SMTP, DNS, FTP, DHCP, SSH, and squid?

Details mentioned below

Service Port

Telnet 23

SMTP 25

DNS 53

FTP 20(Data Transfer) 21(Connections Established)

DHCP 68(dhcp client), 67(DHCP server)

SSH 22

Squid 3128


8)Can you draw the Linux architecture?

9) what happened when system starts?

it executes four script

a) setting variable

1-->/etc/profile

2-->/etc/bashrc

b) setting path

1--> ~/.bash_profile

2-->~/.bashrc

10) In how many ways shell script execute?

a) it will create child process

1-->sh

2-->./

b)it will not creating child

1) .

2) source


11) Sed (stream editor) related interview questions ?

How to get the list of files alone in a directory in Linux?

ls -lrt | grep ^-


How to get the list of directory alone in Linux?

ls -lrt| grep "^d"


How to get list of hidden directory ?

ls -d .*/

How to list the only linked file ?

ls -lrt| grep ^l


How to delete blank Line from a file using sed command?

sed '/^$/d' filename


How to delete Starting from 3rd line and every 2nd line from there from sed command?

sed '3~2d' filename


How to delete the line which matches the given pattern from input?

sed /Sysadmin/d filename


If 3rd line matches the pattern “Hardware”, so it prints from 3rd line to 6th line.

sed -n '/Hardware/,6p' filename


How to deletes the line from which matches the given pattern and 2 lines next to that?

sed '/Storage/,+2d' filename


If 4th line matches the pattern “Security”, so it prints from 3rd line to 4th line.

sed -n '3,/Security/p' filename

How to print from the line matches the given pattern to end of file?

sed -n ‘/PATTERN/,$p’ filename

How to print the lines which matches the pattern and next N lines following the matched line?

sed -n '/Storage/,+2p' filename


How to Print the section of file between two regular expression (including the matched line )?

sed -n ‘/P1/,/P2/p’ filename


How to substitute Only 2nd Occurrence of a Word Using sed ?

sed 's/Linux/Linux-Unix/2' filename

How to write Changes to a File and Print the Changes Using sed ?

sed -n 's/Linux/Linux-Unix/gpw ' filename


12)What is INODE ?

· An inode is a data structure on a filesystem on Linux and other Unix-like operating systems that stores all the information about a file except its name and its actual data. A data structure is a way of storing data so that it can be used efficiently.

· An Inode number points to an Inode. An Inode is a data structure that stores the following information about a file :

  • · Inode number

  • Access control list

  • · Extended attribute

  • · Direct/indirect disk blocks

  • · Number of blocks

  • · File access, change and modification time

  • · File deletion time

  • · File generation number

  • · File size

  • · File type

  • · Group

  • · Number of links

  • · Owner

  • · Permissions

  • · Status flags

To get a listing of an inode number, use the following command:

Code:

$ ls –i filename

You can use the “stat” command to get more information than the inode number:

Code:

$ stat filename


13) what are SOFT LINKS AND HARD LINKS ?

A link in UNIX is a pointer to a file. Like pointers in any programming languages, links

in UNIX are pointers pointing to a file or a directory . Creating links is a kind of

shortcuts to access a file. The two different types of links in UNIX are:

Soft Links or Symbolic Links

Hard links


Soft Links:

Soft Links can be created across file systems.

Soft link has a different inode number than the original file.

On deleting the original file, soft link cannot be accessed.

Soft link needs extra memory to store the original file name as its data.

Source file need not exist for soft link creation.

Can be created on a file or on a directory.

Access to the file is slower due to the overhead to access file.

Hard Links:

Hard links can be created only within the file system.

Hard links have the same inode number as the original file.

On deleting the original file, hard linked file can still be accessed.

Hard links do not need any extra data memory to save since it uses links

Source file should exist.

Can be created only on files, not on directories.

Access to the file is faster compared to soft link.

14) grep related interview questions ?

  • Checking for full words, not for sub-strings using grep -w

grep -iw "is" filename

  • Display N lines after match

grep -A 3 -i "example" filename

  • Display N lines before match

grep -B 2 "single WORD" filename

  • Display N lines around match

grep -C 2 "Example" filename

  • Searching in all files recursively using grep

grep -r "ramesh" *

  • Counting the number of matches using grep

grep -c "go" demo_text

  • Display only the file names which matches the given pattern using grep -l

grep -l this filename*


  • Show line number while displaying the output using grep -n

grep -n "go" filename

  • Count of empty lines ( ^$ )

grep -c "^$" filename

  • how to match more than one pattern

grep -e "a" -e "b" -e "c" filename

  • Count the empty line in a file

grep -c '^$' filename

15)grep and egrep differences ?

Grep

Unlike egrep in grep, ?, +, {, |, (, and ), are rather treated as pattern instead of meta characters. By putting ‘backslash’ in front of meta character can let the grep to treat it as meta characters like \?, \+, \{, \|, \(, and \).

ls | grep '.txt|.py'

Now just consider the above example, in this command we are listing down all the files in the present working directory. Using pipe we are giving the output of ls command to grep. The grep command will check if there is any file with .text|.py extension.

egrep

Now let’s see egrep side of the things. egrep is efficient and fast when it comes to searching for a regular expression pattern in large text files as it treats meta-characters as is and doesn’t substitute them as strings like in grep, and hence you are freed from the burden of escaping them exclusively as in grep.

ls | egrep '.txt|.py'

Now consider again the same example with egrep, here egrep will look for files with either .txt or .py extension. By using egrep we can even search for multiple pattern, files at a time using one command. We can make grep also do the same by escaping the characters but that’s an overhead for a power user since Linux command-line is all about reducing the amount of work to be done.

16) Linux file tree or file structure?

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.