LINUX Interview Questions

Prepare better with the best interview questions and answers, and walk away with top interview tips. These interview questions and answers will boost your core interview skills and help you perform better. Be smarter with every interview.

  • 4.6 Rating
  • 30 Question(s)
  • 30 Mins of Read
  • 9874 Reader(s)

Beginner

The linux directory commands are :

1) pwd: It is a built-in command which stands for ‘print working directory’. It displays the current working location, working path starting with a / and directory of the user. Basically, it displays the full path to the directory you are currently in.

2) Is: This command lists out all the files in the directed folder.

3) cd: This stands for ‘change directory’. This command is used to change to the directory you want to work from the present directory. We just need to type cd followed by the directory name to access that particular directory.

4) mkdir: This command is used to create an entirely new directory.

5) rmdir: This command is used to remove a directory from the system.

The Linux Kernel is a low-level systems software whose main role is to manage hardware resources for the user. It is also used to provide an interface for user-level interaction.

There are so many Linux distributions  but the one thing that they have in common is the Linux kernel. Each operating system uses a kernel. Without a kernel, we can’t have an operating system that actually works. The kernel's main responsibility is to talk to the hardware and software, and to manage the system’s resources as best as possible. It talks with the hardware by means of the drivers that are included in the kernel. For example, when an application needs to accomplish something (say change the brightness of the display), it can simply submit the request to the kernel, and the kernel can utilize the driver it has for the display to change its brightness.

The kernel is highly involved in resource management. It has to ensure that there is sufficient memory available for an application to run, as well as to load an application in the correct location in memory. It tries to optimize the usage of the processor so that it can complete tasks as quickly as possible. It also aims to avoid deadlocks, which are problems that completely halt the system when one application needs a resource that another application is using.

A  linux  process can be in any one of the  following process states:

  • Running: Process is either running or ready to run.
  • Interruptible: a Blocked state of a process and waiting for an event or signal from another process.
  • Uninterruptible: a blocked state. Process waits for a hardware condition and cannot handle any signal.
  • Stopped: Process is stopped or halted and can be restarted by some other process.
  • Zombie: process terminated, but information is still there in the process table.

The cat (short for “concatenate“)  command allows us to create single or multiple files, view content of  file, concatenate files and redirect output in terminal or files.

1. Display contents of a file.

In this example , it will show the contents of the file debug.log

2. View contents of multiple files.

In this example , it will show contents of multiple files.

3. Create a file with cat command

In this example we create a text file using the cat >filename

Command.(Press ctrl + D to exit)

4. Use standard output with redirection operator.

We can redirect standard output of a file into a new file or

existing file  with ‘>‘ (greater than) symbol but the contents

of the existing file will be replaced.

The ps (i.e., process status) command is used to provide information about the currently running processes, including their process identification numbers(PIDs).

A process, also referred to as a task, is an executing (i.e., running) instance of a program. Every process is assigned a unique PID by the system.The basic syntax of ps is -  ps [options]

netstat(short for network statistics) is a command-line network utility which we can use to check the network ports in use and their incoming connections.
Netstat basically checks the network status.
It can  also display network connections for the transmission control protocol, routing tables, and a number of network interface and network protocol statistics.

It is used for finding problems in the network and to determine the amount of traffic on the network as a performance measurement.

Linux operating system basically consists of 3 components which are as follows:

  • Kernel: This is considered as the core part and is responsible for all major activities of Linux operating system. Linux Kernel is considered as free and open source software which is capable of managing hardware resources for the users. It consists of various modules and interacts directly with the underlying hardware.
  • System Library: Most of the functionalities of the operating system are implemented by System Libraries. These act as a special function using which application programs accesses Kernel’s features.
  • System Utility: These programs are responsible for performing specialized, individual level tasks.

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 are adequate. There are basically 5 methods that determine the total memory used by the Linux.

This is explained as below

  • free command: This is the most simple and easy to use the 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 the 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 the memory usage along with other details.

Every file and directory in Linux are assigned three types of owners namely ‘User’, ‘Group’, and ‘Others’. The three types of permissions defined for all the three owners are :

  • Read: This permission allows you to open and read the file as well as list the contents of the directory.
  • Write: This permission allows you to modify the contents of the file as well as allows adding, removing and renaming files stored in the directories.
  • Execute: User can access and run the file in the directory. You cannot run a file unless the execute permission is set.

Process management in linux uses  certain system calls.The system calls are as follows :

  1. fork() : This system call is used to create a new process.
  2. exec() : This system call is used for execution of a new process.
  3. wait() :This system call is used to make the process wait.
  4. exit() : This system call is used to terminate a process.
  5. getpid() : This system call is used to get the unique process id.
  6. getppid() : This system call is used to find the parents process id.
  7. nice() : nice is used to invoke a utility or a shell script with a particular priority, thus giving the process more or less CPU time than other processes.

For executing any commands user uses a program known as the shell. Linux shell is basically a user interface used for executing the commands and communicating with Linux operating system. Shell does not use the kernel to execute certain programs, create files, etc. There are several shells available with Linux which includes the following

  • BASH (Bourne Again Shell)
  • CSH ( C Shell)
  • KSH ( Korn Shell)
  • TCSH

There are basically two types of Shell commands

  • Built-in shell commands: These commands are called from the shell and executed directly within the shell. Examples: ‘pwd’, ’help’,  etc.
  • External/ Linux commands: These commands are totally shell independent, have their own binary and are located in the file system.

To change permissions in linux , we need to understand the concept of binary assignment.

Each permission within a permission group is assigned a binary representation.

Read - 4 ( 100 )

Write - 2 (010)

Execute - 1 (001)

For example, if we wanted to give the owner ‘rwx’  , owner’s group ‘r-x’ and others ‘r--’ permissions will be summed up to give the permission i.e  for owner- r + w + x = 4 + 2 + 1 = 7 , for owner’s group r + x = 4+1=5 , and for others = r = 2.

So, the permission will be “752”.

Now coming to the question  , an admin or an owner can change the permissions of a file using the “chmod” command line utility.

Syntax of the chmod command is of the form:

chmod <file-permission> file-name.

So for bringing the permissions of the above example into effect ,we can use the command chmod 752 file.txt

The wc (word count) command is used to find out the count of

number of characters in a file.

Apart from count of characters wc command can also be used to find out the newline count , byte and character count in files Specified by file arguments.

The syntax of the wc command is of the form:

wc [options] filename

The following are the options and usage provided by the command:

wc -l : prints the number of lines in a file.

wc -w : prints the number of words in a file.

wc -c : prints the number of bytes in a file.

wc -m : prints the number of count of characters in a file.

wc -L : prints only the length of the longest line in a file.

So the answer is the command  wc -m file.txt

The Linux “du” (short for disk usage) is a standard Linux command, used to check the information of disk usage of files and directories on a machine. The du command has many parameter options that can be used to get the results in many formats. The du command also displays the files and directory sizes in a recursively manner.

The  & (ampersand) should be appended to a command to run a process in the background.

Contents of file.

Sorted file.

We can make use of the sort command found in linux to arrange.The records of a file in a particular order.

By default, the sort command sorts file assuming the contents are ASCII. Using options in sort command, it can also be used to sort numerically.

  • SORT command sorts the contents of a text file, line by line.
  • sort is a standard command line program that prints the lines of its input or concatenation of all files listed in its argument list in sorted order.
  • The sort command is a command line utility for sorting lines of text files. It supports sorting alphabetically, in reverse order, by number, by month and can also remove duplicates.
  • The sort command can also sort by items not at the beginning of the line, ignore case sensitivity and return whether a file is sorted or not. Sorting is done based on one or more sort keys extracted from each line of input.
  • By default, the entire input is taken as sort key. Blank space is the default field separator.

It is well known that every command takes an input and displays an output. Keyboard serves as the standard input device and screen serves as the standard output device. Redirection is defined as the process of directing data from one output to another or even cases exist where output serves as an input data for another process.

There are basically three streams available in which input and output of Linux environment are distributed.

These are explained as below:

  • Input Redirection: ‘<’ symbol is used for input redirection and is numbered as (0). Thus it is denoted as STDIN(0).
  • Output Redirection: ‘>’ symbol is used for output redirection and is numbered as (1). Thus it is denoted as STDOUT(1).
  • Error Redirection: It is denoted as STDERR(2)

Yes , it is absolutely legal to edit Linux Kernel. Kernel is released under General Public License(GPL) , and anyone can edit Linux Kernel to the extent permitted under GPL. Linux Kernel comes under the category of Free and Open Source Software(FOSS).

  1. The main difference is that both UNIX and Linux are two different operating systems although they share some common commands.
  2. The source code of linux is available freely to its users while the source code of unix is not available for the general public.
  3. Linux OS is portable and can be executed in different hard drive while unix is not.
  4. Different versions of Linux are Ubuntu , Linux Mint , Redhat, Solaris While the different versions of UNIX are AIS , HP-UX,BSD,Iris etc.
  5. Linux is developed by an active Linux community worldwide while UNIX is developed by AT&T developers.

‘init’ is the first process in linux which is started by the kernel and its process id is 1.

Advanced

grep -c “error” debug.log

The grep command which stands for globally search for regular expression and print out  , searches a file for a particular pattern of characters and displays all lines that matches that pattern.

The pattern that is searched in the file is referred to as regular expression.

Syntax of a grep command is of the form : grep [options] pattern [files]

In the above command the -c options description prints the count of lines in the file debug.log that match the pattern “error”.

Although if we would want to ignore case for matching i.e consider both the words “error” and “ERROR” we need to use the following command:

grep -c -i “error” debug.log , where -i is an option that ignores case for matching.

The six stages of the linux boot process are:

  • BIOS
  • MBR
  • GRUB
  • Kernel
  • Init
  • Runlevel
  1. BIOS - BIOS stands for Basic input/output system. It performs system integrity checks and searches for the boot loader program in CD-rom or hard drive to load and execute it.Once the boot loader program is detected and loaded into memory BIOS gives control to it.So , in short BIOS loads and executes the MBR(Master Boot Record) bootloader.
  1. MBR - MBR stands for Master Boot Record. It is located in the first sector of the bootable disk. Typically in dev/hda, or dev/sda. It contains information about GRUB(Grand unified Bootloader). So , in short MBR loads and executes the GRUB boot loader.     
  1. GRUB - GRUB stands for Grand Unified Loader. It loads the default kernel image as specified in the grub configuration file. The grub configuration file is located at /boot/grub/grub.conf. It basically contains the kernel and initrd(initial ram disk - a scheme for loading a temporary root file system into memory) image.So , in short GRUB loads and executes kernel and initrd images.
  1. Kernel - Kernel mounts the root file system and executes the /sbin/init program. As init is the first program to be executedby the kernel it has a process id of 1. We can do a ps -ef | grep init to verify.
  1. Init - Init looks at the /etc/inittab file to decide the linux run level.The run levels available in linux are : 0 -> halt , 1 -> Single user mode , 2 -> Multiuser , 3 -> Full multiuser mode , 4 -> Unused ,5 -> X11 , 6 ->reboot
  1. Runlevel Programs - When the linux system in booting up , we can see various services booting up.

These are the runlevel programs, executed from the run level directory as defined by the run level.

We use the command head -n5 sample.txt to display the first five lines of the file and the command tail -n5 sample.txt to display the last five lines of the file.



The idea is to make use of the head and tail commands to get the required results.

The head command reads the first ten lines of any given file.

The basic syntax of the head command is:
head [options] [file(s)]

If we need to print more or less than the default ten number of lines from the beginning of a file we need to use the ‘-n’ option along with an integer telling the number of lines to be retrieved.

Similarly  the tail command displays the last ten lines of any given file.
The basic syntax of the tail command is:

tail[options] [file(s)]

If we need to print more or less than the default ten number of lines from the end of a file we need to use the ‘-n’ option along with an integer telling the number of lines to be retrieved.

LILO stands for Linux Loader that is used to load Linux into memory. It can boot operating systems from floppy disks, hard disks, and it does not depend on a specific file system. Lilo handles some tasks such as locate the kernel, identify other supporting programs, load memory and starts the kernel. The configuration file of lilo is located at “/etc/lilo.conf”. Lilo reads this configuration file and it tells Lilo where it should place the bootloader.
Boot process

When LILO loads itself, it displays the name LIO where each word specifies some actions

  1. If it displays nothing then it does not load any part of LILO.
  2. L: This is the first stage of the bootloader that has been loaded. If the process stops here it denotes that there were problems in the second stage. This may occur due to some incorrect disk parameter specified in the configuration file of lilo or some media problems also.
  3. LI: It indicates that the second stage boot loader has been loaded and could not be executed. It can occur due to problems similar to L.
  4. LIL: At this stage, the second stage boot loader has been completed in its execution. If it fails, this stage indicates that there were media problems or map file specified in the configuration file has some problems.
  5. LIL?: This means that the second stage boot loader loaded at an incorrect address.
  6. LIL-: This indicates that the descriptor table is corrupted.
  7. LILO: All parts are successfully loaded.

Swap space is a type of memory used by linux to increase the amount of virtual memory available to the host.
Virtual memory is the memory that appears to exist as main storage although most of it is supported by data held in secondary storage.

It can use one or more dedicated swap partitions or a swap file on a regular filesystem  or logical volume.
The primary function of swap space is to substitute disk space for RAM memory when the RAM is filled up and more space is required.

Let’s say ,  we have a computer with 4GB of RAM. If we run programs that don’t fill up the RAM , they will run and no swapping will be required. But , let’s say that the spreadsheet we are working on grows when we add more rows , and plus everything else that’s running , now fills all of RAM. Without swap space available we would have to stop working on the spreadsheet until we free up some RAM by closing down some other programs.

The kernel uses a memory management program that detects Pages of memory in which the contents have not been used recently.The memory management program swaps enough of these relatively infrequently used pages of memory out to a special partition on the hard drive specifically designed for paging , or swapping. This frees up RAM and makes room for more data to be entered into the spreadsheet.Those pages of memory swapped out to the hard drive are tracked by the memory management code of the kernel and can be paged back into RAM if needed.

Linux keeps user account information in a text file called /etc/passwd. This file also stores one way encrypted password. It is accessed by several tools to get user information, which is a security risk, so file needs to be ‘Word Readable.’ To minimize the security risk, we can use shadow password format. This method saves account information in regular file /etc/passwd.

However, the password is stored as a single “x” character (not actually stored in this file). A second file, called “/etc/shadow” contains encrypted password as well as other information such as account or password expiration values, etc. The /etc/shadow file is readable only by the root account and is therefore less of a security risk.

Yes, a Linux machine can be made a router. This is called “IP Masquerade.” IP Masquerade is a networking function in Linux similar to the one-to-many (1: Many) NAT (Network Address Translation) servers found in many commercial firewalls and network routers.

The IP Masquerade feature allows other “internal” computers connected to this Linux box (via PPP, Ethernet, etc.) to also reach the Internet as well. Linux IP Masquerading allows this functionality even if the internal computers do not have IP addresses.

The IP masquerading can be done by the following steps :

  • The Linux PC must have an internet connection and a connection to LAN. Typically, the Linux PC has two network interfaces-an Ethernet card for the LAN and a dial-up PPP connection to the Internet (through an ISP).
  • All other systems on your LAN use the Linux PC as the default gateway for TCP/IP networking. Use the same ISP-provided DNS addresses on all systems.
  • Enable IP forwarding in the kernel. By default the IP forwarding is not enabled. To ensure that IP forwarding is enabled when you reboot your system, place this command in the /etc/rc.d/rc.local file.
  • Run /sbin/iptables-the IP packet filter administration program-to set up the rules that enable the Linux PC to masquerade for your LAN.

The list of modes used in Network Bonding are :

  • balance-rr or 0 – round-robin mode for fault tolerance and load balancing.
  • active-backup or 1 – Sets active-backup mode for fault tolerance.
  • balance-xor or 2 – Sets an XOR (exclusive-or) mode for fault tolerance and load balancing.
  • broadcast or 3 – Sets a broadcast mode for fault tolerance. All transmissions are sent on all slave interfaces.
  • 802.3ad or 4 – Sets an IEEE 802.3ad dynamic link aggregation mode. Creates aggregation groups that share the same speed & duplex settings.
  • balance-tlb or 5 – Sets a Transmit Load Balancing (TLB) mode for fault tolerance & load balancing.
  • balance-alb or 6 – Sets an Active Load Balancing (ALB) mode for fault tolerance & load balancing.

It is quite common to access servers using ssh. So the following steps can be taken to secure the SSH service, such as:

  • Disabling root login, and even password-based logins will further reinforce the security of the server.
  • Disabling password-based logins and allowing key based logins which are secured but can be taken further by restricting their use from only certain IP addresses.
  • Changing the standard port to something other significantly decreases random brute force attempts from the internet.
  • Forcing the service to use only version 2 of the protocol will introduce both security and feature enhancement.
  • The whitelist approach can be taken, where only the users that belong to a certain list can log in via SSH to the server.

We can use the command :  tail -F $logfile.

where $logfile is an environment variable corresponding to the path to the log file to be monitored.

By default, the Linux tail command prints the last 10 lines of a given file to standard output. The -F(short for --follow) option causes additional file content to be displayed in real time as the file continues to grow.

Description

Prepare better with the best interview questions and answers, and walk away with top interview tips. These interview questions and answers will boost your core interview skills and help you perform better. Be smarter with every interview.
Levels