April 15, 2024

Part-8: Basic 10 Linux Interview Questions and Answers

In this article, we’ve included 8th set of 10 basic Linux Interview Questions and detailed answers that will help candidates prepare for the Linux interview and get the best job in the IT industry.

These Linux interview questions and answers will be useful for both freshers and experienced professionals at any level.

Some of the questions are for advanced Linux professionals, however, beginners or fresher can easily understand the answers and explanations described below.

Q.1) What is at command in Linux?

at is a command-line utility that is used to schedule commands to be executed at a specific time. Jobs created with at are executed only once and not repeated.

To list the user’s pending jobs, run:

$ at -l
or
$ atq

The below command will execute the “script.sh” at 01.10 am

$ echo "sh /path/to/script.sh" | at 01.10 am

Q.2) What is Linux pipes symbol “|”? How does a pipe work in Linux?

Pipes (also known as named pipes). The pipe is a command in Linux and Unix-like operating systems that sends the output of one command or process acts as input to another command or process.

Pipe is used to combine two or more commands at the same time and run them consecutively and so on. Pipe is one of the most powerful shell operators which is heavily used in the shell script.

Also, help Linux administrator to do their daily job very quickly.

Common Syntax of pipes:

command-1 | command-2 | command-3 | …. | command-N

How to use pipe in Linux?

If a directory contains 100 of files that can’t be listed all together in a single page using the ls command. To overcome this issue, you need combine less command with pipe as shown below:

ls -lh | less

Q.3) What’s Network Bonding in Linux?

Bonding is a Linux kernel feature that allows for aggregating two or more network interfaces into a single virtual bonded interface. It offers performance improvements and redundancy by increasing the network throughput and bandwidth.

In simple term, if one physical NIC is down or unplugged, it will automatically move resources to other NIC with help of bonding driver.

The most popular types of bonding methods are as follows:

Mode-0 (balance-rr) : This mode is based on Round-robin policy and it is the default mode. This transmit packets in sequential order from the first available slave through the last, which provides load balancing and fault tolerance.

Mode-1 (active-backup) : This mode is based on Active-backup policy. Only one slave in the bond is active and the other slave becomes active if the active slave fails.

Mode-4 (802.3ad) : This mode is known as a Dynamic Link Aggregation mode. It creates aggregation groups that share the same speed and duplex settings. Utilizes all slaves in the active aggregator according to the 802.3ad specification. It commonly known as Link Aggregation Control Protocol (LACP), which must be enabled on the network switch port as well.

Q.4) What is ulimit command in Linux?

The ulimit command in Linux is used to set or report user resource limits as defined in the '/etc/security/limits.conf' file. The default limits used when adding a new user to the system are defined in the limits.conf file. Limits are classified as soft or hard.

To check the ulimit value, run:

ulimit -a
Linux check user resource limits using ulimit command

Q.5) What is Login Shell in Linux?

A login shell is a shell that assigned to a user once they log in to their account, which requires a username and password. For example, when you access a remote Linux system through ‘SSH’ or switch to user with “su -” command.

A login shell is the first process that executes under your user ID when you log in that also executes a set of pre-defined scripts to configure your shell environment.

To identify the login shell, run: If the output is prepended by a ‘-‘ dash, then it is a login shell.

su - root
Password: 

echo $0
-bash

Login shell executes the pre-defined scripts in the following order:

  • First, Login shell executes /etc/profile file
  • /etc/profile executes all scripts under /etc/profile.d/ directory
  • Then executes users ~/.bash_profile
  • ~/.bash_profile executes users ~/.bashrc
  • ~/.bashrc executes /etc/bashrc

Q.6) What is Non-Login Shell in Linux?

A Non-Login shell, also known as sub-shell, is a shell that is executed without logging in. For example, when you open a graphic terminal in ‘GNOME’ it is a non-login (interactive) shell.

To identify the non-login shell, run: If the output comes without an extra dash, then it is a login shell.

echo $0

bash

Non-login shell executes the following pre-defined scripts:

  • First, Non-login calls ~/.bashrc
  • Then ~/.bashrc calls /etc/bashrc
  • Finally, /etc/bashrc calls the scripts under /etc/profile.d directory

Q.7) What is the maximum length of a file name in Linux?

The maximum length of a file name under Linux is 255 bytes, not 255 characters.

To check the maximum length of the file name, use the getconf command:

getconf -a | grep -i name_max

NAME_MAX                           255
_POSIX_NAME_MAX                    255
LOGNAME_MAX                        256
TTY_NAME_MAX                       32
TZNAME_MAX                         
_POSIX_TZNAME_MAX                  
CHARCLASS_NAME_MAX                 2048
HOST_NAME_MAX                      64
LOGIN_NAME_MAX                     256

Q.8) What are dot (hidden) files in Linux

In general, filenames that are preceded by a dot are hidden files. These files are used to store configurations for various applications and most importantly, hiding these files reduces the chance of accidental deletion.

Files starting with a dot are ignored by default in the ls command output, but they can be listed using the '-a' option as shown below:

ls -la

total 1580
drwxr-xr-x 1 linuxgeek users    1208 Sep  5 04:21 .
drwxr-xr-x 1 root      root       18 Mar  3  2021 ..
drwxr-xr-x 1 linuxgeek users     290 Sep  5 11:09 .atom
-rw------- 1 linuxgeek users   23981 Sep  5 03:34 .bash_history
-rw-r--r-- 1 linuxgeek users    1377 Jul 14 22:19 .bashrc

Q.9) What is shell script in Linux?

Shell scripting is a program with a series of commands designed to be executed by the Unix shell. In simple terms, a shell script is a file containing a series of commands.

Steps to create a Shell Script

  • Create a file name with .sh extension
  • Start the script with #!/bin/sh
  • Add the required code and save file.
  • Finally, execute the file by typing ‘bash demo-script.sh’

How to Write a Shell Script in Linux?

Lets write a simple shell script to check who is currently logged in and how long the Linux system is up and running.

vi demo-script.sh

#!/bin/sh
echo -e "Current logged in user is: `whoami`\nsystem up and running for: `uptime|awk '{print $1}'`"

Output:

bash demo-script.sh

Current logged in user is: linuxgeek
system up and running for: 03:05:03

Q.10) How to execute more than one command at once in Linux

Running two or more commands in one line can save your time. It can be done in three ways as shown below:

  • ; – cmd-1 ; cmd-2 – Execute cmd-1 first and then cmd-2
  • && – cmd-1 && cmd-2 – Execute cmd-2 only if cmd-1 runs successfully
  • || – cmd-1 || cmd-2 – Execute cmd-2 only if cmd-1 fails

Q.10.1) Using (;)

You need to use semicolon (;) if you want to run multiple commands in one line.

cd cpufetch; ls -lh

total 100K
-rw-r--r-- 1 linuxgeek users 3.0K Jul 29 18:20 CONTRIBUTING.md
-rwxr-xr-x 1 linuxgeek users  74K Jul 29 18:28 cpufetch
-rw-r--r-- 1 linuxgeek users 2.4K Jul 29 18:20 cpufetch.1
drwxr-xr-x 1 linuxgeek users   98 Jul 29 18:20 doc
-rw-r--r-- 1 linuxgeek users 1.1K Jul 29 18:20 LICENSE
-rw-r--r-- 1 linuxgeek users 2.1K Jul 29 18:20 Makefile
drwxr-xr-x 1 linuxgeek users  132 Jul 29 18:20 pictures
-rw-r--r-- 1 linuxgeek users 5.0K Jul 29 18:20 README.md
drwxr-xr-x 1 linuxgeek users   24 Jul 29 18:20 src

Q.10.2) Using (&)

The next command will run only when the previous command runs successfully.

cd cpufetch && ls -lh
bash: cd: cpufetch: Not a directory

Q.10.3) Using (||)

The next command will run only when the previous command fails.

cd cpufetch || ls -lh

bash: cd: cpufetch: Not a directory
total 100K
-rw-r--r-- 1 linuxgeek users 3.0K Jul 29 18:20 CONTRIBUTING.md
-rwxr-xr-x 1 linuxgeek users  74K Jul 29 18:28 cpufetch
-rw-r--r-- 1 linuxgeek users 2.4K Jul 29 18:20 cpufetch.1
drwxr-xr-x 1 linuxgeek users   98 Jul 29 18:20 doc
-rw-r--r-- 1 linuxgeek users 1.1K Jul 29 18:20 LICENSE
-rw-r--r-- 1 linuxgeek users 2.1K Jul 29 18:20 Makefile
drwxr-xr-x 1 linuxgeek users  132 Jul 29 18:20 pictures
-rw-r--r-- 1 linuxgeek users 5.0K Jul 29 18:20 README.md
drwxr-xr-x 1 linuxgeek users   24 Jul 29 18:20 src

Conclusion

In this guide, we’ve included the most frequently asked 8th set of 10 basic Linux Interview Questions and detailed answers for your reference purpose and we hope it will be very useful.

If you have any questions or feedback, feel free to comment below.

Leave a Reply

%d bloggers like this: