In this blog post, I have created a list of the most common Linux interview questions. These Linux interview questions not only for fresher but also good for the experienced person. If you want the answer to these Linux questions, then please write in the comment box. If you know the answer to the question, then please write the answer in the comment box. It might be your answer helpful to others. These Linux interview questions, I have created after spending many hours, so I hope these Linux interview questions will be helpful.
What is Linux?
Linux is a family of open-source Unix-like operating systems based on the Linux kernel. Linux was first introduced by Linus Torvalds. The main purpose of Linux was to provide free and low-cost Operating Systems for users who could not afford Operating Systems like Windows or iOS or Unix.
What is the difference between Linux and Unix?
Below I am mentioning some differences between Linux and Unix.
- Linux has both free and paid version but Unix has only paid free version not available in free.
- Linux source code is available to the general public whereas, in Unix, the source code is proprietary.
- Linux user by general user, developer, .etc. But Unix mainly uses in internet server, Workstation server, .etc.
- UNIX OS was created in the late 1960s at AT&T Bell Labs whereas Linux is an operating system built by Linus Torvalds at the University of Helsinki in 1991.
- Linux is a clone of Unix.
- Linux default shell is BASH while the Unix shell is Bourne Shell.
- Linux threat detection and solution are very fast while Unix users require longer wait times to get the proper bug fixing patch.
- Important versions of Linux are Redhat, Ubuntu, OpenSuse, Solaris, whereas important versions of Unix are HP-UX, AIS, BSD, etc.
What are the basic components of Linux?
Below I have mentioned basic components of Linux:
- Kernel: A kernel is the core component of the Operating System that manages operations and hardware.
- Shell: Shell is a Linux interpreter that is used to execute commands.
- GUI: GUI stands for Graphical User Interface which is another way for a user to interact with the system. But unlike CLI, GUI consists of Images, Buttons, TextBoxes for interaction.
- System Utilities: These are the software functions that allow the user to manage the computer.
- Application Programs: Software programs or set of functions designed to accomplish a specific task.
What is Swap Space?
Swap Space is the amount of physical memory that is used by Linux to hold concurrently running programs temporarily. This condition usually occurs, when the RAM does not have enough space to hold the programs. Now processor will look for data in the RAM and the Swap Space. There are different commands and tools available to manage the Swap space usage. You can see the article, the Advantage of the virtual memory. There are different commands and tools available to manage the Swap space usage.
Write the commands to check how much memory is being used by Linux?
Below, we have mentioned the few commands which use to check how much memory is being used by Linux.
top
The top command can display system summary information as well as a list of the process currently being managed by the Linux kernel.
free
The “free” command usually displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel. To see the size of the memory in (MB) megabytes use option as -m.
free -m
htop
The htop command is used to view processes in an interactive mode and also displays information about memory usage.
vmstat
The vmstat command is used to display information about virtual memory statistics.
What are the process states in Linux?
Every process has some states, below we have mentioned process states in broadway with their process state codes.
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. The 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 the information is still there in the process table.
PROCESS STATE CODES:
- D uninterruptible sleep (usually IO)
- R runnable (on run queue)
- S sleeping
- T traced or stopped
- Z a defunct (“zombie”) process
Explain the ‘ls’ command in Linux.
ls is used to list information about files and directories within the file system.
Syntax to use ls:
ls [OPTIONS] [FILES]
Note: When we used ls with no options and arguments then it displays a list of the names of all files in the current working directory.
There are different options that can be used with the ls command, here we have mentioned a few one of them.
- -l lists long format (shows all permissions of the file).
- -ld If you want the details of the directory.
- -i lists files with their inode number.
- -s lists files with their size.
- -a lists all files including hidden files.
- -S lists files with their size and sorts the list by file size.
- -t sorts the listed files by time and date.
- -lh display file size in easy to read format.
How to rename a file in Linux?
Many times you need to rename the file and directory name. Here I am explaining some commands which used to change the name of files and directory. So let us see them,
Renaming files with mv Command:
The mv command is used to rename or move files from one location to another.
Syntax to use mv:
mv [OPTIONS] source destination
If you have a file name aml.txt and you want to change its name with aticleworld.txt. So you need
mv aml.txt aticleworld.txt
Note: Using the mv command you can change the single file name at a time.
Renaming files with rename Command:
rename command is not part of a standard Linux distribution, so you will need to install it. In Ubuntu and Debian-derived distributions you install rename like this:
sudo apt install rename
Syntax to use rename:
rename [options] expression replacement file...
What is BASH?
Bash is the shell, or command language interpreter, for the GNU operating system. The name is an acronym for the ‘Bourne-Again SHell’, a pun on Stephen Bourne, the author of the direct ancestor of the current Unix shell sh, which appeared in the Seventh Edition Bell Labs Research version of Unix.
Bash is largely compatible with sh and incorporates useful features from the Korn shell ksh and the C shell csh. It is intended to be a conformant implementation of the IEEE POSIX Shell and Tools portion of the IEEE POSIX specification (IEEE Standard 1003.1). It offers functional improvements over sh for both interactive and programming use.
While the GNU operating system provides other shells, including a version of csh, Bash is the default shell. Like other GNU software, Bash is quite portable. It currently runs on nearly every version of Unix and a few other operating systems – independently-supported ports exist for MS-DOS, OS/2, and Windows platforms.
What is the basic difference between BASH and DOS?
Bash is a powerful command shell and scripting language developed from the Bourne shell. However, Bash implementations can be found on other operating systems now. Bash supports array variables, shell functions, and very flexible substitution. It is actively developed and new features are added regularly.
BASH and DOS are used to interpret the commands that a user enters. Below we have mentioned some differences between dos and bash.
BASH |
DOS |
Commands written in BASH are case sensitive | DOS commands are not case sensitive. |
In BASH ‘/’ character is a directory separator and ‘\’ is an escape character. | In DOS, ‘/’ is a command argument delimiter and ‘\’ is a directory separator. |
BASH follows no convention in naming files. | DOS follows a convention while naming files. It is 8 characters file name followed by a dot and 3 characters for the extension. |
What is the PWD command?
PWD stands for Print Working Directory. It displays the path of the present working directory, starting from the root.
Syntax of PWD:
pwd [OPTION]...
What are the environmental variables?
Environmental variables are global settings that control the shell’s function as well as that of other Linux programs. Another common term for environmental variables is global shell variables. It can affect the processes ongoing or the programs that are executed in the environment.
Let see an example where I am creating an environment variable and setting a value and later I am accessing its value using the echo command.
$MSG="Love Aticleworld"
$echo $MSG
It produces the following result.
Love Aticleworld
Note: The environment variables are set without using the $ sign but while accessing them we use the $ sign as a prefix. These variables retain their values until we come out of the shell.
Some commonly used ENVs in Linux
$USER: Gives the current user's name. $PATH: Gives search path for commands. $PWD: Gives the path of the present working directory. $HOME: Gives path of the home directory. $HOSTNAME: Gives the name of the host. $LANG: Gives the default system language. $EDITOR: Gives default file editor. $UID: Gives user ID of the current user. $SHELL: Gives location of current user's shell program.
How do you terminate an ongoing process?
Every process has a unique process id. We can terminate the process using the kill command followed by the PID. So first we need to find the PID of the process. The ps command will list all the running processes along with the process id. After running the ps command you can select the process PID which you want to select.
Note: To terminate all processes at once, use kill 0.
Write a command that will display all .txt files, including its individual permission.
ls -al *.txt
How can you find the status of a process?
Using the below option with ps command we can check the status of a process.
ps axu
How can you append one file to another in Linux?
Using the cat command we can append one file to another file in Linux. We need to use ‘>>‘ (double greater than) symbol with cat command to append the file. Let us see an example of how we can append one file to another file.
Here, the contents of the test1 file will be appended at the end of the test2 file.
cat test1 >> test2
Explain how to uninstall the libraries in Linux?
To uninstall the libraries in Linux, you can use below commands,
If you only want to uninstall the library and don’t want to delete the configuration file.
sudo apt-get remove library_name
If you only want to uninstall the library and as well as want to delete the configuration files.
sudo apt-get --purge remove library_name
If you want to remove any unused packages, use the “autoremove” command.
sudo apt-get autoremove
If you want to remove a program and also their dependencies that are no longer being used.
sudo apt-get purge --auto-remove library_name
What is the command to calculate the size of a folder?
To calculate the size of a folder uses the command,
du –sh Folder_Name
Explain virtual desktop.
A virtual desktop means that a user’s desktop environment (the icons, wallpaper, windows, folders, toolbars, widgets, etc.) is stored remotely on a server, rather than on a local PC or other client computing device. Desktop virtualization software separates the desktop operating systems, applications and data from the hardware client, storing this “virtual desktop” on a remote server.
The remote server that runs and supports virtual desktops uses software called a hypervisor to create a “virtual machine” that simulates the user’s desktop environment and capabilities. In a virtual desktop environment, users access their personal desktop remotely, over the Internet, from any client device.
There are two ways Virtual Desktop can be implemented:
- Switching Desktops.
- Oversized Desktops.
How do you execute more than one command or program from a single command line entry?
You can combine several commands by separating each command or program using a semicolon symbol. For example, you can issue such a series of commands in a single entry:
ls –l cd .. ls –a MYWORK which is equivalent to 3 commands: ls -l cd.. ls -a MYWORK
Note that this will be executed one after the other, in the order specified.
On a uniprocessor machine with preemption disabled, what will happen internally when we say spin_lock()?
What are the minimum requirements of Linux to work on hardware?
What are the pros and cons of using per CPU variable as a synchronization method?
What is the difference between context switch and preemption?
Can I lock a spinlock in one CPU and unlock it another CPU?
How do you test whether there are memory leaks in a Linux application?
On a multiprocessor system, how do you find out which process is running on which processor?
What is the use of L1, L2, L3 Caches?
What is the difference between binary semaphore and mutex in Linux?
What is the difference between down_interruptible vs down_killable in semaphore?
How to change the priority of a process in Linux?
Where are page tables stored?
What is SMP?
Difference between IO Mapped IO and Memory Mapped IO.
Difference between kmalloc and vmalloc.
Difference between processor and core.
How can I find out the Count of the number of times a process has been preempted in Linux?
What does malloc(0) return?
If the size of the requested space is zero, the behavior will be implementation-defined. The return value of the malloc could be a null pointer or it shows the behavior like that size is some nonzero value. It is suggested by the standard to not use the pointer to access an object that is returned by the malloc while the size is zero.
Let see an example C program, where I am allocating memory using the malloc with size 0.
#include<stdio.h> #include<stdlib.h> int main (void) { int *piBuffer = NULL; //allocating memory using //the malloc with size 0. piBuffer = malloc(0); //make sure piBuffer is valid or not if (piBuffer == NULL) { // allocation failed, exit from the program fprintf(stderr, "Out of memory!\n"); exit(1); } *piBuffer = 10; printf("%d\n",*piBuffer); free(piBuffer); return 0; }
Output: Implementation-dependent.
What is the cause of the stack overflow?
In the embedded application we have a little amount of stack memory as compare to the desktop application. So we have to work on embedded application very carefully either we can face the stack overflow issues that can be a cause of the application crash.
Here, I have mentioned some causes of unwanted use of the stack.
1. Improper use of the recursive function.
2. Passing to many arguments in the function.
3. Passing a structure directly into a function.
4. Nested function calls.
5. Creating a huge size local array.
What is NUMA?
What is the maximum amount of time CPU can be in the critical section after acquiring spinlock?
Difference between GFP_KERNEL and GFP_ATOMIC.
What happens internally during context switch in Linux kernel?
Which file in Linux gives you information about memory zones?
What is buffer/cache?
What is the asm-generic folder in the Linux source code?
What it contains?
Will a module be loaded if it has while(1) loop in module_init function?
What is the maximum memory that can be allocated using vmalloc?
What is the maximum memory that can be allocated using kmalloc?
What is the difference between VIRT, RES and SHR fields in top command?
What is the system call used by malloc and free?
What is the maximum memory that I can allocate using malloc?
The Makefile macro that one sets to identify what file for the kernel Makefile to make into a module is _____. a. obj-m b. obj-y c. target d. list.
How do you check how many lanes are being used by PCIe card in Linux?
The maximum number of PCI devices that can be connected to a host.
What are lanes in PCI?
How auto-detection of PCI devices happen in PCI?
What is a PCI bridge?
Where do executables look for shared objects at runtime?
Do microprocessors have internal memory?
What is the purpose of fPIC flag while generating the shared library.
What is the load average in Linux?
Linux intentionally leaves the first few kilobytes (or even megabytes) of each process’s virtual address space unmapped, so that attempts to dereference null pointers generate an unhandled page fault resulting in an immediate SIGSEGV, killing the process.
Is System bus = address bus + data bus + control bus.
What is the use of O_SYNC flag while opening the file?
What is resident memory in the process?
Virtual memory is divided into ____ a) pages b) bytes c) bits.
What are the advantages of using virtual memory?
What are the advantages of the static library over a shared library?
What is the use of the Procedure-linking table (PLT) while an application is starting up.
What are the advantages of the shared library over static library?
What should be the number of jobs when compiling Linux kernel make -j.
What is the difference between a file and an inode?
How is the scheduler invoked in Linux kernel?
What is the use of initrd image while booting?
What is present inside a static library?
What is the use of likely and unlikely macros in the Linux kernel?
How do you find out which init manager is running on your Linux machine: sysvinit/systemd?
What are relocations in an object file?
Can I call kmalloc(GFP_KERNEL) while holding a spinlock?
What is the benefit of declaring a symbol as weak?
What is the first function that will be called when Linux kernel is loaded?
What are the lock-free algorithms present in Linux kernel?
Why running ps command without options on shell shows only two entries?
What happens when you call spin_lock() on a uniprocessor system with preemption enabled?
What are the operations performed by the linker during the compilation process?
What is the data structure implemented in the Linux kernel?
What are the advantages of sigaction over signal?
What is the use of the swapper process in Linux?
How to kill the process which is in TASK_UNINTERRUPTIBLE state?
Recommended Post
- Can protocol interview Questions.
- 100 C interview Questions.
- I2C interview Questions
- Interview questions on bitwise operators in C
- C++ Interview Questions.
- 10 questions about dynamic memory allocation.
- File handling in C.
- Pointer in C.
- C format specifiers.