Thursday, 2 May 2024

10 Highly Important Linux Interview Questions And Answers

 

1. What are the different layers in a Linux operating system?

The layers present in a Linux operating system are:

  • Hardware: The innermost layer consists of physical devices like RAM, CPU, etc. There may be driver software to communicate with devices in some OS.
  • Kernel: Kernel is the heart of an OS which hides the complexities of the underlying hardware and provides a high-level abstraction to upper layers. There are different types of kernels: microkernel, monolithic kernel, etc. The Linux kernel is of monolithic type.
  • Shell: The shell is a program that runs on top of the kernel. It acts as the primary method of interaction between the user and the kernel. In other words, it is a program that can run other programs. The shell accepts commands and passes it for execution. Nowadays, GUI has replaced the shell to a large extent.
  • Utility Programs (utilities): These are programs or software that run on top of the operating system. They help users for day-to-day generic activities like scheduling a cron job or for specific tasks like creating text documents.

2. What are the three popular Linux shells?

  • Bash Shell: It is the default shell in many Linux/Unix distributions. Bash has features like:
    • Edit command history.
    • Shell functions and gives aliases to it.
    • Unlimited command history.
    • Array with unlimited size.
  • Tcsh/Csh Shell (also known as C shell): Tcsh is an enhanced C shell. It has the following features:
    • More of C like syntax.
    • Auto-completion of words and filename is programmable.
    • Spell check.
    • Job control.
  • K Shell: The K shell is known as Korn shell or Ksh. Much more than an interactive shell, the K shell is a complete, powerful, high-level programming language. Some features of the K shell are:
    • Options and variables that give you more ways to customize your environment.
    • Advanced security features.
    • Advanced regular expressions and well-known utilities like grep and awk.

3. What are Manual Pages?

Manual pages hold an explanation of every available command. The manual page for a specific command contains the entire information about that command.

How do you invoke the manual pages command? “Man ls” is one way of calling the command. This command shows a detailed explanation of the “ls” command. Manual pages are categorized into different categories: user commands, system calls, library functions, etc.

The general layout of a manual page is:

  • NAME: The name of the command or function and a brief explanation about it.
  • SYNOPSIS: Tells how to run the command and the parameters it takes. For functions, a list of the parameters it takes and the name of the header file which contains its definition.
  • DESCRIPTION: A detailed description of the command or function we are searching for.
  • EXAMPLES: Some examples of the command’s usages.
  • SEE ALSO: This section contains a list of related commands or functions.

4. How do you retrieve the list of currently running processes and resource utilization in Linux?

This is one of the basic Linux interview questions. Below is the process and resource utilization in Linux are as follows:

The “top” command is used for this requirement. It gives information about each process running on the machine like:

  • Process ID (PID).
  • Owner of the process (USER).
  • The priority of the process (PR).
  • Percentage of CPU (%CPU).
  • Percentage of memory.
  • Total CPU time spent on the process.
  • Command used to start a process.

Commonly used parameters employed with the top command are:

  • top -u -> Process by a user.
  • top -i -> Exclude idle tasks.
  • top -p -> Shows a particular process.

5. What is the pipeline operator in Linux?

The pipeline operator in Linux is used to redirect the output of one program or command to another program/command for further processing. It is usually termed as redirection. Vertical bars, ’|’ (“pipes” in common Unix verbiage), are used for this. For example, the ls -l | grep key, will redirect the output of the ls -l command to the grep key command.

1000+ Question on Linux Operating System

6. What is file permission in Linux?

Permissions are established for all files and directories. Permissions specify who can access a file or directory along with the type of access. All files and directories are owned by a user. Permissions are controlled at three levels:

  • Owner level (user, or ‘u’).
  • Group level (‘g’).
  • Other users (‘o’).

There are three levels of access:

  • Read: The file can only be viewed or copied.
  • Write: The file can be overwritten.
  • Execute: The file can be executed.

To change the permission, chmod < file(s)> is used. The parameter file(s) is one or more files (or directories). One approach to specify permissions is to describe the changes to be applied as a combination of u, g, and o along with r, w, and x. To add permission, use + and to remove permission, use .

7. What is a process in Linux?

A process is a running program. Processes can be started from the GUI or from the command line. Processes can also start other processes. Whenever a process runs, Linux keeps track of it through a process ID (PID). After booting, the first process is an initialization process called init. It is given a PID of 1. From that point on, each new process gets the next available PID.

A process can only be created by another process. We refer to the creating process as the parent and the created process as the child. The parent process spawns one or more child processes. The spawning of a process can be accomplished in several ways. Each requires a system call (function call) to the Linux kernel. These function calls are fork(), vfork(), clone(), wait(), and exec().

Read more about Linux Operating System at Wikipedia.

8. What are regular expressions (regex)?

A regular expression (regex) is a string that expresses a pattern used to match against other strings. The pattern will either match some portion of another string or won’t match at all. There is a list of predefined metacharacters used in a regex:

  • *  (asterisk) is used to match the preceding character if it appears 0 or more times.
  • + (plus) is used to match the preceding character if it appears 1 or more times.
  • ?  (question mark) is used to match the preceding character if it appears 0 or 1 time.

9. What is the sed command used for?

Commonly asked Linux interview questions and answers are incomplete without sed. Sed is a stream editor. A stream editor is a program that takes a stream of text and modifies it. With sed, you specify a regular expression that represents a pattern of what you want to replace. The generic form of the sed command is sed ‘s/pattern/replacement/’ filename.

10. What is the difference between a hard link and a soft link?

A soft link (also known as a symbolic link) points to another file by name. Since it contains only a name, that name does not actually have to exist or exist on a different file system. If you replace the file or change the file content without changing its name, the soft link still contains the same name and points to that file. A hard link points to the file by inode number. The file should actually exist in the same file system. A file will only be deleted from disk when the last link to its inode is removed.

20 Linux Interview Questions and Answers

Q:1 How to check current run level of a Linux server ?
Ans: ‘who -r’ & ‘runlevel’ commands are used to check the current runlevel of a linux box.

Q:2 How to check the default gateway in Linux ?
Ans: Using the commands like “ip route show”, “route -n” and “netstat -nr” we can check default gateway. Apart from the default gateway info , these commands also display the current routing tables .

Q:3 How to rebuild initrd image file on Linux ?
Ans: In case of CentOS 5.X / RHEL 5.X , mkinitrd command is used to create initrd file , example is shown below :

# mkinitrd -f -v /boot/initrd-$(uname -r).img $(uname -r)

If you want to create initrd for a specific kernel version , then replace ‘uname -r’ with desired kernel

In Case of CentOS 6.X / RHEL 6.X , dracut command is used to create initrd file example is shown below :

# dracut -f

Above command will create the initrd file for the current version. To rebuild the initrd file for a specific kernel , use below command :

# dracut -f initramfs-2.x.xx-xx.el6.x86_64.img 2.x.xx-xx.el6.x86_64

Q:4 What is cpio command ?
Ans: cpio stands for Copy in and copy out. Cpio copies files, lists and extract files to and from a archive ( or a single file).

Q:5 What is patch command and where to use it ?
Ans: As the name suggest patch command is used to apply changes ( or patches) to the text file. Patch command generally accept output from the diff and convert older version of files into newer versions. For example Linux kernel source code consists of number of files with millions of lines , so whenever any contributor contribute the changes , then he/she will be send the only changes instead of sending the whole source code. Then the receiver will apply the changes with patch command to its original source code.

Create a diff file for use with patch,

# diff -Naur old_file new_file > diff_file

Where old_file and new_file are either single files or directories containing files. The r option supports recursion of a directory tree.

Once the diff file has been created, we can apply it to patch the old file into the new file:

# patch < diff_file

Q:6 What is use of aspell ?
Ans: As the name suggest aspell is an interactive spelling checker in linux operating system. The aspell command is the successor to an earlier program named ispell, and can be used, for the most part, as a drop-in replacement. While the aspell program is mostly used by other programs that require spell-checking capability, it can also be used very effectively as a stand-alone tool from the command line.

Q:7 How to check the SPF record of domain from command line ?
Ans: We can check SPF record of a domain using dig command. Example is shown below :

linuxtechi@localhost:~$ dig -t TXT google.com

Q:8 How to identify which package the specified file (/etc/fstab) is associated with in linux ?
Ans: # rpm -qf /etc/fstab

Above command will list the package which provides file “/etc/fstab”

Q:9 Which command is used to check the status of bond0 ?
Ans: cat /proc/net/bonding/bond0

Q:10 What is the use of /proc file system in linux ?
Ans: The /proc file system is a RAM based file system which maintains information about the current state of the running kernel including details on CPU, memory, partitioning, interrupts, I/O addresses, DMA channels, and running processes. This file system is represented by various files which do not actually store the information, they point to the information in the memory. The /proc file system is maintained automatically by the system.

Q:11 How to find files larger than 10MB in size in /usr directory ?
Ans: # find /usr -size +10M -exec ls -lah {} \;

Q:12 How to find files in the /home directory which were modified more than 120 days ago ?
Ans: # find /home -mtime +120

Q:13 How to find files in the /var directory that have not been accessed in the last 90 days ?
Ans: # find /var -atime -90

Q:14 Search for core files in the entire directory tree and delete them as found without prompting for confirmation
Ans: # find / -name core -exec rm {} \;

Q:15 What is the purpose of strings command ?
Ans: The strings command is used to extract and display the legible contents of a non-text file.

Q:16 What is the use tee filter ?
Ans: The tee filter is used to send an output to more than one destination. It can send one copy of the output to a file and another to the screen (or some other program) if used with pipe.

linuxtechi@localhost:~$ ll /etc | nl | tee /tmp/ll.out

In the above example, the output from ll is numbered and captured in /tmp/ll.out file. The output is also displayed on the screen.

Q:17 What would the command export PS1 = ”$LOGNAME@`hostname`:\$PWD: do ?
Ans: The export command provided will change the login prompt to display username, hostname, and the current working directory.

Q:18 What would the command ll | awk ‘{print $3,”owns”,$9}’ do ?
Ans: The ll command provided will display file names and their owners.

Q:19 What is the use of at command in linux ?
Ans: The at command is used to schedule a one-time execution of a program in the future. All submitted jobs are spooled in the /var/spool/at directory and executed by the atd daemon when the scheduled time arrives.

Q:20 What is the role of lspci command in linux ?
Ans: The lspci command displays information about PCI buses and the devices attached to your system. Specify -v, -vv, or -vvv for detailed output. With the -m option, the command produces more legible output.