Sunday 5 July 2020

How to create and delete a directory in Linux?


51. How to create and delete a directory in Linux?

To create a directory use the mkdir command as shown;
mkdir directory_name

For example to create a directory called 'data' run the command:
# mkdir data

To delete a directory in Linux, use either the rm or rmdir command.
The rmdir command is used for removing empty directories.
The rm command is mostly used with the -R flag for recursively removing directories.

52. How to list and mount devices in Linux?

To list mount points run the command:
# df  -aTh

To find more information about the mount points on your system, execute the command:
# findmnt

Additionally, you can use the cat command below
# cat /proc/self/mounts

Also, you can use the mount command as shown
# mount -l

53. How to schedule a task in Linux? What is crontab and explain the fields in a crontab?

The cron is a deamon that executes commands at specific dates and times in Linux. You can use this to schedule activities, either as one-time events or as recurring tasks. Crontab is the program used to install, deinstall or list the tables used to drive the cron daemon in a server. Each user can have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly. 

Here are few of the command line options for crontab.

# crontab -e #Edit your crontab file.
# crontab -l #Show your crontab file.
# crontab -r #Remove your crontab file.

Traditional cron format consists of six fields separated by white spaces:

<Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week> <command/program to execute>

The format is explained as follows:

* * * * * *
| | | | | |
| | | | | +-- Year (range: 1900-3000)
| | | | +---- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month (range: 1-31)
| +---------- Hour (range: 0-23)
+------------ Minute (range: 0-59)

54. What are the basic Vim commands you know?

a) Save
To save a file, press ESC button and press :wq! OR :ZZ

b) Exit
To exit from a file without making changes, run the command :q

c) Jump to a particular line in a file
Press ESC and press j to move down by one line.
To move up by one line press k on the keyboard
Move the cursor to the beginning of a line Press ^
Move the cursor to the end of a line Press $
Move the cursor to the beginning of a file 1G
Move the cursor to the end of a file G
Moves the cursor to the start of line number “n” in the file nG

d) Copy text
Move the cursor to the beginning of the string or text. Next hit v on your keyboard and press cursor forward to highlight text. Once you get to the end of text that you wish to copy, hit y short for yank, to copy the text.
To copy text from current position to the end of the line - y$
To copy the entire line - yy
To copy 4 lines below - 4yy

e) Paste text
To paste text, simply press p on the keyboard

f) Delete text
To delete a line, move to the beginning of a line. Press the ESC button and press dd
To delete a single word, place the cursor in front of the word and hit dw
To delete text from the current word to the end of the line hit d$
To delete 3 lines below run 3dd

55. Explain are different process states in Linux?

When we execute “ps aux”  in Linux terminal console, we can see multiple states of processes running in the system under the STAT column.

R: Process is running with CPU or waiting for CPU (Running or Runnable)

S: Process is waiting for the set event to complete, Like an input from terminal (SLEEP)

D: Process is with uninterruptible sleep stats which cannot be changed or killed \ rollback. The only way to go away is the reboot system.

Z: Process in Zombie status means the process is already killed but process information and data still exist in the process table.

T: Process either completed or terminated by the operating system or user. This is also known as Terminated /  Completed.

56. What are inodes in Linux? How to find the inode associated with a file?

The inode (index node) is a data structure in a Unix-style file system that describes a file-system object such as a file or a directory. When a file is created, it is assigned both a name and an inode number, which is an integer that is unique within the filesystem.

Explanation,
File systems have two parts: the metadata or the “data” about the data and the data. Metadata consist of information about the data. It includes information such as the Access Control List (ACL), the date the file was modified, file owner, file permissions, size of file, device ID, uid of the file, etc. inodes store this metadata information and typically they also store information about where the data is located on the storage media.

In a file system, inodes consist roughly of 1% of the total disk space, whether it is a whole storage unit (hard disk, thumb drive, etc.) or a partition on a storage unit. The inode space is used to track the files stored on the hard disk. The inode entries only points to these structures rather than storing the data. Each entry is 128 bytes in size. Space for inodes is allocated when the operating system or a new file system is installed and when it does its initial structuring. So this way we can see that in a file system, the maximum number of inodes and hence maximum number of files are set. Now, the above concept brings up another interesting fact. 

A file system can run out of space in two ways:

No space for adding new data is left
All the inodes are consumed.

To get a listing of an inode number, use ls -i command.

# ls -li or # ls -li file_name 

# find /root -inum inode_number

57. Explain file permission in Linux?

In Linux, there are 3 main types of file permissions: read, write and execute. These permissions can be assigned to either a file or a directory recursively.  Being a multi-user system, you can assign these permissions to the root user, groups or even to other users using the system.

Read: The read permission grant uses the ability to open and read a file.

Write: Write permission allows a user to open and modify or edit the file's contents and save the changes.

Execute: This allows a user to execute or run the file or a program or shell script which is executable.

Decimal Permission Representation

0 No Permission - - -
1 Execute - - x
2 Write - w -
3 Write + Execute - w x
4 Read r - -
5 Read + Execute r - x
6 Read + Write r w –
7 Read + Write + Execute r w x

58. What basics measures could you take to secure an ssh connection? For Linux users, it is frequent to access servers by ssh. But are we sure that the communication established is really good secured?

Linux has secured shell services used to connect a Linux system in a secured manner. SSH is is the most common tool for a system administrator for better system management and security. SSH gives some advance features that need proper knowledge and expertise to use. SSH gives more features to the user.

Steps to secure ssh services as below:-

1. Disable/enable Root User
2. Reset Root user password
3. Disable password-based login and enable key-based logins
4. Change standard ports of the system and assign random ports
5. Restricted network access from the system
6. Use a white list approach to allow only authorized IPs to connect

59. Describe what happens when you run the rm command?

The rm command removes a filename from a directory list, decrements the link count of the corresponding i-node by 1, and, if the link count thereby falls to 0, deallocates the i-node and the data blocks to which it refers.

60. What is a process?

A process is an instance of an executing program. When a program is executed, the kernel loads the code of the program into virtual memory, allocates space for program variables, and sets up kernel bookkeeping data structures to record various information (such as process ID, termination status, user IDs, and group IDs) about the process. From a kernel point of view, processes are the entities among which the kernel must share the various resources of the computer.

61. What are the logically divided parts of a process?

A process is logically divided into the following parts, known as segments:
* Text: the read-only machine-language instructions of the program run by the process.
* Data: initialized/uninitialized global and static variables used by the program;
* Heap: an area from which memory (for variables) can be dynamically allocated at run time. The top end of the heap is called the program break;
* Stack: a piece of memory that grows and shrinks as functions are called and return and that is used to allocate storage for local variables and function call linkage information;

62. How are threads different from processes?

Like processes, threads are a mechanism that permits an application to perform multiple tasks concurrently. A single process can contain multiple threads. All threads are independently executing the same program, and they all share the same global memory, including the initialized data, uninitialized data, and heap segments.

Sharing information between threads is easy and fast. It is just a matter of copying data into shared (global or heap) variables. However, in order to avoid the problems that can occur when multiple threads try to update the same information, we must employ some synchronization techniques.
Thread creation is faster than process creation—typically, ten times faster or better. On Linux, threads are implemented using the clone() system call.

63.  What is a Signal in Linux, and what signal is invoked when you use the kill command? What is the difference between kill and kill -9?

A. A signal is a limited form of inter-process communication used in Unix, Unix-like, and other POSIX-compliant operating systems. It is an asynchronous notification sent to a process or to a specific thread within the same process in order to notify it of an event that occurred. When a signal is sent, the operating system interrupts the target process's normal flow of execution.

The difference between invoking kill with no signal specified (which uses SIGTERM, number 15) and kill -9 is that the latter tries to kill the process without consideration to open files and resources in use.

64. How do you debug a running process or a library that is being called?

# strace -p PID
# ltrace libraryfile

65. How to see a memory map of a process, along with how much memory a process uses?

# pmap -x PID

66. What is Huge Pages in Linux and what use is there for them?

Hugepages is a mechanism that allows the Linux kernel to utilize the multiple page size capabilities of modern hardware architectures. Linux uses pages as the basic unit of memory, where physical memory is partitioned and accessed using the basic page unit. The default page size is 4096 Bytes in the x86 architecture. Hugepages allows large amounts of memory to be utilized with a reduced overhead.

To check: # cat /proc/sys/vm/nr_hugepages.
To set: # echo 5 > /proc/sys/vm/nr_hugepages

67. What symbol represents that ACLs have been set on a file?

+

68. How would you continuously display memory usage every 5 seconds?

# free -s 5

69. How to stop a running process in Linux?

To stop a running process, use the kill command followed by the PID of the process.

to stop a process with PID 3836, run the command
# kill 3836

70. What is the difference between name based virtual hosting and IP based virtual hosting?

Virtual hosts are used to host multiple domains on a single apache instance. You can have one virtual host for each IP your server has, or the same IP but different ports, or the same IP, the same port but different host names. The latter is called "name based vhosts".

On IP-based virtual hosting, we can run more than one web site on the same server machine, but each web site has its own IP address while In Name-based virtual hosting, we host multiple websites on the same IP address. But for this to succeed, you have to put more than one DNS record for your IP address in the DNS database.

71. What is the advantage of Network Bonding?

Network Bonding(also known as NIC Teaming) is a Linux kernel feature that allows aggregating multiple network interfaces into a single virtual link. This is a great way to achieve redundant links, fault tolerance or load balancing networks in the production system. If one of the physical NIC is down or unplugged, it will automatically move traffic to the other NIC card. Similar way the bonding will increase the interface throughput to handle the traffic it is configured in active-active mode.

72. Where is password file located in Linux and how can you improve the security of password file?

User information along with the passwords in Linux is stored in /etc/passwd that is a compatible format. But this file is used to get the user information by several tools. Here, security is at risk. So, we have to make it secured.
To improve the security of the password file, instead of using a compatible format we can use shadow password format. So, in shadow password format, the password will be stored as single “x” character which is not the same file (/etc/passwd). This information is stored in another file instead with a file name /etc/shadow. So, to enhance the security, the file is made word readable and also, this file is readable only by the root user. Thus security risks are overcome to a great extent by using the shadow password format.

73. Explain all the fields in the/etc/passwd file?

/etc/passwd file contains the useful information for all the system users who log in. Many fields in /etc/passwd file such as username, password, user ID, group ID, comment or user ID info, home directory, command /shell, etc. So, this file contains sensitive information regarding all the user accounts. There is a single line per user in this file. Colon (:) separates the fields in /etc/passwd. 

Below is the explanation of the fields.

Username: First field is the username that contains the username which is 1 to 32 length characters.

Password: This field does not show the actual password as the password is encrypted. Here, x character shows that password is encrypted that is located in /etc/shadow file.

User ID (UID): All the users created in Linux is given a user ID whenever the user is created. UID 0 is fixed and reserved for the root user.

Group ID (GID): This field specifies the name of the group to which the user belongs. The group information is also stored in a file /etc/group.

User ID Info: Here you can add comments and you can add any extra information related to the users like full name, contact number, etc.

Home directory: This field provides the path where the user is directed after the login. For example, /home/smith.

Command/shell: This field provides the path of a command/shell and denotes that user has access to this shell i.e. /bin/bash.

74. What is a Master boot Record and how do you back it up and restore it?

The MBR  is a 512 byte segment on the very first sector of your hard drive composed of three parts: 
 the boot code which is 446 bytes long, 
 the partiton table which is 64 bytes long, and 
 the boot code signature which is 2 bytes long.

To backup: # dd if=/dev/sda of=/tmp/mbr.img_backup bs=512 count=1
To restore: # dd if=/tmp/mbr.img of=/dev/sda bs=512 count=1

75. You are using iSCSI or a virtual machine with attached block device. Due to high IO or network latencies the FS goes in read only mode from time to time. What can you do to increase the write time out on the block device?

To increase the write time out on a block device in real time use the sys fs:
echo 60 > /sys/block/sdk/device/timeout
        
                                                                                           MENU         PREVIOUS | NEXT 

No comments:

Post a Comment