50+ Most Asked Linux Interview Questions and Answers

https://www.youtube.com/watch?v=9X7G3uhnP6k&list=WL&index=45&t=1s

  1. What is a shell?

    Ans:

  2. What is shell scripting?

    Ans:

  3. How to tell which shell you are in or running?

    Ans: $0

  4. Different types of commonly used shells on Linux systems?

    Ans:

  5. How to run/execute a script?

    Ans: ./script.sh or /path/script.sh or sh script.sh

  6. After executing a shell script, how to terminate it?

    Ans:

  7. What is the default login shell? How to change it?

    Ans: CHSH

  8. What is the purpose of the shebang line?

    Ans: (#!/bin/bash) To tell the Linux OS which interpreter to use to parse the rest of the file.

  9. Comments in a shell script?

    Ans:

  10. Multi-line comments in a shell script?

    Ans:

  11. Which commands are used to print output in bash?

    Ans: echo

  12. Which exit status indicates a successful execution for a bash script?

    Ans: 0

  13. How to make any variable unchangeable or readonly?

    Ans: using 'readonly' readonly variable_name=value

  14. How to find out how many arguments passed to a script?

    Ans: using $#

  15. Which of the following are used to end a case statement in a Bash script?

    Ans: esac

  16. How to check a directory exists or not using bash?

    Ans: [!-d /directory]

  17. In what ways, the shell script can check if the user executing it is a 'root' user or not?

    Ans: We can use against the value of UID shell variable UID of root user is always 0

  18. How will you automate your script to run/execute at a given time?

    Ans: Using crontab or at commands

  19. How will you find total shells available in your system?

    Ans: cat /etc/shells What is the use of ? gives you current process ID

  20. In a Bash shell script, if you want to restart the loop at the next iteration before the loop completes, use the ____ statement.

    Ans: Continue

  21. In a Bash script, which of the following tests if the value stored in the variable "MON" is equal to the string of "June"?

    Ans: [[${MON}="June"]]

  22. You are writing a Bash script and want to test if the "/tmp/out.txt" exists and can be read by the user running the script. Which of the following conditionals would you use?

    Ans: [-f/tmp/out.txt]

  23. In a Bash shell script, which of the following operators is used to test if 2 strings are equal?

    Ans: =

  24. Which of the following will take the output of the "hostname" command and store it in the variable named "HOST"?

    Ans: HOST=$(hostname)

  25. Which of the variable is valid to use in shell script?

    Ans: var@ 2.var-1 3.1var 4.VAR 5.$VAR Ans: 4

  26. Differentiate between $@ and $*.

    Ans: $* - consider all arguments as single entry $@ - consider all arguments as seperate entry

    Example: for i in "$@"

  27. How to redirect and append both stdout and stderr to a file with Bash?

    Ans: file.txt 2>&1

  28. A while loop is a loop that repeats a series of commands for as long as a given condition is ___

    Ans: True

  29. Difference between if-then and case statement?

    Ans: if-then looks for the matching criteria and case gives you the list of options to choose from

  30. In what ways, shell script get input values from a user or terminal?

    Ans: a. By reading command: read -p "Your name" name b. By parametets: ./script param1 param2

  31. How to check whether a link is a hard one or a soft link?

    Ans: We can use -h and -L operators of the test command to check whether a link is hard or soft (symbolic link)

  32. How to debug a shell script?

    Ans: By setting -x in the script we can enable deubugging.

  33. To run ascrot, what typpe of permission ascript should have?

    Ans: Executable permisssion need to be provided to script. We can use 'chmod' command for this.

  34. Is it possible to run multiple scripts at a time? If yes then how?

    Ans: To run multiple scripts at a time, we can invoke name of the scripts in an another script.

    Like script1, script2 can be used in script3

  35. What are Metacharacters in a shell?

    Ans: Special character which provide information about other character. Example:*

  36. What are loops in shell? What are the types?

    Ans: Loops are used to repeatedly run a portion of scipt for a given no, of repetition or until condition is met

    for loop, while loop, until loop

  37. Which of the following are used to end a if-else statement in a Bash script?

    Ans: done 2. fi 3. exit 4.end Ans: fi

  38. How to define an infinite loop in a Bash script?

    Ans: while true

  39. How to use expressions in a script?

    Ans: We can use 'let' Example: let i++

  40. How to get name of the current script and print within a script?

    Ans: We can use ${0} within our script which will print the name of our current running script

  41. What are functions in shell scripting?

    Ans: Block of code which perform some task and run when it is called.

    Can be reuse many times in our program which lessen our lines of code.

    We can pass arguments to the functions.

  42. How to make a basic function in shell scripting?

    Ans: function myfun{ echo "Hello" }

    myfun(){}

  43. How to print a random integer in a shell scripting?

    Ans: We can use built in bash variable called RANDOM which will print random no. between 0-32767

  44. In a shell script, what will you do if you don't want to print the output of a command on the terminal and also in a file?

    Ans: We can redirect the output to /dev/null

  45. What can be used to create delay between 2 executions?

    Ans: We can use sleep command like: sleep 1s sleep 1m

  46. How to stop a script in based on a given condition? Which keyword can be used to stop a script.

    Ans: Using exit command

  47. What is the use of logger in a shell scripting?

    Ans: It is used to maintain logs for your script. We can find the logs under /var/logs/messages

  48. What is the use exit status $? in a shell scripting?

  49. How to check whether a link is a hard one or a soft link? We can use -h and -L operators of the test command to check whether a link is hard or soft (symbolic link)

  50. To run a script, what type of permission a script should have and how will you provide it?

    Executable permission need to be provided to script. We can use 'chmod' command for this.

    Is it possible to run multiple scripts at a time? If yes then how?

    To run multiple scripts at a time, wr can invoke name of the scipts in an another script. Like script1, script2 can be used in script3

    What are metacharacters in a shell?

    Special character which provide inforamtion about other character eg: *

    What are loops in shell? what are the types?

    Loops are used to repeatedly run a portion of script for a given no, of repetition or until condition is met.

    for loop, while loop, until loop

    With the help of Maven, Gradle or ANT we can create .jar, .war and .ear files

  51. Divisible by 3, divisible by 5, not 3*5=15

    for i in {1..100};do

    if ([ expr $i % 3 == 0 ] || [expre $i % 5 == 0]) && [ expr $i %15 != 0]; then echo $i fi; done

  52. How to check current default permission?

    Ans: unmask Permsisson will be (Permission = 0777 - 0022 = 0755 unmask -S Syntax of unmask (unmask u+rw,g+w,o+wx)

    To change default permission permanenetly Add the permission in .bashrc file.

  53. What is SSH?

    Ans: SSH is also command in Linux. It is used to access an another Linux server or accessing a Linux server from a terminal Syntax: ssh user_name@host(IP/Domain_name)

  54. How to use? To use SSH, ssh service must be installed on Linux server. You can check using (rpm -qa | grep ssh) or check the file (/etc/ssh/sshd_config)

    If not already installed then install below two (install openssh-clients openssh-server and now enable the ssh or check if it is enabled using (systemctl status sshd)

    Examples:

    • Access Linux server using Putty

    • Access a Linux server from another server

Permissions: (rwx levels) u - yourself g - group in which user belongs to o - others a - all

  1. What are wildcards? (A wildcard is a character that can be used as a substitute for any of a class of characters in a search.

    • zero or more characters ? - single character [] - range of character
  2. List some of the commonly used shell commands?

    Ans: ls, cp, mv, mkdir, touch, vim, grep, netcat route

  3. Write a simple shell script to list all processes

  4. Write a script to print only errors from a remote log

    Ans: curl pipe and grep

  5. Write a shell script to print numbers divided by 3 & 5 and not 15

    Ans: Divisble by 3, Divisible by 5, And not by 15

    for i in {1..100}; do

    if ([ expr $i % 3 == 0 ] || [expr $i % 5\==0]) && [`expr &i % 15` !=0];

    then

    echo $i

    fi;

    done

  6. How do you debug a shell script?

    Ans: set -x

  7. What is crontab in Linux? Can you provide an example of usage?

    Ans: Crontab -> Linux admin; Report -> node health

  8. How to open a read only file?

    Ans: vim -r test.txt