Linux Flashcards
What is Linux?
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
What is Linux Distribution?
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.
What is Linux kernel?
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.
What is SSH?
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.
What is PuTTY?
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.
What is ‘/’?
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.
What is /bin?
It is the directory where essential command binary and other executable programs are kept for all users.
For eg. cat, ls, cp.
What is /boot?
It is the directory where bootloader files are kept.
For eg. kernel, initrd
What is /dev?
It is the directory where device files are kept.
For eg. /dev/disk0, /dev/sda1
What is /etc?
It is the directory where host specific system-wide configuration files are kept. These configuration files control how the OS/application should behave.
What is /home?
It is users’ home directory and contains saved files, personal settings etc.
What is /lib?
It is the library folder for binaries in /bin and /sbin.
What is /media?
It is the mount point for removable media such as CD-ROM.
What is /mnt?
It is the directory where temporarily mounted filesystems are stored.
What is /opt?
It is the directory where optional/ third party application software packages, not provided by distro by default, are kept.
For eg. Google Earth
What is /proc?
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.
What is /root?
It is the home directory for root user.
What is /sbin?
It contains essential system binaries that require root permissions.
For eg. fsck, init, route.
What is /usr?
User System Resources (usr) is the secondary hierarchy for read-only user data and contains the majority of user utilities and applications.
What is /usr/bin?
It contains non-essential command binaries for all users.
What is /usr/sbin?
It contains non-essential system binaries that require root permission.
For eg. daemons for various network-services.
What is /var?
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
What is the difference between
/bin
/sbin
/usr/bin
/usr/sbin?
/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.
What is the difference between / and /root?
/ is the root directory where the file hierarchy starts.
/root is the home directory for root user and is inside the root directory ( / ).
What is shell?
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 can we know if we have logged in as root user?
The root user shell prompt ends with # where as for normal user it is $.
[ user@linuxsvr ~ ] $ is normal user
[ user@linuxsvr ~ ] # is root user
What does the ~ sign in shell prompt mean?
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
Explain these commands:
ls cd pwd cat tac echo man exit clear
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
Explain the ls command and its options.
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).
What is $PATH?
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.
What does ‘which’ command do?
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.
What does the . and .. mean?
. means current directory
.. means parent directory of current directory
What does ‘cd’ (without any arguments) and ‘cd -‘ mean?
cd without any arguments takes us to our home directory.
cd - takes us to the previous directory we were in.
How to create a directory?
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 to remove a directory?
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 to delete the directory with all the files inside it?
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.
What are the hidden files?
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.
What does the tree command do?
It provides recursive listing of directory contents in a tree structure.
Explain file/directory permissions.
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
Explain chmod.
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
Explain chgrp.
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.
Explain file creation mask.
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.
Explain ‘find’ command.
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
Explain ‘locate’ command.
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.
What is the difference between ‘more’, ‘less’ and ‘most’ commands?
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 to copy a file from source to destination?
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