Sunday 5 July 2020

How to create user and group in Linux?


201. How to create user and group in Linux?

To create the user and also create the user's home directory to match the username,
# useradd -m <username>

Issue the command to set password,
# passwd <username>

To create a group
# groupadd <groupname>

To add our new user
# usermod -a -G <groupname> <username>

To know which users are already a member of a group
# grep <groupname> /etc/group

202. Mention the ways to debug the kernel code?

We can debug a kernel code simply with the command printks. Else we can also use KDB and kernel probes. Other methods are:

UML (User Mode Linux) – It is the best method for debugging but it does not support device drivers.

KGDB (Kernel GNU Debugger)

kdump tools which are used to dump kernel cores.

203. How would you swap the stdout and stderr of a command?

$ command 3>&2 2>&1 1>&3
To swap stdout and stderr of a command, a third file descriptor is being created (in this case 3), which is assigned to the same target that stderr is pointed to (referenced by &2). Then stderr is pointed to the same target stdout is pointed to (&1). Finally, stdout is pointed back to where the newly created file descriptor is pointed (which is the same target stderr originally pointed to.)

204. How would you write a shell script that prints all the additional arguments passed to it in reverse order?

for (( i = ${#}; i > 0; i-- )); do
        echo ${!i}
done
The arguments are available as $<n>, where n is the position of the argument. For example, $0 would give the name of the script, $1 would give the first additional argument, $2 the second, and so on. The total number of additional arguments is found in $#.
A loop that starts at $# and ends at 1 can be used to print each additional argument in reverse order.

205. What is the correct path for the grub’s configuration file?

The configuration file (/boot/grub/grub.conf OR /etc/grub2/grub.conf), which is used to create the list of operating systems to boot in GRUB's menu interface, essentially allows the user to select a pre-set group of commands to execute.

206. How to add binary to $PATH variable?

$ export PATH=$PATH:/path/to/the/binary/file

207. Why setup password less ssh login?

To improve system security even further, most of the organizations turned to use key based authentications instead of Password-based authentication. We can enforce the key-based authentication by disabling the standard password authentication, which involves a public key private key pair. The public key is added in the server configuration file while the private key is kept confidential on the client-side.

208. What is Key-based authentication? Explain.

One of the ways to achieve the security is to use Key-based authentication. To use this type of authentication, we have to disable the password-based authentication. So, there is a procedure to set up this authentication which is as follows:

We have to get the SSH key pair using below command:
$ ssh-keygen -t rsa
It will generate the public/private rsa key pair.

Enter file where you want to save this generated key (/home/username/.ssh/id_rsa):
It will prompt you for the same location, i.e. ~/.ssh/id_rsa for the key pair. Press enter if you want to confirm the same location. Else, if you want to provide any other location, enter that and confirm the same.

Now copy ~/.ssh/id_rsa.pub into the ~/.ssh/authorized_keys that will be located where you have to connect.
Now, we have to provide the permissions to the file as per below command:
$ chmod 600 ~/.ssh/authorized_keys

Now try to sshthe machine you want to connect, and you will see that you are able to login to the machine without a password.

If you are confirmed that key-based authentication is working fine, disable the password-based authentication.
Go to the path /etc/ ssh/sshd_config
set the following property as no.
PasswordAuthentication no

209. Explain the logical steps to increase the size of LVM partition?

Some logical steps need to be followed to increase the size of LVM partition. 

These are as follows:

Run the command as per given format:
lvextend -L +500M /dev/<Name of the LVM Partition>
Here, we are extending the size of LVM partition by 500MB.

resize2fs /dev/<Name of the LVM Partition>
You can check the size of partition using ‘df -h’ command

210. Which utility is to create a partition from the raw disk?

To create the partition from the raw disk, you have to use fdisk utility. Below are the steps to create a partition from the raw disk:

Step 1: Run the below command:
fdisk  /dev/hd* (IDE) or /dev/sd* (SCSI)

Step 2: Type n to create a new partition

Step 3: Now partition has been created, and we have to write the changes to the partition table, so type w command to write the changes.

211. If a volume group already exists and we need to extend the volume group to some extent. How will you achieve this?

Linux provide the facility to increase the size of a volume group even if it already exists. For this, we need to run a command.

First of all, we have to create a physical volume (/dev/sda1)
Size of the physical volume should be the size you want the size of the logical volume.

Now, run the below command:
vgextend VG1 /dev/sda1

Here VG1 is the name of the volume group.

212. How we can enable ACL?

ACL is an acronym for Access Control List which is used to provide flexible permission mechanism for the file systems. We can enable ACL by following methods:
Type the code in the shell: /etc/fstab with a label=/home/ext3 acl

Now we have to remount this file system with the ACL partition: mount –t ext3 –o acl /dev/sda3/home

213. What are the possible methods to deploy a module inside a kernel?

To check the modules that are already installed inside the kernel, you have to run this code: lsmod. When the module has been built, now it is the stage to load it in the kernel. You can load it by the command “Insmod” or “Modprobe”.

Syntax: Insmod[filename][module-options] //module-options are command line arguments to kernel objects.

Insmod always accepts only one filename at a time.'

Modprobe offers more features than Insmod like it can decide which module is to be loaded and is aware of the module dependencies.

214. Mention the case when we use “user virtual address” instead of “kernel virtual address”?

When we run a program in userspace then we use “user virtual address” as we do not have any access to kernel virtual memory address. Normally when we are running our program in kernel mode then we use kernel address but in case we have to run our program in kernel mode and that program needs an interaction with a userspace then we will use “user virtual address” and be careful to first translate it to user virtual address.

215. How would you write a shell script and ensure that only one instance of the script may run for every user? Strong atomicity is not required.

In Bash:
LOCKFILE=/tmp/lock-`whoami`
if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
    echo "Already running!"
    exit 1
fi
trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
echo $$ > ${LOCKFILE}

Start by determining a name for the lock file. In this case, the lock file is generated by suffixing a common name with the username of the current user.

Then, check if the lock file exists and if the PID contained within the lock file is running. If it is, exit with a message.

Create a trap to remove the lock file on a clean exit, or unclean exits (any exit with the signal INT or TERM).

Finally, if the script has not exited yet, create the lock file, and store the PID of the current process ($$) in it.

216. What are terminal multiplexers? What are some of their key features? What are some of the more popular ones currently available?

Terminal multiplexers enable several terminals to be created and controlled from a single screen or from a single remote session. The terminals and sessions can be detached and left running, even with the user logging off.

Two of the more common ones available today are GNU Screen and tmux.

Screen enables you to connect to multiple remote servers without needing to open multiple terminal shells. Work can be preserved and a session detached, for example, to wait for the output of a long-running command. On subsequent reconnection, users can reattach to existing sessions or run new sessions. Sessions can also be shared among different users, which may be useful in audit or training scenarios.

Both Screen and tmux support split-screen functionality (to be more precise, tmux supports this and Screen supports it via a plugin). This allows, for example, runningtail on a service’s log file in one part of the screen, and editing the configuration of that service, and restarting it if necessary, in another.

217. What is a Linux null (or Blackhole) route? How can it be used to mitigate unwanted incoming connections?

A Linux null (or Blackhole) route is a type of routing table entry which, upon matching a packet, discards it without forwarding the packet any further or sending any ICMP.

Using this technique, it is possible to block an IP (or range of IP addresses) by running a simple command. For example, blocking 192.168.0.1 can simply be done with the following command:
# ip route add blackhole 192.168.0.1/32 

218. How to add a null route?

In our example we are receiving unwanted SSH login attempts from 192.168.0.195
# netstat -na | grep :22
 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
 tcp 0 0 192.168.0.197:22 192.168.0.195:57776 ESTABLISHED

To add the null route we will use the ip command
# ip route add blackhole 192.168.0.195/32

To verify the route is in place will will use ip route show
# ip route show
 default via 192.168.0.1 dev eth0 metric 100
 blackhole 192.168.0.195

After a little while the established ssh connections will time out and all subsequent connections from the blocked ip will receive the following.
 baduser@attacker:~$ ssh 192.168.0.197
 ssh: connect to host 192.168.0.197 port 22: No route to host

219. How to remove a null route?

After the attack has subsided or in case you add the wrong ip you may want to remove the blackhole route. To do so we will use the ip command again.
# ip route del 192.168.0.195
# ip route show
 default via 192.168.0.1 dev eth0 metric 100

220. what are the contents of /usr/local?

/usr/local comprises files installed locally. This is an important directory in the environment where files have to keep in a network. Typically, files locally-installed go to /usr/local/lib, /usr/local/bin, etc.). Another application of directory is its usage for software packages, or software not shipped formally with the distribution.

221. Name some major Stateless Linux Server’s features?

Some of the major features of Stateless Linux Server are as follow:
1. It stores every system’s prototype.
2. It stores the home directories.
3. It stores the snapshot was taken. 
4. It uses LDAP that holds information concerning which snapshot run on which system. 

222. How to Calculate Load Average in Linux?

The load average is calculated through the decimal number in Linux, starting from 0.00. The load average suggests the number of programs waiting for their turn to be run on the system.  The status would be one-minute average, five-minute average, and fifteen minutes average.
223. how to color the Git console?

To color the Git console, you can use the command git config—global color.ui auto. In the command, the color.ui variable sets the default value for a variable such as color.diff and color.grep.

224. What is the steps if the remote server is not connecting?

Ping remote server IP. (to check  alive or not)
Make sure remote server sshd service enabled
If everything is ok, login remote server ILO, open the remote console and check the status

225. Tell me the difference between ext3 and ext2 file systems?

Some of the major differences between the ext3 and ext2 file systems are as follow:

The ext3 file system is a higher form of the ext2 file system.

After an unexpected system crash or power failure (a system shutdown), ext2 file system should be analyzed for constancy through the e2fsck program. It is a time-taking procedure and through this process, information on the volumes is inaccessible.

ext3 supports journaling, it is one of the major differences among both ext2 and ext3.

The journaling offered by the ext3 file system indicates that this type of file system check is not essential to perform after an unclean system shutdown. A consistency check only occurs in rare hardware failure while using ext3, for example, hard drive failures. after an unclean system shutdown, the recovering time of an ext3 file system not based on the file system size or the number of files. But it is based on the journal size used to preserve constancy. The default size of the journal takes a couple of seconds to recover, based on the hardware speed.

                                                                                           MENU         PREVIOUS | NEXT


No comments:

Post a Comment