All Courses
Linux Interview Questions

Did you know that more than 90% of the fastest computers in the world use Linux? No doubt the reason! Linux is fast, powerful and a favorite of engineers. If you want to be a Linux administrator, you’re in the right place to prepare for an interview. This article describes the most common and important interview questions and answers for Linux.
Are Interested in managing Administration? Enroll with our Linux online course.

1. What is the boot process on Linux?

As a user, when you press the power button on your system, the monitor will display a login screen, and after logging in, you will be able to start working.
Have you ever wondered what happens between the time you press the power button and the time you see the login screen?
Each operating system has a boot process that runs in sequence, and finally, the operating system login screen is displayed.

Linux Interview Questions
LINUX BOOTING PROCESS.

These are the section that walks you through the Linux boot process.

2. How to create zero-size file Linux?

  • Touch the file name when creating an empty file.
  • If you want to add data and keep existing files, it’s a good idea to redirect the files to another folder.
  • You can use echo to create an empty file using the echo -n / filename command.
  • The following steps describe how to create a file without an existing name. Printf *>
  • If you don’t see the filename, try ls -l.

3. what is the main difference between a soft link and a hard link?

Here you will learn the main differences between hard links and soft links. The commands to create the links are:

$ ln myfile.txt my-hard-link
$ ln -s myfile.txt my-soft-link

The various differences between hard links and soft links are:

Linux Interview Questions
  • Softlinks are specific pointers that connect system files. This is very similar to the shortcut feature available on Microsoft’s Windows operating system. In contrast, hard links are different from soft links. Serves as a pointer to the original file. That is an exact mirror copy of the original file. This is just an alias for an existing file.
  • A hard link is an additional name for the original file that points to the target file through the inode. Soft Link, on the other hand, is an alternative file, unlike the original file, but does not use inodes.
  • Hard links remain valid even if the target file is deleted. On the other hand, if you delete the original file, the softlink will be disabled.
  • Under certain circumstances, hard links perform better than soft links.
  • The “ln” command is used to create hard links on Linux. On the other hand, the softlink command is “ln -s”.
  • Hard links are restricted to their own partitions. Softlinks, on the other hand, can cover a variety of file systems.
  • Softlinks support both absolute and relative paths. Hard links, on the other hand, do not support relative paths.
  • Hard links are faster than soft links. Soft links, on the other hand, are slower than hard links.
  • You cannot create hard links outside the file system. Softlinks, on the other hand, can be established throughout the file system.
  • Hard links can only point to files, not directories. Softlinks, on the other hand, can reference both files and directories.

4. How to run a shell script in the background?

  • Execute a command in the background using & $ ./my-shell-script.sh &
  • Execute a command in the background using nohup
  • After you execute a command (or shell script) in the background using &, if you log out from the session, the command will get killed. To avoid that, you should use nohup as shown below.
  • $ nohup ./my-shell-script.sh &

5. What is crontab?

Crontab is a list of commands that you run on a regular basis and the names of the commands that are used to maintain that list. Crontab stands for “cron table” because it uses the job scheduler cron to perform tasks. Cron itself is named after Chronos, the Greek word for time. Cron is a system process that automatically executes tasks on a set schedule. The schedule is called crontab and is also the name of the program used to edit this schedule. Linux crontab format

MIN HOUR DOM MON DOW CMD

Crontab Fields and Allowed Ranges (Linux Crontab Syntax)

Field Description Allowed Value
MIN Minute field 0 to 59
HOUR Hour field 0 to 23
DOM Day of Month 1-31
MON Month field 1-12
DOW Day Of Week 0-6
CMD Command Any command to be executed.

6. How to allow the ports in Linux?

  • iptables -A INPUT -p tcp –dport 12375 -j ACCEPT
  • iptables -A OUTPUT -p tcp –dport 12375 -j ACCEPT
  • you can also add them to your iptables- config file then restart iptables
/etc/init.d/iptables restart

7. How to trouble shoot the remote server having some issue?

  • First will do ssh if it’s connected
  • Uptime
  • Top
  • Free
  • df
  • du

8. What is ping? Telnet? Curl? Wget?

  • Ping commonly used to check whether your connection to the server is healthy or not.
  • Telnet is a user command and an underlyingTCP/IP protocol for accessing remote computers.
  • wget is a tool to download files from servers.
  • curl is a tool that let’s you exchange requests/responses with a server

9. How to check all running services in Linux?

There are many ways and tools to check and list all the services running on Linux.
Most Linux administrators typically use “serviceservice_name status” or “/etc/init.d/service_namestatus” for System V (SysV) init systems and “systemctl status service_name” for systemd systems for specific services. To do.
The above command indicates whether each service is running on the server. These are the commands commonly used by Linux administrators. If you are new to the
environment and want to know what services are running on your system, read on.
The following command lists all the services running on the system and shows what the system is used for. It may also disable certain services that are not used by your system.
“init” (short for initialization) is the first process started when the computer system boots. init is a daemon process that keeps running until the system shuts down.

Many init systems have been developed for Linux, but the three most common and commonly used init systems are:

  • System V (Sys V) is the older init system
  • Upstart is an event-based replacement for the traditional init system
  • systemd is the new init system, that was adopted by most of the latest Linux distributions

10. How to kill a process in Linux?

Locate the Process to Kill

The ps command displays information similar to top, but not in the form of an interface. Instead, the ps command provides a complete list of running processes, formatted based on the tags you add.

ps <options>

kill Command

If you know a process ID, you can kill it with the command:

kill <processID>

The kill command kills one process at a time using the specified process ID. Sends a SIGTERM signal telling you to stop the process. Waiting for the program to execute the shutdown routine.
You can use the -signal command to specify a signal that is not SIGTERM.

kill -9 Linux Command

kill -9 is a useful command if you need to shut down a service that is not responding. Execute it in the same way as a normal kill command.

kill -9 <processID>

(or)

kill -SIGKILL <processID>

The kill -9 command sends a SIGKILL signal. This signal tells the service to shut down immediately. Non-responding programs ignore the kill command, but shut down when the kill-9 command is issued. Use this command with caution. Bypassing the standard shutdown routine, all unsaved data will be lost.
If the SIGKILL signal does not shut down the service, the operating system is not running properly.

11. What is nice and renice?

  • You can ask the kernel to put a particular process of yours, on higher priority than others using nice commnad
  • Process priority values range from -20 to 19 nice value.
  • nice -10 <command name>
  • In order to change the priority of an already running process you can use “renice” command.
  • renice 13 -p <PID>
  • -20 (most favorable to the process) to 19 (least favorable to the process)

12. What is inode value?

  • An inode is an entry in inode table, containing information ( the metadata ) about a regular file and directory.
  • An inode is a data structure on a traditional Unix-style file system such as ext3 or ext4
  • $ ls -il myfile.txt

1150561 -rw-r–r– 1 root root 0 Mar 10
01:06 myfile.txt

13. How to check the CPU utilization?

top Command to View Linux CPU Load

Open a terminal window and enter the following:

top

The system should respond by displaying a list of all currently running processes. It also shows users, tasks, CPU usage, and memory usage.
This list can change frequently at the start and completion of background tasks. A convenient switch is to start from scratch with the -i switch.

top –i

This hides all idle processes and makes it easier to sort the list.
To exit the top function, press the letter q on your keyboard.

14. Difference between Top/HTop?

  • In ‘htop’ you can scroll the list vertically and horizontally to see all processes and complete command lines.
  • In ‘top’ you are subject to a delay for each unassigned key you press (especially annoying when multi-key escape sequences are triggered by accident).
  • ‘htop’ starts faster (‘top’ seems to collect data for a while before displaying anything).
  • In ‘htop’ you don’t need to type the process number to kill a process, in ‘top’ you do.
  • In ‘htop’ you don’t need to type the process number or the priority value to renice a process, in ‘top’ you do.
  • ‘htop’ supports mouse operation, ‘top’ doesn’t
  • ‘top’ is older, hence, more used and tested.

15. What is mount? How to create mount?

  • Mounting a filesystem means making the particular filesystem accessible at a certain point in the Linux directory tree.
  • $ mount
  • When you type this at a command prompt, this command will display all the mounted devices, the filesystem type it is mounted as, and the mount point.
  • mount [OPTION…] DEVICE_NAME DIRECTORY
  • https://linuxize.com/post/how-to-mount-and-unmount-file-systems-in-linux/

16. How to troubleshoot live logs?

Troubleshooting is one of the main reasons people create logs. If you encounter a problem, you need to diagnose it and understand why and why it occurred. An error message or set of events can give you clues as to the root cause, how to reproduce the problem, and help you arrive at a solution. This section shows scenarios where you can use Linux logs for troubleshooting.

We can troubleshoot live logs by using the following command:

“tail –f filename”

17. What is sed command?

The Linux SED command stands for StreamEDitor and is useful for a variety of common operations on text files and streams. Sed helps you select text, replace text, modify the original file, add lines to text, remove lines from text, and more. With
SED, you can edit without opening the file. It’s much faster to find and replace the file in the file than to first open the file in the VI Editor and then modify it.
Unix SED commands support regular expressions, allowing you to perform complex pattern matching.

Syntax:

sed [OPTION]… {script-only-if-no-other-script} [input-file]…  

18. What is AWK command?

The awk command is used for word processing on Linux. The sed command is also used for word processing, but it has some limitations that make the awk command a convenient option for word processing. You have strong control over your data.
Awk is a powerful scripting language used for text scripts. Find and replace text, sort, validate, and index databases.
This is one of the most used tools for programmers to create small and effective programs in the form of statements that define text patterns and designs.
Works as a filter on Linux. Also known as gawk (GNU awk) on Linux.

Syntax:

The AWK command is used as follows:

awk options ‘selection _criteria {action }’ input-file > output-file  

The options can be:

  • -f program files: It reads the source code of the script written on the awk command
  • -F fs: It is used as the input field separator.

19. What is grep and egrep?

  • grep command we will use to search particular content.
  • grep madhu filename.txt
  • Egrep we will use you want grep mutiple word at a time
  • egrep ‘madhu|sudhan’ filename.txt

20. How to check the running ports?

To check the listening ports and applications on Linux:

  • Open a terminal application i.e. shell prompt.
  • Run any one of the following command on Linux to see open ports:
sudo lsof -i -P -n | grep LISTEN
sudo netstat -tulpn | grep LISTEN
sudo ss -tulpn | grep LISTEN
sudo lsof -i:22 ## see a specific port such as 22 ##
sudo nmap -sTU -O IP-address-Here
  • For the latest version of Linux use the ss command. For example, ss -tulw

21. How to declare a variable in a shell script?

Variables are used to store any values. The value can be an integer, a floating point number, a character, or a string. You don’t have to write the data type as you would in a C or C ++ language.

For example:

days=5
name=”Pavithra”
percentage=97.7

These are the all valid variable definitions. Also, you do not need to specify the data type. The shell handles it automatically.

The general syntax is:

variable=value

22. What is $#, $?, $0, $*,$@ ?

  • $# Stores the number of command-line arguments that were passed to the shell program.
  • $? Stores the exit value of the last command that was executed. (0 or 1)
  • $0 Stores the first word of the entered command (the name of the shell program).
  • $* Stores all the arguments that were entered on the command line ($1 $2 …).
  • $@ Stores all the arguments that were entered on the command line, individually quoted (“$1” “$2” …).

23. What is umask?

  • Return, or set, the value of the system’s file mode creation mask.
  • In Linux, the default permissions value is 666 for a regular file, and 777 for a directory.
  • When creating a new file or directory, the kernel takes this default value, “subtracts” the umask value, and gives the new files the resulting permissions.
  • ( 666– 022 = 644)
  • /etc/profile.d

24. How to change file permission in Linux?

  • using chmod commnad.
  • the user can read, write, ande xecute it;
  • members of your group can read ande xecute it; and
  • others may only read it.
  • chmod u=rwx,g=rx,o=r myfile
  • chmod 754 myfile
  • 4 stands for “read”,
  • 2 stands for “write”,
  • 1 stands for “execute”, and
  • 0 stands for “no permission.”

25. What is the purpose of export command?

The export command is a built-in Linux Bash shell utility. This is used to ensure that environment variables and functions are passed to the child process. It does not affect existing environment variables.
When you open a new shell session, the environment variables are set. If you change the variable value at any point, the shell has no way of choosing that change. You can use the export command to update the current session for changes made to the exported variables. You don’t have to wait to start a new shell session.

Syntax:

export [-f] [-n] [name[=value] …] or export -p  

For example:

The basic export command displays all the system’s exported environment variables. It will be executed as follows:

export  

Consider a snapshot of the following output:.

Linux Interview Questions
export Command