As you prepare for your Linux interview, you will be in confusion as to which questions the employer is going to ask you. Don't worry! Here are some popular Linux interview questions to get you through.
From the command shell, use the command for memory usage information :
% cat /proc/meminfo MemTotal: 16250912 kB MemFree: 3281056 kB MemAvailable: 10404492 kB Buffers: 1101852 kB Cached: 4654684 kB SwapCached: 129304 kB Active: 7930860 kB Inactive: 2892144 kB Active(anon): 4118480 kB Inactive(anon): 1197660 kB Active(file): 3812380 kB Inactive(file): 1694484 kB Unevictable: 236 kB
There are other commands also which gives memory info :
free - m vmstat top htop
To list every occurrence of the term “warn” on a separate line,
run grep -o warn <path>. Adding the r flag to the command makes recursive search for every file under the given path, and the I flag ensures that matches in binary files are ignored.
In addition, the w flag can be included to match the exact term only, and ignore superstrings such as “warnings”, and to make the search case-insensitive, the i flag can be added.
% grep -iworI warn | wc -l 12
In Linux, Curl command is a tool to transfer data from or to a server. In Linux, curl command is used to test an application's endpoint or connectivity to an upstream service endpoint. It is used to determine if the application can reach another service, like a database, or to check if the service is up and running . This command doesn’t require any user interaction.
$ curl -I -s application:5000 HTTP/1.0 500 INTERNAL SERVER ERROR
This example with an exception shows that the application can't reach server. Options used In the above command,
-I option -> shows the header information -s option -> silences the response body.
Curl command with -O option is used to download the file :
curl -O http://knowledgehut.com/myfile.tar.gz #Save the file as tar,gz
|Hard link||Soft link|
|Hard link associates two (or more) filenames with an inode.||Soft link is a special file type which points to another file and the contents of this special file is the name of the file that it points to.|
|Hard links all share the same disk data blocks while functioning as independent directory entries.||Soft links are created by the - ln -s command.|
|Hard links may not span disk partitions since inode numbers are only unique within a given device.||Once a file which is pointed to by a symbolic link is deleted, the link still points to it, leaving a hanging link|
|Command to create a hard link to ‘knowledgehut’ is: |
$ ln knowledgehut hlink
|Command to create a symbolic link to ‘knowledgehut’ :|
$ ln -s knowledgehut slink
SED command in UNIX stands for stream editor, which is used to make changes to file content.
It can be used to find and replace strings or patterns without opening a file
The default behavior is that the SED command replaces the first occurrence of a pattern in each and it won’t replace the second, third or multiple occurrence in the line.
If we use the ‘g’ option along with the above command then SED command will replace all Unix strings with Linux globally ( g stands for globally) .
sed 's/unix/linux/g' sample.txt
sed '/^$/d' sample.txt
Here “^” symbol represents the starting point of a line and “$” represents the end of the line. Whereas “^$” represents the empty lines , d stands for delete .
Top is the command used to get the list of running processes and resource utilization (RAM or CPU usage). It gives all the information about each process running on the host :
Sample Output :
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7629 greys 20 0 749m 291m 28m S 1 7.4 16:51.40 firefox
19935 greys 20 0 133m 14m 10m S 0 0.4 2:38.52 smplayer
1 root 20 0 4020 880 592 S 0 0.0 0:00.96 init
2 root 15 -5 0 0 0 S 0 0.0 0:00.00 khutreadd
3 root RT -5 0 0 0 S 0 0.0 0:00.04 datamigration/0
4 root 15 -5 0 0 0 S 0 0.0 0:00.90 ksoftirqd/0
5 root RT -5 0 0 0 S 0 0.0 0:00.00 watchdog/0
6 root RT -5 0 0 0 S 0 0.0 0:00.06 datamigration/1
Most commonly used options with the top command are below –
top -u -> Process by a user. top – i -> exclude idle tasks top -p -> Show a particular process
lsof - ‘LiSt Open Files’ is used to find out which files are opened or are in use .
# lsof -u kunand
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1838 kunand mem REG 253,0 122436 190247 /lib/libselinux.so.1
sshd 1838 kunand mem REG 253,0 255968 190256 /lib/libgssapi_krb5.so.2.2
sshd 1838 kunand mem REG 253,0 874580 190255 /lib/libkrb5.so.3.3
Zombie process is a process whose execution is completed but have not been removed from the process table.
When a program forks and the child finishes before the parent , kernel has some of the child information .
In case parent needs to check for child's exit status - parent calls 'wait()'. Child is said to be a zombie process in the duration child terminating and the parent calling 'wait()'.
Execute the below command
ps aux | grep Z
child will have a 'Z' in its status field to indicate zombie process
Also this command will give details of all zombie processes in the processes table.
As zombie processes are already dead , the user Cannot kill something which is already dead.
Execute the below command :
kill -s SIGCHLD pid
Replace the pid with Parent process ID, so that parent process will remove all the child processes that are dead.
This can be checked by using du command ( disk usage)
du –sh . | grep G - lists all the directory which has GIGS in Size.
$ du –sh . | grep G
Below commands can be used :
nslookup - to find the IP address from a hostname or vice-versa.
ipconfig or ifconfig - based on whether the host is Windows or Unix
hostname -i - on Linux
touch command is used to create an empty filename
$ touch knowledgehut.txt
Also for existing files or directories, touch command changes the last access time to current time.
|Head command is used to display first few lines (default is 10) of the file.||Tail is used to display last few lines(default is 10) of the file .|
#Display first 10 lines of the file - application.log
#Display first 50 lines of the file - application.log
head -50 application.log
#Display last 10 lines of the file - application.log
#Display last 50 lines of the file - application.log
tail -50 application.log
This can be done using 'sed' command :
# Here 'p' to print and '-n' to not print each line
sed -n 10,20p input.txt > output.txt
This can be done using wc command (word count)-
#Number of lines
$ wc -l knowledgehut.txt 4 knowledgehut.txt
#Number of words
$ wc -w knowledgehut.txt 3 knowledgehut.txt
#Number of characters
$ wc -m knowledgehut.txt 19 knowledgehut.txt
Aliases are abbreviated shortcuts used to represent a command or a group of commands executed with or without custom options.
#Alias for log directory
alias logs="cd /user/application/logs"
These aliases can be put in the ~/.bash_aliases file.
To have the aliased command on any existing terminal, user needs to source ~/.bashrc from the terminal :
Below are the system calls used for Process management:
fork () :- create a child process
exec() :- Execute a new program
wait():- wait until the process finishes execution
exit():- Exit from the process
getpid():- get the unique process id of the process
getppid():- get the parent process unique id
nice():- run a program with modified scheduling priority
LILO is a boot loader for Linux. LILO stands for Linux Loader that is used to load Linux into memory.
It is used mainly to load the Linux operating system into main memory so as to begin operation.
Lilo handles some tasks such as locating the kernel, identifying other supporting programs, load memory and starts the kernel. The configuration file of lilo is located at “/etc/lilo.conf”. Lilo reads this configuration file and it tells Lilo where to place the bootloader.
find –perm option is used to find files based on permissions
Here "." or period denotes the current directory
$ find . -perm 777
|Unix is an Operating System and Linux is mainly a Kernel for Linux Based OS.||Linux is a Unix-Like based OS, it means the core functionalities behind is similar to UNIX OS.|
|Unix is an operating system having some common command as that of Linux.||Linux is an operating system having some common command as that of Unix.|
|Unix uses Command Line Interface.||Linux uses Graphical User Interface with an optional Command Line Interface.|
|Unix is mainly used in Server Systems, Mainframes and High-End Computers||Linux is mainly used in Home Based PC, Mobile Phones, Desktops, etc.|
|Unix has a rigid requirement of the Hardware. Hence, cannot be installed on every other machine.||Linux is very flexible and can be installed on most of the Home Based Pcs.|
|Different Versions of Unix are: AIS, HP-UX, BSD, Iris, etc.||Different Versions of Linux are: Ubuntu, Debian, OpenSuse, Redhat, Solaris, etc.|
|Both Unix are written in C & Assembly language.||Both Linux are written in C & Assembly language.|
Type any one of the following command to find os name and version in Linux:
cat /etc/os-release lsb_release -a hostnamectl
% hostnamectl Static hostname: WDFL41000139D Icon name: computer-vm Chassis: vm Machine ID: bfc98d9a56631ccde8f8578d58347195 Boot ID: dc39baafe82849b39413507cfd395b54 Virtualization: microsoft Operating System: SUSE Linux Enterprise Server 12 SP2 CPE OS Name: cpe:/o:suse:sles:12:sp2 Kernel: Linux 4.4.121-92.98-default Architecture: x86-64 %lsb_release -a LSB Version: n/a Distributor ID: SUSE Description: SUSE Linux Enterprise Server 12 SP2 Release: 12.2 Codename: n/a
Also type the following command to find Linux kernel version:
ps (i.e., process status) command is used to provide information about the currently running processes, including their PIDs (process identification numbers). A process is a running instance of a program. Every process is assigned a unique PID by the.
$ ps -ef $ ps -ef | grep tomcat
All the files and directories in Linux have below 3 permissions, represented by a three digit octal value .-
Read - It provides the ability to read the contents of a file (represented by 'r' in the first position "r--")
Write - It Provides the ability to edit or delete the content of a file (represented by 'r' in the second position "-w-")
Execute - It Provides the ability to execute the file (represented by 'x' in the third position "--x")
The octal value is calculated as the sum of the permissions:
“read” is 4
“write” is 2
“execute” is 1