Top 48 Linux Interview Questions & Answers

Channel: Linux
Abstract: use the chmod command followed by the octal value or symbolic value then the file name or directory name. For example to assign a file permissions val

Are you preparing for a Linux interview? We have prepared some of the commonly asked Linux interview questions and their answers.

If you are a beginner (with some knowledge of Linux or having certification) or with professional Linux administration experience, then following Q & A help for your interview preparation.

1. What is Linux and basic components?

Linux is a free and opensource Operating system that is based on the Linux kernel. It's one of the widely used operating systems, especially in the server world and by developers. Its usage extends from hosting websites and applications to being the core operating system in smart devices like smartphones, tablets, and TVs.

Top 10 Interview Questions and Answ...

To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video

Top 10 Interview Questions and Answers

Linux constitutes 4 basic components:

Kernel: This is the core of the Linux system. It's responsible for interacting with the hardware components and ensuring the operating system communicates with hardware devices.

Shell: A shell is an interface between the Linux user and the kernel. It's in the shell that commands are issued by the user and are thereafter executed by the operating system.

System libraries: These are special programs or functions which are responsible for implementing a majority of the functionalities of the operating system without relying on the kernel modules' code access rights.

System Utilities: These are specific programs that executed certain tasks. e.g LibreOffice, Brasero, Gparted.

2. What is the Linux Kernel?

A Linux kernel is the core of the Linux system. It interfaces the underlying hardware with the operating system.

3. What is the difference between Linux and Unix?

Linux is a clone of the UNIX system from which Linux is derived from. While Linux is opensource and free to use, UNIX is a proprietary operating system.

4. What are inodes in Linux? How to find the inode associated with a file?

The inode (index node) is a data structure in a Unix-style file system that describes a file-system object such as a file or a directory. When a file is created, it is assigned both a name and an inode number, which is an integer that is unique within the filesystem.

Explanation

File systems, in general, have two parts: the metadata or the 「data」 about the data and the data itself. Metadata consist of information about the data. More precisely it includes information such as the Access Control List (ACL), the date the file was modified, file owner, file permissions, size of file, device ID, uid of the file, etc. This type of information is key to a file system otherwise we just have a bunch of bits on the storage media that don’t mean much. Inodes store this metadata information and typically they also store information about where the data is located on the storage media.

In a file system, inodes consist roughly of 1% of the total disk space, whether it is a whole storage unit (hard disk, thumb drive, etc.) or a partition on a storage unit. The inode space is used to track the files stored on the hard disk. The inode entries only points to these structures rather than storing the data. Each entry is 128 bytes in size.Space for Inodes is allocated when the operating system or a new file system is installed and when it does its initial structuring. So this way we can see that in a file system, the maximum number of Inodes and hence maximum number of files are set. Now, the above concept brings up another interesting fact. A file system can run out of space in two ways:

  • No space for adding new data is left
  • All the Inodes are consumed.

To get a listing of an inode number, use ls -i command.

ls -li
total 3336
57741 -rw-r--r-- 1 root root 3412144 Nov 20 17:26 coreutils-8.22-18.el7.x86_64.rpm
57725 -rw-r--r-- 1 root root       0 Apr  7 04:00 file
57736 -rw-r--r-- 1 root root       0 Apr  7 04:00 new-file

# ls -li new-file 
57736 -rw-r--r-- 1 root root 0 Apr  7 04:00 new-file

# find /root -inum 57736
/root/new-file
5. What is Linux shell? How to check current shell?

A Linux shell is a command interpreter or a program that accepts commands and passes them to the operating system for execution. To check the shell that you are using, run the command  echo $0

6. What command to find memory and swap usage?

Use free command

7. Differences between Softlink and Hardlink?

a) Hardlink cannot be created for directories. A hard link can only be created for a file.

b) Symbolic links or symlinks can link to a directory.

c) Removing the original file that your hard link points to does not remove the hardlink itself; the hardlink still provides the content of the underlying file.

d) If you remove the hard link or the symlink itself, the original file will stay intact.

e) Removing the original file does not remove the attached symbolic link or symlink, but without the original file, the symlink is useless

8. Explain file permission in Linux?

In Linux, there are 3 main types of file permissions: read, write and execute. These permissions can be assigned to either a file or a directory recursively.  Being a multi-user system, you can assign these permissions to the root user, groups or even to other users using the system.

Read: The read permission grants uses the ability to open and read a file.

Write: Write permission allows a user to open and modify or edit the file's contents and save the changes.

Execute: This allows a user to execute or run the file or a program or shell script which is executable.

Please find below linux permission table:

9. How to create and delete a directory in Linux?

To create a directory use the mkdir command as shown;

mkdir directory_name

For example to create a directory called 'data'  run the command:

mkdir data

To delete a directory in Linux, use either the rm or rmdir command.

The rmdir command is used for removing empty directories.

The rm command is mostly used with the -R flag for recursively removing directories.

10. How to create files in Linux?

To create files, use the touch command. For example to create a file file1.doc run the command:

touch file1.doc

You can also use cat command followed by the redirection operator or greater than sign >
followed by the name of the file. Next, type the content of the file and lastly press CTRL + D to exit the file.

cat > file1.doc

Type the file contents and hit CTRL + D

11. How to rename file and directory in Linux?

To rename files, use the mv command. Note that for this to work, the path of the file to be renamed needs to be the same. For example, to rename the file file1.doc in /data/files directory  to file2.doc execute the command:

mv /data/files/file1.doc /data/files/file2.doc

12. How to copy files and directory in Linux?

To copy files in Linux, use the cp command. The syntax is quite simple

cp  /source/of/the/file  /destination/of/the/file

13. How to list and mount devices in Linux?

To list mount points run the command:

df  -aTh

To find more information about the mount points on your system, execute the command:

findmnt

Additionally, you can use the cat command below

cat /proc/self/mounts

Also, you can use the mount command as shown

mount -l

14. How to create empty file in Linux?

To create an empty file in Linux, use the touch command as shown:

touch new_file

15. How to schedule a task in Linux? What is crontab and explain the fields in a crontab?

The cron is a deamon that executes commands at specific dates and times in Linux. You can use this to schedule activities, either as one-time events or as recurring tasks. Crontab is the program used to install, deinstall or list the tables used to drive the cron daemon in a server. Each user can have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly. Here are few of the command line options for crontab.

crontab -e Edit your crontab file.
crontab -l Show your crontab file.
crontab -r Remove your crontab file.

Traditional cron format consists of six fields separated by white spaces:

<Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week> <command/program to execute>

The format is explained as follows:

* * * * * *
| | | | | |
| | | | | +-- Year (range: 1900-3000)
| | | | +---- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month (range: 1-31)
| +---------- Hour (range: 0-23)
+------------ Minute (range: 0-59)
16. What are the basic Vim commands you know?

a) Save

To save a file, press ESC button and press :wq! OR :ZZ

b) Exit

To exit from a file without making changes, run the command :q

c) Jump to a particular line in a file

Press ESC and press j to move down by one line.

To move up by one line press k on the keyboard

Move the cursor to the beginning of a line Press ^

Move the cursor to the end of a line Press $

Move the cursor to the beginning of a file 1G

Move the cursor to the end of a file G

Moves the cursor to the start of line number 「n」 in the file nG

d) Copy text

Move the cursor to the beginning of the string or text. Next hit v on your keyboard and press cursor forward to highlight text. Once you get to the end of text that you wish to copy, hit y short for yank, to copy the text.

To copy text from current position to the end of the line - y$

To copy the entire line - yy

To copy 4 lines below - 4yy

d) Paste text

To paste text, simply press p on the keyboard

d) Delete text

To delete a line, move to the beginning of a line. Press the ESC button and press dd

To delete a single word, place the cursor in front of the word and hit dw

To delete text from the current word to the end of the line hit d$

To delete 3 lines below run 3dd

17. How to add binary to $PATH variable?
$ export PATH=$PATH:/path/to/the/binary/file
18. Explain Grep command and Regular Expressions?

Grep command is an expression used for filtering results or output. For instance, to list a directory and only search for files with the word 'cron', run the command:

ls | grep cron

19. What is the difference between umask and ulimit?

Umask stands for ‘User file creation mask’, which determines the settings of a mask that controls which file permissions are set for files and directories when they are created. While ulimit is a linux built in command which provides control over the resources available to the shell and/or to processes started by it.

You can limit user to specific range by editing /etc/security/limits.conf at the same time system wide settings can be updated in /etc/sysctl.conf

20. Explain output of ls command in Linux?

ls command lists the contents of a directory in Linux. It displays the files and sub-directories inside the directory. When used with other arguments, it can list files permissions and even display hidden files.

21. Explain the output of the top command in Linux?

The top command is a monitoring utility that gives a user insights into the system metrics such as uptime, load average,  %CPU and % memory utilization.

22. What are the process states in Linux?

Process States, as the name suggests refers to the current state that a Linux process is in. There are 4 process states namely Running, waiting, stopped and Zombie.

23. How to backup or archive files in Linux (tar command)?

To back up a directory, simply run the tar command  below

$ tar -cvf tarball_name.tar /path/to/directory

For example, to create an archive in tar.gz format of a file sales.txt  in /data/reports/ path execute:

$ tar -cvf sales.tar.gz  /data/reports/sales.txt

You can also archive a whole directory as shown:

$ tar -cvf repors.tar.gz  /data/reports/

24. How to stop a running process in Linux?

To stop a running process, use the kill command followed by the PID of the process.

For example, to stop a process with PID 3836, run the command

kill 3836

25. How to search files in linux?

To search files in Linux, use the locate  or find comand

26. How to check if a particular service in running?

To check if a service is running, use the syntax:

systemctl status service_name

For instance, to check is Postfix is running, run the command:

systemctl status postfix

27. How to start a service on reboot?

To start a service on reboot run use the syntax:

systemctl enable service_name

For example, to start httpd web server upon a reboot, run

systemctl enable httpd

28. How do you start and stop a service?

To start a service in a systemd system, run the command:

systemctl start service_name

For example, to start ssh service, run the command:

systemctl start sshd

To stop the service, run:

systemctl stop service_name

To stop ssh run:

systemctl stop sshd

29. How to remote login to another linux computer?

You can log in to a remote Linux computer using SSH protocol or using the VNC service.

30. How do you check disk usage?

Use the df command to check the space used on your hard drive and the space remaining / free space.
Additionally, use the du command to check usage by specific files and directories.

31. Why you use export command?

Export command marks and exports environment variables.

32. How to get version from multiple Linux servers using bash script?
#!/bin/bash
#we user variable serverlist to keep there path to file with server names
serverlist='server_list.txt'
#we write in variable all server list
servers=`cat $serverlist`
#we use variable result to keep there path to file with result
result='result.txt'

#this print header to file with resilt using \t\t to add 2 tab symbols
echo -e "Servername \t\t kernel version"> $result
#this get each line of serverlist one by one and write to server variable
for server in $servers
do
#this login to server by ssh and get uname -r
kernel=`ssh root@${server} "uname -r"`
#this write server name and kernel version separated by 2 tab to result file
echo -e "$server \t\t $kernel" >> $result
#end of for loop.
done
33. What is the minimum requirement for Linux installation?

With newer Linux distributions being launched ever other week or month, there's no clear cut minimum requirement. Newer versions will demand higher minimum requirements than older versions due to revamped GUI features and architecture. Nevertheless, any Linux distribution should work with a PC with the following minimum requirements:

25 GB of hard disk space
2 GB RAM
2 Ghz dual core processor
A screen resolution of 1024x768
A CD/DVD ROM or USB port for inserting installation media
34. How do you set Linux file/directory permissions?

To set file permissions, use the chmod command followed by the octal value or symbolic value then the file name or directory name.

For example to assign a file permissions value of 664 run

chmod 664 filename

To assign a directory permissions, use the -R option to assign permissions recursively.

chmod -R 664 directory_name

35. How to set ownership for files/directories?

To set file owner use the chown command. For example chown user:user filename

For a directory use the -R option to assign permissions recursively. For example

chown -R user:user directory_name

36. How to create user and group in Linux?

To create a user, run the command adduser user_nameFor example

adduser john
37. How to find the kernel/OS version in Linux?

Run the command uname -a

38. How to find the interface IP address?

You can easily find the IP interface by running the command ifconfig interface_name or using ip command (recommended).

For example

ifconfig eth0

ip addr show

39. What is initrd image?

The initial RAM disk (initrd) is an initial root file system that is mounted prior to when the real root file system is available. The initrd is bound to the kernel and loaded as part of the kernel boot procedure. The kernel then mounts this initrd as part of the two-stage boot process to load the modules to make the real file systems available and get at the real root file system. Thus initrd image plays a vital role in linux booting process.

40. Explain the terms suid, sgid and sticky bit?

In addition to the basic file permissions in Linux, there are few special permissions that are available for executable files and directories.

SUID: If setuid bit is set, when the file is executed by a user, the process will have the same rights as the owner of the file being executed.

SGID: Same as above, but inherits group privileges of the file on execution, not user privileges. Similar way when you create a file within the directory, it will inherit the group ownership of the directories.

Sticky bit: Sticky bit was used on executables in linux so that they would remain in the memory more time after the initial execution, hoping they would be needed in the near future. But mainly it is on folders, to imply that a file or folder created inside a stickybit enabled folder could only be deleted by the owner. A very good implementation of sticky bit is /tmp , where every user has write permission but only users who own a file can delete them.

41. What are the run levels in linux and how to change them?

A run level is a state of init and the whole system that defines what system services are operating and they are identified by numbers. There are 7 different run levels present (run level 0-6) in a Linux system for the different purpose. The descriptions are given below.

0: Halt System (To shutdown the system)
1: Single user mode
2: Basic multi user mode without NFS
3: Full multi user mode (text based)
4: unused
5: Multi user mode with Graphical User Interface
6: Reboot System

To change the run level, edit the file 「/etc/inittab」 and change initdefault entry ( id:5:initdefault:). If we want to change the run level on the fly, it can be done using ‘init’ command.

For example, when we type ‘init 3' in the command line, this will move the system from current runlevel to runlevl 3. Current level can be listed by typing the command 'who -r'

42. What is SeLinux?

SELinux is an acronym for Security-enhanced Linux. It is an access control implementation and security feature for the Linux kernel. It is designed to protect the server against misconfigurations and/or compromised daemons. It put limits and instructs server daemons or programs what files they can access and what actions they can take by defining a security policy.

43. Choose 5 Linux commands, what are your choices?

1) rsync command

The rsync command can be used to synchronize two directories or directory trees whether they are on the same computer or on different computers but it can do so much more than that. rsync creates or updates the target directory to be identical to the source directory.

rsync -aH sourcedir targetdir

The -a option is for archive mode which preserves permissions, ownerships and symbolic (soft) links. The -H is used to preserve hard links. Note that either the source or target directories can be on a remote host.

2) sed command

Sed command is used when you need to select specific lines of a file. Sed is short for stream editor, is one way to do this. you want to combine multiple files that all had headers or to do a bulk find and replace a file.
insert a blank line above every line which matches "regex"

$ sed '/regex/{x;p;x;}'

change "scarlet" or "ruby" or "puce" to "red"

$ sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'

3) awk command

Awk is a programming language that allows easy manipulation of structured data and the generation of formatted reports. It is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that match with the specified patterns and then perform associated actions. It is like sed command. If you are interested with awk command makes your life easy.

Print Specific Field

$ awk -F':' '{ print $1 }' /etc/group
$ date | awk '{print $2 " " $6}'

4) lsof command

lsof is a command line utility which is used to list the information about the files that are opened by various processes. In unix, everything is a file: pipes, sockets, directories, devices, etc. So by using lsof, you can get the information about any opened files.

List processes which opened a specific file

# lsof /var/log/syslog

lists all open files belonging to processes owned by the user

# lsof -u username

Kill all process that belongs to a particular user

# kill -9 `lsof -t -u username

List all network connections

# lsof -i

List all network files in use by a specific process

# lsof -i -a -c ssh

List processes which are listening on a particular port

# lsof -i :25

5) grep command

Grep is a command used to search text or for a given file for lines containing a match to the given strings or words. By default, grep displays the matching lines.

print network connection used by firefox

# netstat -pltnu | grep firefox

print the line which contains "root" on /etc/passwd file

# cat /etc/passwd | grep root
44. What is the difference between name based virtual hosting and IP based virtual hosting?

Virtual hosts are used to host multiple domains on a single apache instance. You can have one virtual host for each IP your server has, or the same IP but different ports, or the same IP, the same port but different host names. The latter is called "name based vhosts".

On IP-based virtual hosting, we can run more than one web site on the same server machine, but each web site has its own IP address while In Name-based virtual hosting, we host multiple websites on the same IP address. But for this to succeed, you have to put more than one DNS record for your IP address in the DNS database.

45. What is the advantage of Network Bonding?

Network Bonding is a Linux kernel feature that allows to aggregate multiple network interfaces into a single virtual link. This is a great way to achieve redundant links, fault tolerance or load balancing networks in the production system. If one of the physical NIC is down or unplugged, it will automatically move traffic to the other NIC card. Similar way the bonding will increase the interface throughput to handle the traffic it is configured in active-active mode.

46. What use of /etc/passwd and /etc/shadow file?

The /etc/shadow file stores actual password in an encrypted format with some additional properties related to user password. It mainly holds the account aging parameters. All fields are separated by a colon (:) symbol. It contains one entry per line for each user listed in /etc/passwd file.

47. Why setup password less ssh login?

To improve system security even further, most of the organizations turned to use key based authentications instead of Password-based authentication. We can enforce the key-based authentication by disabling the standard password authentication, which involves a public key private key pair. The public key is added in the server configuration file while the private key is kept confidential on the client-side.

48. What is swappiness in Linux?

The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.

swappiness can have a value of between 0 and 100

swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as possible

swappiness=100 tells the kernel to aggressively swap processes out of physical memory and move them to swap cache

Conclusion

In this tutorial, we have explained the top expected interview questions for a Linux job. Apart from these questions, you should be prepared to explain daily Linux tasks, projects, some critical situations you have faced. Good luck!!

Read Also:
  • Ansible Interview Questions & Answers for Devops
  • 30 Expected DevOps Interview Questions and Answers
  • 70 Shell Scripting Interview Questions & Answers

Ref From: linoxide
Channels:

Related articles