Linux Interview Questions
By: Great Learning
Tags: Linux Tutorial, Interview, Linux, Interview Questions

Contributed by: Subashree

The Linux Operating System (OS) is an open-source and one of the most reliable OS in the world. With rising numbers of data centers, many organizations use Linux. It is easy to customize and makes it highly versatile. Android OS, the mobile operating system is based on a modified version of Linux OS.

As Per 2019 report, 100% of the world’s supercomputers run on Linux! 90% of all cloud infrastructure operates on Linux!

Linux desktop market share is just about 3.17% currently, which is not a great deal compared to Windows with >75% market share in Desktops. Nevertheless, its penetration for the Cloud based market is huge.

We are now in times when the COVID-19 pandemic is impacting every industry worldwide, there arises a higher demand for cloud-based systems. This further enables market growth for Linux OS, now and in the future. Many open source projects using Linux OS are supporting healthcare providers, scientists, innovators, software developers across geographies to help alleviate impacts from the pandemic. Advent of hybrid cloud technology aid to the increase in the demand for the Linux OS. There cannot be a more favorable time to adopt Linux OS.

Curated below are some of the frequently asked Linux interview questions. While it is expected from the learners that basic Linux terminologies are clearly understood, please follow below for the interview questions.

1. How to check for running processes and CPU usage from command prompt?

Answer: The ‘top’ command is used to check for all the current running processes, CPU usage and memory usage from the command prompt. This provides a dynamic real-time information on resource usage.

2. Write the command to find all the files modified in the last 3 days and write to a myfile.txt file?

Answer: The command is as below:

find / -mtime 3 > myfile.txt

find / -type f -mtime -3 -printf “%M %u %g %TR %TD %p

” > last3days.txt

3. How to list all the directories in Linux?

Answer: The command ls -d */ is used to list all directories.

4. How to see environment variables in Linux?

Answer: env command lists all the ENVs in the shell.

5. How to know the disk partition details?

Answer: The command df -k is used to know the partition information.

6. How to start a particular process say ‘myengine.sh’ automatically every time after reboot?

Answer: Crontab can be set up to run this .sh file automatically after reboot. The command is as follows to edit the crontab and to add the .sh file to the cron:

crontab -e

@reboot myengine.sh

7. How do you know what shell you are currently using on Linux?

Answer: echo “$SHELL”

8. How to know which process is using a particular port?

Answer: netstat command is used to display network connection details. For a particular port, using grep with netstat as below will help.

netstat -ltnp | grep 8080

To further know the PID of the process, use of sudo helps.

sudo netstat -ltnp | grep 8080

9. What is the difference between kill and kill -9 commands?

Answer: kill will generate a SIGTERM asking the process to kill itself gracefully. This will free up memory and gracefully exit.

kill -9 will generate a SIGKILL which kills the process immediately irrespective of which state the process is in. It does not wait for the process to clear memory or wait for a graceful exit.

10. Log details generally get printed in the console. I want 2 files — 1 is errorlog.txt that has all the errors and dailylog.txt that should have all other statements such as debug, information, warnings, etc. How do you do that?

Answer: While running the process cout can be used as below:

./processName.sh 2>errorlog.txt 1>dailylog.txt &

11. How to setup GitHub with SSH in Linux? What are the benefits of using ssh in this case?

Answer: Using SSH for Git is to connect with public key encryption and hence there is no need of typing username and password for every Git command. By default, Git uses HTTPS and using SSH instead is easier and secure. We can setup SSH using the below steps:

We need to generate new ssh keys using:

ssh-keygen -t rsa -b 4096 -C email_id@email.com

Now, after checking if ssh is running, add the SSH key to the ssh-agent:

ssh-add ~/ .ssh/id_rsa

Now, copy the public SSH key to clipboard

In GitHub settings, add this public key.

12. How to map a drive in Linux?

Answer: We will need a samba-client installed and as a root user:

mount -t cifs //hostname/share /targetdir -o user=userID

13. What is shebang and how to use it?

Answer: Shebang is a special character sequence in a script file that specifies which program should be called to run the script. It will always be the first line in the file and starts as #! followed by the path of the interpreter. For example:

To use Perl: #!/usr/bin/perl

14. You have a python file say ‘collectedData.py’. How will you run it in shell from the current directory without mentioning python each time?

Answer: In the .py file, add the below in the first line. This lets the shell know python is to be used to run this file.

#!/usr/bin/python

Next, provide the executable permission to this file using:

chmod +x collectedData.py

From now on, ./collectedData.py will work.