Linux Flashcards

1
Q

What is Linux?

A

Linux is an OS based on Unix, which is a collection of software that manages hardware resources and provides an environment where applications run.

Linux has 3 important components:
kernel
system library
system utility

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Linux Distribution?

A

It is a collection of software and different applications bundled together and distributed as a single operating system.

For eg, one Linux distribution might come with one particular web browser while another one might support different one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Linux kernel?

A

Linux Kernel is the core of Linux OS and a layer that sits between system’s hardware and its processes. It communicates between the two, managing the resources.

Kernel has 4 jobs:

  • Memory management
  • Process management
  • Device drivers
  • System calls and security

It uses Linux Kernel API through which user programs interact with kernel.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is SSH?

A

Secure SHell (SSH) is a network protocol and it allows us to connect from one system to another securely over network.

It provides a secure channel over an unsecured network by using client-server architecture, connecting an SSH client application with an SSH server.

Standard port for SSH is 22.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is PuTTY?

A

PuTTY is an open-source terminal emulator, console and network file transfer application. Originally written for Windows, it is now a popular client for SSH connections.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is ‘/’?

A

Also known as root directory, it is the top of the entire file system hierarchy in Linux.

In Windows, we have multiple root directories such as C:, D: etc but Linux allows only one root directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is /bin?

A

It is the directory where essential command binary and other executable programs are kept for all users.

For eg. cat, ls, cp.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is /boot?

A

It is the directory where bootloader files are kept.

For eg. kernel, initrd

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is /dev?

A

It is the directory where device files are kept.

For eg. /dev/disk0, /dev/sda1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is /etc?

A

It is the directory where host specific system-wide configuration files are kept. These configuration files control how the OS/application should behave.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is /home?

A

It is users’ home directory and contains saved files, personal settings etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is /lib?

A

It is the library folder for binaries in /bin and /sbin.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is /media?

A

It is the mount point for removable media such as CD-ROM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is /mnt?

A

It is the directory where temporarily mounted filesystems are stored.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is /opt?

A

It is the directory where optional/ third party application software packages, not provided by distro by default, are kept.

For eg. Google Earth

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is /proc?

A

This directory contains virtual filesystem providing process and kernel information as files. This directory is generally automatically generated and populated by the system, on the fly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is /root?

A

It is the home directory for root user.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is /sbin?

A

It contains essential system binaries that require root permissions.

For eg. fsck, init, route.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is /usr?

A

User System Resources (usr) is the secondary hierarchy for read-only user data and contains the majority of user utilities and applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is /usr/bin?

A

It contains non-essential command binaries for all users.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is /usr/sbin?

A

It contains non-essential system binaries that require root permission.

For eg. daemons for various network-services.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is /var?

A

This directory contains variable files, files whose content is expected to change continually during normal operation of the system.

For eg, log files, spool files, temporary email files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the difference between

/bin
/sbin
/usr/bin
/usr/sbin?

A

/bin contains binaries usable before /usr partition is mounted. Hence this is used for trivial binaries need to be available in booting single-user mode, such as cat, ls etc.

/sbin is same as /bin but for binaries with root permission required, such as fsck, init.

/usr/bin is same as /bin but for general system-wide binaries.

/usr/sbin is same as /usr/bin but for binaries with root permission required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the difference between / and /root?

A

/ is the root directory where the file hierarchy starts.

/root is the home directory for root user and is inside the root directory ( / ).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is shell?

A

Shell is the interface in OS that takes commands from user (via keyboard) and gives them to OS to perform.

It is named so because its the outermost layer around OS.

There are two types of shell:
CLI
GUI

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How can we know if we have logged in as root user?

A

The root user shell prompt ends with # where as for normal user it is $.

[ user@linuxsvr ~ ] $ is normal user

[ user@linuxsvr ~ ] # is root user

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What does the ~ sign in shell prompt mean?

A

It means that we are currently in the home directory of user.

[ john@linuxsvr ~ ] $ means we are inside /home/john.

We can also go to a user’s home directory or a service’s (who typically doesn’t have home directory) own account by typing ~ followed by user/service name.

~jason will take us to /home/jason

~ftp will take us to /srv/ftp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Explain these commands:

ls
cd
pwd
cat
tac
echo
man
exit
clear
A

ls: lists directory contents

cd:
(1) without argument: takes us to current user’s home directroy
(2) cd - : takes us to previous directory where we were
(3) cd /some/dir : takes us to mentioned directory

pwd: displays present working directory
cat: concatenates and displays file contents
tac: displays the file contents in reverse order
echo: displays arguments to the screen
man: displays manual pages for command provided as argument
exit: exits the shell prompt/current session
clear: clears the screen

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Explain the ls command and its options.

A

The ls command lists the contents of directory given as an argument or contents of current directory if no arguments are given.

The -l option displays directory content in a long listing format that includes file permissions, owner and group names, created and modified timestamps, file sizes etc.

The -a option displays all files, even the hidden files (starting with .)

The -r option reverses the order.

The -t option sorts by modification time, newer first.

The -F option displays / character for directories, @ for links and * for executables in their names.

The -R option recursively lists the directory content (files inside subdirectories).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is $PATH?

A

It is an environment variable that controls the command search path. It contains colon-separated list of directories where a particular command is searched.

For eg. when we type ‘ls’, it will be searched in the directories mentioned in $PATH and executed, if found. If not found, it gives a command not found error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What does ‘which’ command do?

A

It provides the directory (mentioned in $PATH) where the command (supplied as argument) exists.

If the command exists in multiple directories, then whichever directory is encountered first in $PATH is displayed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What does the . and .. mean?

A

. means current directory

.. means parent directory of current directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What does ‘cd’ (without any arguments) and ‘cd -‘ mean?

A

cd without any arguments takes us to our home directory.

cd - takes us to the previous directory we were in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

How to create a directory?

A

mkdir one: will create a directory ‘one’

mkdir one two three: will create three different directories ‘one’, ‘two’ and ‘three’

mkdir -p one/two : will create a parent directory ‘one’, and inside that, it will create another directory ‘two’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

How to remove a directory?

A

rmdir pictures will remove the empty directory pictures.

If the directory is not empty, it will give an error and not delete the directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

How to delete the directory with all the files inside it?

A

rm -rf pictures will delete the files inside directory pictures and then delete the empty directory.

rm is the remove command.

The -r option removes the contents recursively (deletes content inside content)

The -f option removes the contents forcefully, without giving any prompt.

The -i option will provide a prompt asking user if the file should be deleted, for every file inside the director.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

What are the hidden files?

A

The system files, not meant to be used by normal user are kept hiddem so that they don’t accidentally get deleted.

Their names start with ‘.’ and they are not seen in the ls command’s output by default.

To see these files, we need to use ‘-a’ option with ls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

What does the tree command do?

A

It provides recursive listing of directory contents in a tree structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Explain file/directory permissions.

A

There are 10 bits in the file permissions.

The first character tells us if it is a directory (d), regular file ( - ) or symbolic link ( l ).

The next 9 bits are arranged into three groups, containing three bits each. The first group denotes owner permissions (u), second group denotes group permissions (g) and third one is for permissions to rest of the users (o) not falling into either owner or group category.

The characters in these three groups are r, w, x (in given order) and mean read, write and execute respectively.

For a file,
read permission means one can read file contents
write permission means one can write to the file
execute permission means one can execute the file

For a directory,

  • read permission means one can read file names in directory
  • write permission means one can modify entries within directory (create/rename/delete files within directory) if execute permissions are set.
  • execute permission means one can enter the directory (cd into it), access files and directories inside

There are 4 permission categories:

u: owner
g: group
o: others
a: all three categories

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Explain chmod.

A

change mode (chmod) command changes file/directory permissions.

It uses

  • four categories: u, g, o, a
  • operands: + (add), -(remove), =(set)
  • permissions: r,w,x

chmod g+x sales.data: will add write permission to group owning sales.data file

chmod g=x sales.data: will remove all other permissions of group owning sales.data file and set the permission to write only.

chmod o= sales.data: will remove all permissions from others

chmod u+x, g-r sales.data: will add execute permission to owner and remove read permission from group

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Explain chgrp.

A

change group (chgrp) command changes the group owning the file to given group.

chgrp sales records.data: will change the group of file records.data to sales.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

Explain file creation mask.

A

File creation masks determine the default permissions for files when they are created.

The base permissions for directories are 777 and for files are 666. If the mask is 002, then for a file, 666-002= 664 will be the default permission.

This mask can be seen by ‘umask’ command and can also be changed. umask 007 will change the mask to 007.

Had the umask been 007, the for file it would be 660 (because 666-007 isn’t exactly 660).

The fourth bit in umask is special mode and can be setuid, setgid or sticky bit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

Explain ‘find’ command.

A

The find command recursively finds files in path that match the pattern. If no arguments are provided then it lists all files in given directory.

find [path] [option pattern] is the syntax and we can also provide various options to it.

find one/ -name “peter” will find files and directories in directory one matching the name to peter.

find one/ -iname “peter” is same as -name but it ignores cases.

find one/ -mtime 10 will find files that are 10 days old

find one/ -size +1M will find files that are of size greater than 1MB

find one/ -newer sales.data will find files newer than sales.data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

Explain ‘locate’ command.

A

The locate command lists files that match given pattern, by not matching each and every file with the pattern but by querying the system database and is faster than find command.

It checks the files in the system database and hence, may not provide real-time results, such as file created a minute ago will not exist in database until the system database refresh job runs.

To refresh the database, ‘updatedb’ command can be run but it needs root permissions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What is the difference between ‘more’, ‘less’ and ‘most’ commands?

A

All three are terminal pagers or paging programs used to view the contents of large text file, that are displayed page by page.

‘more’: is basic and oldest pager. One can scroll through the pages by pressing space bar to move forward and ‘b’ to move backward.

‘less’: is advanced than ‘more’ and allows scrolling and search in forward and backward direction, can go to the file start and end immediately.

To scroll in forward/backward direction, press space bar/b.

To search in forward direction: /searchterm
To search in backward direction: ?searchterm

To move to previous/next match: N/n

‘most’: is advanced than ‘less’, it can do everything ‘less’ does but with multiple files by allowing us to switch between multiple files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

How to copy a file from source to destination?

A

cp source_file destination_directory: will copy the file

cp file1 file2 file3 dir: will copy files1,2,3 to dir

cp -r dir1 dir2: will recursively copy all the contents of dir1 to dir2 (dir1 inside dir2). If the dir2 doesn’t exist, it will create it and then copy the contents.

cp file1 file2: will copy the file1 and rename the copied version as file2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

How to move and rename files?

A

mv source destination

if source is file and destination is directory, file will be moved to destination

if source and destination are both files or directories, source will be renamed destination.

48
Q

How to display disk usage?

A

du: estimates file usage

du -k : displays size in KB

du -h: displays size in human-readable format (2M instead of 2136457)

49
Q

What is wildcard?

A

It is a character/string used for pattern matching. Globbing expands the wildcard pattern into a list of files/directories.

  • matches zero or more characters
    ? matches exactly one character
    [abc] matches exactly one character given in bracket
    [!3-5] matches exactly one character not in the range given in bracket
50
Q

What is a character class?

A

The pattern provided in square brackets used for matching file/directory names is called character class. It matches exactly one character from the given set of characters.

There are some pre-defined character classes:

[:alpha:] is same as [a-zA-Z]
[:alnum:] is same as [a-zA-Z0-9]
[:digit:] is same as [0-9]
[:space:] for matching whitespace, tab, newline, carriage return
[:lower:] is same as [a-z]
[:upper:] is same as [A-Z]
51
Q

What is globbing?

A

When the wildcards/character classes are provided for pattern matching, bash carries out a filename expansion process called globbing.

Globbing interprets the standard wild card characters and expands them.

52
Q

What is setuid?

A

setuid which stands for set user ID on execution, is a special type of file permission in Linux and permits the users to run certain programs with the permissions set to that of owner, instead of setting it to the user who launched it.

For eg., when user wants to change his/her password, passwd command is run. The passwd program is owned by root and its setuid is set, hence the user executes the program with root permissions.

When we do ‘ls -l filename’, we can see the file permissions. Presence of ‘s’ in owner permissions instead of ‘x’ suggests that owner has execution permission and setuid is set for given file.

We can set setuid permission with command
‘chmod u+s filename’.

When the owner does not have execute permission on file and if setuid bit is set, it is seen as uppercase S.
rwSrw-r– means that owner does not have execution permission. In this case, setting setuid has no effect.

setuid bit has no effect on directories.

53
Q

What is setgid?

A

setgid which stands for set group ID on execution, is a special type of file/directory permission in Linux.

When a file has setgid bit set, when executed, instead of running with the privileges of the group of users who launched it, runs with those of the group who owns the file.

When a directory has setgid bit set, the files in that directory will have the same group as the group of parent directory, and not that of the user who created those files. This is used for file sharing since they can be modified by all users who are part of the group of parent directory.

When we do ‘ls -ld dirname’, we can see the directory permissions. Presence of ‘s’ in group permissions instead of ‘x’ suggests that group has execution permission and setgid is set for given file.

We can set setgid permission with command
‘chmod g+s dirname’.

54
Q

What is sticky bit?

A

Sticky bit is the special type of permissions for directory (does not affect files).

When the directory has sticky bit set, the files inside the directory can be deleted/renamed/modified only by file owners, directory owners and root user.

For eg., /tmp directory is writable by all users on the system, but to avoid the situation where one user can modify/delete files created by another user, sticky bit is set on /tmp directory.

When we do ‘ls -ld dirname’, we can see the directory permissions. Presence of ‘t’ in others’ permissions instead of ‘x’ suggests that others have execution permission and sticky bit is set for given directory.

We can set sticky bit with command
‘chmod o+t dirname’.

55
Q

How to match file that end with question mark (or any character that itself is a wildcard character?)

A

We can escape the character with ‘'.

*\? will match all the files that end with ?

56
Q

What will these commands do?

ls ?

ls ??

A

ls ? will list all the files having exactly one character in their names

ls ?? will list all the files having exactly two characters in their names

57
Q

What are file descriptors?

A

When a file is opened on Linux system, the OS creates an entry in the global file table to represent that file and store its information.

The File Descriptor (FD) is the unique non-negative entry number that identifies the open files.

In *nix systems, the first three FDs are reserved for STDIN(0), STDOUT(1) and STDERR(2)

58
Q

Explain STDIN, STDOUT and STDERR.

A

These are the three data streams created when a Linux command is launched. Data stream is something that can transfer data.

STDIN is the default data stream for input. This defaults to keyboard input from user unless redirected input is received.

STDOUT is the default data stream for output, such as command printing text. This defaults to user’s screen unless redirected to a file.

STDERR relates to error occurring. This defaults to user’s screen as well unless redirected to a file.

59
Q

Explain the data redirection in Linux.

A

Redirection is a feature in Linux such that when executing a command, you can change the standard input/output devices.

> redirects standard output to given file by overwriting existing contents of file.
For eg. cat homer.txt > marge.txt
will overwrite marge.txt with homer.txt’s contents

> > redirects standard output to given file by appending to existing contents
For eg. cat homer.txt&raquo_space; marge.txt
will append homer.txt’s content to marge.txt’s content

< redirects input from a file to a command
For eg. cat < homer.txt will display contents of homer.txt on screen.

& is used as redirection operand.
For eg. ls a.txt b.txt > out.txt 2>&1
will redirect STDERR (2) to STDOUT (&1) which is actually set to out.txt. Hence both error and output will go to out.txt

60
Q

What is Unix/Linux bit bucket?

A

Bit bucket is the way of referring to /dev/null, which means null device, which throws away anything fed to it.

61
Q

What will this line do?

sort < files.txt > sorted_files.txt

A

It will first execute “sort < files.txt” where lines inside files.txt will be sorted.

This output is fed to sorted_files.txt

62
Q

How to redirect output to one file and error to another file in single command?

A

ls fileOne fileTwo 1> out.txt 2>err.txt

63
Q

How to compare the contents of two files?

A

diff file1 file2
will compare two files

sdiff file1 file2
will display side-by-side comparison

vimdiff file1 file2
will highlight the differences in vim editor

64
Q

Explain the output of diff command.

A

diff file1 file2 will compare two files’ contents and give following output:

The first number(s) represent line numbers from file1 where content is different

followed by either A(add), C(change) or D(Delete)

followed by number(s) representing line numbers from file2 where content is different.

65
Q

Explain grep command.

A

grep pattern filename

will search the pattern in given file ‘filename’ and display lines containing that pattern.

  • i will perform case insensitive search
  • c will only count the number of occurrences in file
  • n will precede the output will line numbers
  • v will perform invert match and print lines that don’t match

Performing ‘grep’ over binary/non-text file, we will know whether the pattern exists in file or not, but it will not display the lines. To do that, we must use ‘strings’ command which pulls out the textual bits and then supply it to grep command.

66
Q

How to display file type?

A

With ‘file’ command.

file filename: will display the file type.

file homer or file homer.txt will give ASCII text as output.

67
Q

How to find a text pattern in binary/non-text files such that the output is properly displayed on screen?

A

With ‘strings’ command.

For eg. strings jabber | grep pattern

will extract the text bits from file ‘jabber’ and supply them to grep command.

68
Q

Explain the piping concept.

A

The vertical bar ( | ) is called pipe and is used for redirection. It is used mostly to supply one command’s output to another command as input.

cat homer | grep pattern
is same as grep pattern homer and will supply lines of file homer to grep command to find pattern.

69
Q

Explain cut command.

A

The cut command allows us to select portions of a file or to cut out pieces of a file.

  • d option allows us to specify the delimiter
  • f option tells which field to display

cut -d ‘ ‘ -f 2 homer
will display 2nd field in file homer, delimited by whitespace.

cut -d: -f1,5 homer
will display fields 1 and 5 in file homer, delimited by colon.

70
Q

Explain tr command.

A

tr command is used to translate one character to another and print the resulting output.

cat homer | tr “:” “ “
will replace : with whitespace in file homer

cat homer | tr [:lower:] [:upper:]
will capitalize every lowercase letter

cat homer | tr -d ‘ ‘
will delete the whitespaces in file homer

71
Q

Explain column command.

A

column command format the input into multiple columns.

  • s option specifies the delimiter for input (default is two whitespaces)
  • o option specified the column delimiter for output (default is two whitespaces)
  • t option prints the output in table format
72
Q

Explain scp and sftp command.

A

secure copy (scp) is the copy command used for copying files over the network and it works similar to cp command, except that scp does not send login credentials over network in plain text and encrypts uploaded/downloaded files.

secure FTP (sftp) starts the secure file transfer session with host. For eg. sftp hostname.

73
Q

How can we customize the shell prompt?

A

By modifying the PS1 variable in .bash_profile file.

PS1= [\u@\h \w] $
will display username, followed by @ symbol, followed by hostname, followed by present working directory.

Outside of brackets, it will display if the logged-in user is root/normal user.

74
Q

Explain alias command.

A

alias command is used to reduce the long/most used commands to short versions. It can be also used to fix frequent typos/to make one system behave like other (such as aliasing Windows cls to clear command).

Just typing ‘alias’ will give the list of currently shortened commands on the system.

We can create our own aliases by typing
alias name=’value’
and placing it inside .bash_profile file for persistence.

and remove it by typing
unalias name

75
Q

How can we create/update/remove/view the environment variables set for system?

A

printenv: will list all EVs

printenv HOME: will give value of HOME variable

echo $HOME: will work same as above.

We can create/update an EV with command:
export VAR_NAME=”value”
without any whitespaces around =.

We can remove an EV with command:
unset VAR_NAME

76
Q

Explain ps command.

A

ps command displays process status in tabular format which shows PID of process, terminal from which the process is being executed, time and command being executed.

  • e option shows all process, even the ones not owned by logged in user
  • f option provides full format listing

‘-u john’ displays process’s owned by john

‘-p 9536’ displays process whose PID is 9536

‘-H’ options displays process tree

’–forest’ works same as ‘-H’

Another similar command pstree displays processes and their children in tree format.

77
Q

Explain pstree command.

A

It is very similar to ps but instead of listing the processes it shows them in a tree.

With its structure, it is easy to know which processes are parents and which ones are children. This way we can terminate the proper parent.

pstree will also display the threads by default, which ps command won’t do, unless supplied with ‘-L’ option.

78
Q

Explain top command.

A

It provides the real-time view of the processor activity of system and tasks managed by kernel.

It shows information like tasks, memory, cpu etc.

To sort the table, hit Shift+O, which displays another menu to choose from fields to sort on.
To sort by CPU utilization, hit Shift+P

top -u john will display processes specific to user john.

By default, the screen refresh rate is 3 seconds which can be changed by pressing ‘d’ option, which displays a line ‘change delay from 3.0 to: ‘, where we can input our customized time in seconds.

We can also kill processes without exiting the top screen, by pressing ‘k’ and giving the PID.

79
Q

How to start a process in background?

A

By writing ‘&’ at the end of a command and then executing it.

80
Q

How to kill and suspend the process in foreground?

A

ctrl+c to kill a process in foreground

ctrl+z to suspend a process in foreground

81
Q

How to move a foreground process to background?

A

ctrl+z to first suspend the process

followed by typing bg.

82
Q

How to move any process
from foreground to background
and
from background to foreground?

A

from foreground to background
bg PID

from background to foreground
fg PID

83
Q

Explain the kill command.

A

The kill command is used to exit a process and different signals can be passed to signify how the process should exit.

The basic syntax is:
kill PID or
kill -s sigName PID or
kill -sigName PID or
kill -sigNum PID

The default signal is SIGTERM or TERM for short and is numbered 15.

sigName is the symbolic signal name specifying the signal to be sent instead of default SIGTERM.

sigNum is non-negative integer, specifying the signal to be sent instead of default 15.

-l or -L option lists the available kill signals to choose from.

If the PID is a positive integer, then the process whose PID is same as mentioned PID is killed.

If the PID is 0, all the processes whose PGID is equal to PGID of user are killed, except the kill process itself and init process.

If the PID is -1, all the processes are killed, except the kill process itself and init process.

If the PID is negative but not -1, all the processes whose PGID is equal to the absolute value of PID mentioned are killed. The negative ID identifies the whole group.

84
Q

What are the different kill commands available in Linux?

A

(1) SIGHUP: hangs up the process as connecting terminal is closed
(2) SIGINT: interrupts the process
(9) SIGKILL: kills the process
(15) SIGTERM: terminates the process

(17, 19, 23) SIGSTOP: pauses the process

(18) SIGCONT: continues the previously stopped process

85
Q

What is the difference between kill signals?

A

SIGKILL and SIGSTOP cannot be blocked, ignored or handled whereas SIGTERM can be.

With SIGTERM, the process is given a chance to clean up before exiting. SIGKILL kills the process immediately, with no clean up.

86
Q

What is the difference between Unix and Linux?

A

Unix is the proprietary OS from Bell Labs whereas Linux is free, open-source OS based on Unix.

87
Q

What is BASH?

A

Bourne Again Shell (bash) is widely used shell in Linux, based on its previous version Bourne Shell (bsh).

It is represented as /bin/bash.

88
Q

What is terminal?

A

It is a program that acts as a medium for input and output operations for Linux.

It is the GUI used for providing commands to the system.

89
Q

What is root account/root user?

A

It is the system administrator account in Linux, which allows full access of the system in all modes (single/multi user).

It is a default account when Linux is installed and has UID 0.

The home directory of root user is /root and not / (which is root directory).

90
Q

Explain the chown command.

A

It is used to change the owner of a file and is typically done with the help of su or sudo command.

With su, we need to type root user’s password wheras with sudo, we need to type our own password.

chown john hello.txt will change the owner to john.

chown john: salesGroup hello.txt will change the owner to john and group ownership to salesGroup.

91
Q

What is the maximum length allowed for a filename in Linux?

A

The filename can be maximum 255 character long, excluding the pathname.

The pathname can be maximum 4096 character long.

92
Q

Is Linux case sensitive?

A

Yes.

ls is different than LS.

93
Q

What are the three timestamps a file has?

A

Access time (atime) is when a file was last accessed/read by some command or application such as cat, vim, grep.

Modification time (mtime) is when a file was last modified/created.

Change time (ctime) is when a file content or metadata (permission/ownership/location) was changed. ctime updates when touch command is used on a file.

We can change an existing file’s
atime with command ‘touch -a hello.txt’
mtime with command ‘touch -m hello.txt’
ctime with command ‘touch -c hello.txt’

When changing mtime, ctime will also be changed.

We can view all these times with command stat.
For eg. stat homer
will show all the times related to homer.

94
Q

How to create a file with different timestamp other than current one?

A
  1. touch -d ‘1 June 2018 11:02’ file1
    will change the atime and mtime of file1 to date and time mentioned in string.

The date string needs to be enclosed in quotes.
We can also give partial date such as ‘1 June’, it will change the year to current year.

  1. touch -t 06011102 file1
    will change the atime and mtime of file1 to 1 June of current year and time to 11:02.

We can give the timestamp in this format:
[[CC] YY] MMDDhhmm[.ss]

  1. touch -r file1 file2
    will use the timestamp of file1 for file2.
95
Q

How to change the timestamp of a symbolic link?

A

By default, it we use a touch command on a symbolic link, it will change the timestamps of its referenced file.

To modify the timestamp of symlink, below command is used:
touch -h symlink1

96
Q

What are the two numbers displayed when a job is run in background?

A

When a job is run in background by appending the command with &, two numbers are displayed:

The first one in square brackets is the job number
The second one is PID.

The job number can be referenced by preceeding it with % sign.

A process can be checked with command ‘ps -p PID’, in the same way, a job can be checked with command ‘jobs %jobnumber’. The current job can be represented as %% or %+.

We can see all jobs currently running, with command ‘jobs’.

The + sign in job number represents current job and - sign represents previous job.

97
Q

What is cron and crontab?

A

cron is the time-based job scheduling service in Linux.

crontabl, shortly crontab is the program to create/read/update/delete a routine background task at specific times. It uses cron service to schedule and automate the tasks.

Crontab entries can be
viewed using crontab -l command
edited/added to using crontab -e command
removed using crontab -r command (deletes all cron jobs).

crontab myjobs.txt installs a new crontab from file myjobs.txt

98
Q

How can we schedule a job using crontab?

A

The basic syntax is
* * * * * command_to_be_executed

There are 5 fields for specifying the day/time for a task to run:

min:0-59
hour:0-23
day of month: 1-31
month:1-12
day of week: 0-6 with Sunday as 0th day

For eg. 30 08 10 06 * /home/john/backup
will run the backup task at 8.30am on the 10the day of 6th month (10th June).

0 7 * * 1 /home/john/backup
will run the backup task at 7am every Monday

0,15,30,45 * * * * /opt/acme/15-min-check
will run every 15 minutes

*/15 * * * *   /opt/acme/15-min-check
will also run every 15 minutes

0-4 * * * * /opt/acme/first-five-minutes
will run for first five minutes of every hour every day every month.

There are also some shortcuts for scheduling jobs:
@yearly/@annually is same as 0 0 1 1 *
@monthly is same as 0 0 1 * *
@weekly is same as 0 0 * * 0
@daily/@midnight is same as 0 0 * * *
@hourly is same as 0 * * * *
99
Q

Explain the su command.

A

switch user (su) command is used to change to mentioned user or to become root user (if no username is mentioned). It asks us the password of this new user, which we must know.

su john: changes to user john keeping same environment as that of original user

su - john: changes to user john, changing the environment to john’s.

su: is similar to su root
- (hyphen) is used provide an environment similar to what the user would expect has the user logged in directly.

If we don’t use hyphen, the environment variables from original user will show up and new user can use them.

If we want to execute a command by switching to new user, we can use -c option.
For eg, su -c /home/john/backup - john

100
Q

What is the difference between these commands:

su john

su - john

A

If a user jane performs ‘su john’, jane’s environment will be available for use when the shell changes to john’s.

With su - john, john’s environment will be seen, not jane’s.

The hyphen switches from the current directory to the home directory of new user by logging in as that user.

This is incredibly useful when root user perform su command. Without hyphen, root user’s environment will be accessible to new user which is undesirable.

101
Q

Explain the sudo command.

A

super user do(sudo) executes a command as another user, typically the superuser.

Similar to su command, if no username is specified, sudo command switches the user to root user.

With sudo, we don’t need to know the password of other user, unlike su. sudo prompts us for our own password and this stops password sharing.

‘sudo -l’ lists the commands available for current user to run as sudo.

We can modify the sudo configuration with command ‘visudo’, which opens the file /etc/sudoers in vim editor, if current user has permissions to do so.

‘sudo command’ runs command as root user.

‘sudo -u root command’ runs command as root user.

‘sudo -u john command’ runs command as john.

‘sudo su’ switches to superuser account

‘sudo su -‘ switches to superuser account with root’s environment

‘sudo su - john’ switches to john with john’s environment.

‘sudo -s’ starts a shell as root user

‘sudo -u root -s’ is same as ‘sudo -s’

‘sudo -u john -s’ starts the shell as john

102
Q

Explain the /etc/sudoers file format.

A

The basic format is this:

user host=(users)[NOPASSWD:]commands

john ALL=(ALL) ALL
says the user john should be able to run all commands on all hosts as all users

john ALL=(ALL) [NOPASSWD:] ALL
says the user john should be able to run all commands on all hosts as all users without being prompted for password.

john linuxsvr=(root) /etc/init.d/oracle
says the user john should be able to run command ‘/etc/init.d/oracle’ as root user on host linuxsvr.

103
Q

How to know which commands have been run in past?

A

With command ‘history’, we can display the shell history, which accomodates number of commands specified in HISTSIZE variable.

104
Q

What does below commands mean?

!3

!!

!homer

A

!3 repeats command numbered 3 in history file

!! repeats previous command

!homer repeats the most recent command starting with ‘homer’

105
Q

What does below commands mean?

head files.txt sorted_files.txt notes.txt

vi !^
vi !:2
vi !$

A

The ! points to previous command and
^ points to 1st argument in previous command.
:2 points to 2nd argument in previous command.
$ points to last argument in previous command.

Hence
vi !^ is same as vi files.txt
vi !:2 is same as vi sorted_file.txt
vi !$ is same as vi notes.txt

106
Q

Explain the yum command.

A

Yellodog Updater, Modified (yum) is free, open-source command-line package management utility tool which uses Redhat Packet Manager (RPM), installs, delets, queries and manages software packages for Linux.

  1. yum search string
    searches for string package
  2. yum info [package]
    displays package info if package name is provided. Otherwise provides details of all packages installed in system.
  3. yum install [-y] package
    installs given package. With -y option, all the intermediate questions are answered ‘yes’ automatically.
  4. yum remove package
    removes given package.

Installing and removing packages using yum requires root permissions.

107
Q

What is rpm?

A

Redhat Package Manager (rpm) is a package management utility, similar to yum which is mostly used to install packages not directly available for yum to install.

rpm -ivh package

  • i installs given package
  • v gives verbal details
  • h displays the installation process completion in hashes

rpm -qa
queries all the packages and lists them

108
Q

Explain Linux boot process.

A

There are 6 steps in Linux Boot process:

  1. BIOS
  2. MBR
  3. GRUB
  4. Kernel
  5. Init
  6. Runlevel programs
109
Q

Explain BIOS.

A

Basic Input Output System (BIOS) is a special type of firmware and is the first piece of software that runs when system is powered on.

BIOS is tasked with

  • performing system integrity checks
  • checking various external devices that are connected
  • locating and loading the system boot program in memory

The BIOS performs POST which stands for Power On Self Test used for performing integrity checks.

BIOS contains a list of boot devices such as hard disk, DVD drive, USB device etc. The BIOS searches that list for a bootable device in the order specified. This order can be changed by interrupting the boot sequence and entering into the configuration for the BIOS.

BIOS is now replaced by UEFI (Unified Extensible Firmware Interface). Once BIOS is complete, the control is transferred to MBR.

110
Q

Explain MBR.

A

Master Boot Records (MBR) locates and loads the bootloader (LILO/GRUB).

MBR is located in the first sector of bootable disk and is usually less than 512 bytes with three components:

First 446 bytes: primary bootloader information
Next 64 bytes: partition information
last 2 bytes: validation checks

111
Q

Explain GRUB.

A

GRand Unified Bootloader (grub) is responsible for starting the OS, technically, loading a specific kernel image from multiple images.

If there are multiple kernel images, GRUB provides a splash screen, waiting for few seconds where we can provide our choice of kernel. If no choice is provided, GRUB moves ahead with default kernel image specified in /etc/default/grub file.

GRUB has replaces original LILO (Linux Loader) as the latter didn’t have knowledge of file systems.

112
Q

Explain kernel step of Linux boot loading.

A

Kernel mounts the root file system in memory by executing /sbin/init program. The Linux kernel is typically named vmlinux or vmlinuz (if compressed).

InitRD stands for Initial RAM Disk and acts as temporary file system till Kernel can boot and load the real file system.

Since Init is the first program to be executed by kernel, its PID is 1.

InitRD contains necessary drivers and hard drive partition information.

113
Q

Explain Init step of Linux boot loading.

A

It executes the runlevel specified in /etc/inittab file to decide Linux’s operational mode. Nowadays, systemd is being used to change the runlevel.

There are 7 runlevels:

0: halt (shutdown)
1, S, s: single-user mode (for administrative purposes)
2: multi-user mode (without Network File System)
3: multi-user mode (with Network File System)
4: unused
5: start the system normally with file system and GUI manager
6: reboot

The default runlevels are either 3 or 5.

114
Q

Explain disk partitioning.

A

When a disk is divided into parts, its called disk partitioning.

It allows separation of data, prevents one part of system from adversely affecting another part of system and prevents a user from filling up the entire disk.

115
Q

What is a mount point?

A

A mount point is simply a directory that is used to access data on a partition.

For eg. if a partition is allocated for user home directories, that partition would be mounted at /home. The files are directories that are at/below the /home mount point will reside on that partition.

/home/john is on partition mounted on /home.