TechSchool
Home / Interview questions / Linux

Linux interview questions

45 questions about Linux with short answers and code. Each answer links to a full lesson on this site. Free, with no signup and no ads.

Basics

1. What is Linux?

Linux is a free and open-source operating system. It is based on Unix ideas and it runs on many kinds of hardware.

Full lesson: Linux tutorial

2. Who wrote the first Linux kernel?

Linus Torvalds wrote the first Linux kernel in 1991. He wrote it as a hobby while he studied at the University of Helsinki.

Full lesson: Linux intro

3. What is a Linux distribution?

A Linux distribution is a package of the Linux kernel and application software. The distribution gives you a ready system to install and use.

Full lesson: Linux intro

4. What is the shell?

The shell is a program that gives you an interface to the kernel. It is both a command language and a programming language.

Full lesson: Linux shell

5. How do you define a shell variable?

Write the variable name, then an equals sign, then the value. Do not put spaces around the equals sign.

your_name="example"

Full lesson: Linux shell variable

6. How do you run a shell script?

First make the script executable. Then run it with its path.

chmod +x ./test.sh
./test.sh

Full lesson: Linux shell

7. How do you print text with the echo command?

Give echo the text that you want to print. The shell writes the text to the screen.

echo "Hello Linux"

Full lesson: Linux shell echo

8. How do you create an array in the shell?

Put the values in parentheses and separate them with spaces. Array subscripts start from 0.

my_array=(A B "C" D)

Full lesson: Linux shell array

9. How do you find out what type a command is?

Use the type command with the command name. It tells you if the command is a built-in, an alias, a function, or an external program.

type ls

Full lesson: Linux comm type

10. How do you compare two text files?

Use the diff command with the two file names. It compares the files line by line and shows the differences.

diff file1.txt file2.txt

Full lesson: Linux comm diff

11. How do you check the type of a file?

Use the file command with the file name. It prints the file type.

file report.txt

Full lesson: Linux comm file

12. How do you check disk space usage?

Use the df command. The -h option shows the sizes in a human-readable form.

df -h

Full lesson: Linux filesystem

13. How do you activate a network interface?

Use the ifup command with the interface name. It reads the configuration file and sets the network parameters.

sudo ifup eth0

Full lesson: Linux comm ifup

14. How do you compare three files at the same time?

Use the diff3 command with the three file names. It shows the differences among all three files.

diff3 my.txt orig.txt other.txt

Full lesson: Linux comm diff3

15. Why does the command var = "value" fail in the shell?

The shell reads the spaces around the equals sign as separate words. Write the assignment with no spaces.

var="value"

Full lesson: Linux shell variable

Intermediate

16. How do you show the value of a shell variable?

Put a dollar sign before the variable name. You can also wrap the name in braces.

name="Linux"
echo $name
echo ${name}

Full lesson: Linux shell variable

17. How do you read the contents of an MS-DOS file in Linux?

Use the mtype command with the file name. The -t option converts MS-DOS line endings to Linux line endings.

mtype -t dos.txt

Full lesson: Linux comm mtype

18. How do you define a shell function?

Write the function name, then a pair of parentheses, then a block in braces. You can add the function keyword before the name.

greet() {
  echo "Hello"
}
greet

Full lesson: Linux shell func

19. How do you test a condition in a shell script?

Use the test command or a pair of square brackets. Put spaces inside the brackets.

if [ -f file.txt ]; then
  echo "File exists"
fi

Full lesson: Linux shell test

20. How do you print formatted output in the shell?

Use the printf command with a format string. printf does not add a newline by itself.

printf "Name: %s\n" "Linux"

Full lesson: Linux shell printf

21. How do you deactivate a network interface?

Use the ifdown command with the interface name. The command reads the interface settings from the configuration file.

ifdown eth0

Full lesson: Linux comm ifdown

22. How do you display or configure a network device?

Use the ifconfig command with the device name. Run it without arguments to show all active devices.

ifconfig eth0

Full lesson: Linux comm ifconfig

23. How do you copy a file and set its permissions in one step?

Use the install command with the source file and the target file. Add options to set the mode and the owner.

install -m 755 app.sh /usr/local/bin/app.sh

Full lesson: Linux comm install

24. How do you show statistics from a diff output?

Pipe the diff output to the diffstat command. diffstat counts the inserted and deleted lines for each file.

diff old.txt new.txt | diffstat

Full lesson: Linux comm diffstat

25. How do you include one shell file in another shell file?

Use the source command or a dot before the file name. The included file runs in the current shell.

. ./config.sh

Full lesson: Linux shell include file

26. How do you use arithmetic and comparison operators in the shell?

Put the expression inside double parentheses for arithmetic. Use the test command for string and number comparisons.

a=5
b=3
if [ $a -gt $b ]; then
  echo "a is larger"
fi

Full lesson: Linux shell basic operators

27. How do you install a package with the yum command?

Run yum install with the package name. Add the -y option to answer yes to all prompts.

yum install -y nginx

Full lesson: Linux yum

28. Why does a shell function return an unexpected value?

A function returns the exit status of its last command when you omit the return statement. Add return with a number from 0 to 255 to set the value.

myfunc() {
  echo "done"
  return 0
}

Full lesson: Linux shell func

29. Why does a test with square brackets fail with a syntax error?

The square brackets need a space after the opening bracket and before the closing bracket. A missing space makes the shell read the bracket as part of a word.

[ -f file.txt ] && echo "ok"

Full lesson: Linux shell test

30. Why does printf output run together on one line?

printf does not add a newline at the end of the output. Add the \n escape sequence to the format string.

printf "line one\nline two\n"

Full lesson: Linux shell printf

Common mistakes

31. How do you open a file in the vi editor?

Use the vi command followed by the file name. The editor opens the file in command mode.

vi example.txt

Full lesson: Linux vim

32. How do you show the users who are logged into the system?

Run the w command. It lists the logged-in users and their current programs.

w

Full lesson: Linux comm w

33. How do you show information about one logged-in user?

Give the user name to the w command. The command shows data for that user only.

w alice

Full lesson: Linux comm w

34. How do you install MySQL on CentOS?

First install the build tools with yum. Then download and compile the MySQL source code.

yum -y install gcc gcc-c++ make autoconf

Full lesson: Mysql install setup

35. How do you install Nginx on CentOS?

First install the build tools and libraries with yum. Then compile Nginx from source.

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

Full lesson: Nginx install setup

36. How do you find a Linux command in the command library?

Open the command library page. It groups commands by category for easy search.

Full lesson: Linux command manual

37. How do you add two numbers in a shell script?

Use the expr command inside backticks. Assign the result to a variable.

val=`expr 2 + 2`
echo "The sum is: $val"

Full lesson: Linux shell basic operators

38. How do you compare two numbers in a shell script?

Use a relational operator with the test command. The -eq operator checks for equality.

if [ $a -eq $b ]; then echo equal; fi

Full lesson: Linux shell basic operators

39. How do you check if a file is a directory in the shell?

Use the -d file test operator with the test command. It returns true if the path is a directory.

if [ -d /tmp ]; then echo yes; fi

Full lesson: Linux shell test

40. How do you check if a string is empty in the shell?

Use the -z string test operator. It returns true when the string has zero length.

if [ -z "$name" ]; then echo empty; fi

Full lesson: Linux shell test

41. Why does the command cd /tmp fail in a shell script?

The cd command runs in a subshell. The parent shell does not change its directory.

Full lesson: Linux shell

42. Why does the command echo $var show nothing?

The variable var is not set in the current shell. Set the variable before you use it.

var=hello
echo $var

Full lesson: Linux shell variable

43. Why does the command if [ $a = $b ] fail when a is empty?

The empty value leaves the test with too few arguments. Quote the variables to keep the test valid.

if [ "$a" = "$b" ]; then echo same; fi

Full lesson: Linux shell test

44. Why does the command source ./lib.sh fail?

The file lib.sh is not in the current directory. Use the correct path or run the command from the right directory.

source ./scripts/lib.sh

Full lesson: Linux shell include file

45. Why does the command yum install fail with a dependency error?

The package needs other packages that are not installed. Use yum to resolve the dependencies automatically.

yum install package_name

Full lesson: Linux yum

More on TechSchool
See the Linux cheat sheet Python JavaScript TypeScript HTML CSS React Vue 3 Node.js Java PHP C C++ C# Go SQL MongoDB Redis Git Docker Data Structures