Thursday, July 22, 2021

Linux interview question and answers.

 Linux

How Will you change default user id value in linux?
id manjeet
vim /etc/login.defs
UID_MIN-

cmd
groupmod -g 600 group01
usermod -u 900 -g 600 user01

Add user in group

usermod -aG groupname username
Remove user from group 
gpasswd -d username groupname 
Delete group
groupdel groupname 



root# rm -rf /tmp/test gives error operation not permitted. Reason?
chattr +i /tmp/test     chattr applied


/etc/hosts (which RPM is reposible for creating this file).
rpm -qf /etc/hosts

what is difference b/w RPM and YUM?
RPM -redhat
rpm -qPR httpd-2.4.6-90.e17.centos.x86_64.rpm

yum install all s/w with dependency


what is difference b/w Hard and Soft Link?
ll -i /root     display inode
ln /tmp/test /etc/lokendra     it do hard link and inode will be same
hard link create only the same disk

ln -s /tmp/test /etc/lokendra    it will create soft link create and it pointing to inode
soft link can create accross the file system

What is sticky bit?
sticky bit implement on the folder or directory, not on file
drwxrwxrwt    prevent to delete unwanted deletion, the owner of a file or the root can delete
chmod 1777 dir
chmod +t dir

How will you check open ports on Linux Server?
netstat -tunlp


How Will you check open ports on remote servers (without login).
nmap

Your site is throwing 500 error, how will you start troubleshooting?
server side issue, like webserver not responding etc

How will you start troubleshooting if your site is down?
server side issue, like webserver not responding etc

How will you create space on disk if it is showing 100% used?
df -Th        Disk space
du -sh *     Check file folder space

What is package of sar cmd and what does it do?
vi /etc/sysconfig/sysstat


What is Swap Space?
Part of disk space can use as RAM for temp hold some programs
less than 2 GB        2 times the amount of RAM        3 times the amount of RAM

2 GB - 8 GB        Equal to the amount of RAM        2 times the amount of RAM

8 GB - 64 GB        0.5 times the amount of RAM        1.5 times the amount of RAM

more than 64 GB        workload dependent            hibernation not recommended

What are the basic components of linux?
Kernal - core component of OS that manage operations and H/W
shell - interpreter which is used to execute cmd
GUI -
System utilities - allows user to manage computer
Application Programs - s/w program or set of fuctions designed to accomplish a specfic task.

How do Enable/disable Eth Device
vi /etc/sysconfig/network-scripts/devicename
For enable ONBOOT = yes
For disable ONBOOT = no

What are the process states in linux?
Ready: created ready to run
Running Being executed
Blocked or wait: Process waiting for input from the user
Terminated or Completed: Process completed execution, or term by OS
Zombie: Process termin, but info still exist in process table
child dies before the parents, in this case structural info still in process table,
Can finish when parent dies, it can clear by kernel.

how to clear zombie process?



Explain each system calls used for process management in linux?
system calls

File related- open(), read(), write(), close(), create()
Device Related- read, write, repostion, ioctl,
Information- getpid, attributes, get system time
Process Control- Load, execute, abort, fork, wait, signa, allocate etc.
Communication- pipe()

Fork() - used to create a new 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() - to bias the existing property of process

what is env variable?
In Linux and Unix based systems environment variables are a set of dynamic named values, stored within the system that are used by applications launched in shells or subshells. In simple words, an environment variable is a variable with a name and an associated value.


There are several commands available that allow you to list and set environment variables in Linux:

$ printenv //displays all the global ENVs
or
$ set //display all the ENVs(global as well as local)
or
$ env //display all the global ENVs

To set a global ENV
$ export NAME=Value
or
$ set NAME=Value

To set user wide ENVs

These variable are set and configured in ~/.bashrc, ~/.bash_profile, ~/.bash_login, ~/.profile
Step 1: Open the terminal.
Step 2:

$ sudo vi ~/.bashrc

Step 3:Enter password.
Step 4:Add variable in the file opened.

export NAME=Value

Step 5: Save and close the file.
Step 6:

$ source ~/.bashrc


To set system wide ENVs
These variable are set and configured in /etc/environment, /etc/profile, /etc/profile.d/, /etc/bash.bashrc files according to the requirement.

Step 1: Open the terminal.
Step 2:

$ sudo -H vi /etc/environment

Step 3:Enter password.
Step 4:Add variable in the file opened.

NAME=Value

Step 5: Save and close the file.
Step 6: Logout and Login again.

Some commonly used ENVs in Linux

$USER: Gives current user's name.
$PATH: Gives search path for commands.
$PWD: Gives the path of present working directory.
$HOME: Gives path of home directory.
$HOSTNAME: Gives name of the host.
$LANG: Gives the default system language.
$EDITOR: Gives default file editor.
$UID: Gives user ID of current user.
$SHELL: Gives location of current user's shell program.


How do you find out all processes that are currently running?

ps -f

How do you find out the processes that are currently running or partcular user?

ps -aux localmanjeet

what is file path of network config?
/etc/sysconfig/network-script

what is file path of DNS config?
/etc/resolv.conf

how to update locate db?
cd /var/lib/mlocate
updatedb
vi /etc/updatedb.conf

what is boot process in linux?

BIOS - So, in simple terms BIOS loads and executes the MBR boot loader.

MBR -  1st sector bootable disk,  
MBR is less than 512 bytes in size,
1) primary boot loader info in 1st 446 bytes
2) partition table info in next 64 bytes
3) mbr validation check in last 2 bytes.
It contains information about GRUB (or LILO in old systems).
So, in simple terms MBR loads and executes the GRUB boot loader.

GRUB
If you have multiple kernel images installed on your system, you can choose which one to be executed.
GRUB displays a splash screen, waits for few seconds, if you don’t enter anything, it loads the default kernel image as specified in the grub configuration file.
GRUB has the knowledge of the filesystem (the older Linux loader LILO didn’t understand filesystem).
Grub configuration file is /boot/grub/grub.conf (/etc/grub.conf is a link to this). The following is sample grub.conf of CentOS.
So, in simple terms GRUB just loads and executes Kernel and initrd images.

Kernel
Mounts the root file system as specified in the “root=” in grub.conf
Kernel executes the /sbin/init program
Since init was the 1st program to be executed by Linux Kernel, it has the process id (PID) of 1. Do a ‘ps -ef | grep init’ and check the pid.
initrd stands for Initial RAM Disk.
initrd is used by kernel as temporary root file system until kernel is booted and the real root file system is mounted. It also contains necessary drivers compiled inside, which helps it to access the hard drive partitions, and other hardware.
Once the kernel has extracted itself, it loads systemd, which is the replacement for the old SysV init program, and turns control over to it.


The startup process

systemd
systemd is the mother of all processes and it is responsible for bringing the Linux host up to a state in which productive work can be done
First, systemd mounts the filesystems as defined by /etc/fstab
/etc/systemd/system/default.target,
For a desktop workstation, this is typically going to be the graphical.target, which is equivalent to runlevel 5 in the old SystemV init
For a server, the default is more likely to be the multi-user.target which is like runlevel 3 in SystemV
The emergency.target is similar to single user mode.


SystemV Runlevel     systemd target         systemd target aliases     Description
              halt.target                       Halts the system without powering it down.

0             poweroff.target     runlevel0.target     Halts the system and turns the power off.

S             emergency.target                   Single user mode. No services are running; filesystems are not mounted. This is the most basic level of operation with only an emergency shell running on the main console for the user to interact with the system.

1             rescue.target         runlevel1.target     A base system including mounting the filesystems with only the most basic services running and a rescue shell on the main console.

2                           runlevel2.target     Multiuser, without NFS but all other non-GUI services running.

3             multi-user.target     runlevel3.target     All services running but command line interface (CLI) only.

4                           runlevel4.target     Unused.

5             graphical.target     runlevel5.target     multi-user with a GUI.

6             reboot.target         runlevel6.target     Reboot

              default.target                       This target is always aliased with a symbolic link to either multi-user.target or graphical.target. systemd always uses the default.target to start the system. The default.target should never be aliased to halt.target, poweroff.target, or reboot.target.

Init
Looks at the /etc/inittab file to decide the Linux run level.
Following are the available run levels

    0 – halt
    1 – Single user mode
    2 – Multiuser, without NFS
    3 – Full multiuser mode
    4 – unused
    5 – X11
    6 – reboot
Typically you would set the default run level to either 3 or 5

Runlevel programs


What is umask?
user file creation mode, when user creates any file, it has default file permissions.

umask u=rwx,g=x,o=
umask 067



What is network bonding in linux?
Network bonding is a process of combining more than 2 network interfaces to form a single network interface.

How to check the default route and routing table?
route -n
netstat -rn
ip

what cmd use for Error checking and Error Fixing?
fsck and efsck

what is LVM?
Logical partition

Physical Volume (PV), - pvs
Volume Group (VG) -vgs
Logical Volume (LV) - lvs

sda, sdb, sdc
fdisk -l

pvcreate /dev/sda1 /dev/sdb1 /dev/sdc1
pvs

# vgcreate -s 32M tecmint_add_vg /dev/sda1 /dev/sdb1 /dev/sdc1
vgs

vgdisplay tecmint_add_vg

lvs

Method 1: Creating Logical Volumes using PE Size’s
vgdisplay tecmint_add_vg

bc
1725PE/3 = 575 PE.
575 PE x 32MB = 18400 --> 18GB

# lvcreate -l (Extend size) -n (name_of_logical_volume) (volume_group)
# lvcreate -l 575 -n tecmint_documents tecmint_add_vg
# lvcreate -l 575 -n tecmint_manager tecmint_add_vg
# lvcreate -l 575 -n tecmint_public tecmint_add_vg

lvs

Method 2: Creating Logical Volumes using GB Size’s
# lvcreate -L 18G -n tecmint_documents tecmint_add_vg

# lvcreate -L 18G -n tecmint_manager tecmint_add_vg

# lvcreate -L 18G -n tecmint_public tecmint_add_vg

# lvcreate -L 17.8G -n tecmint_public tecmint_add_vg

Creating File System
# mkfs.ext4 /dev/tecmint_add_vg/tecmint_documents

# mkfs.ext4 /dev/tecmint_add_vg/tecmint_public

# mkfs.ext4 /dev/tecmint_add_vg/tecmint_manager

Mount
# mount /dev/tecmint_add_vg/tecmint_documents /mnt/tecmint_documents/

# mount /dev/tecmint_add_vg/tecmint_public /mnt/tecmint_public/

# mount /dev/tecmint_add_vg/tecmint_manager /mnt/tecmint_manager/

# df -h

Permanent Mounting
# vim /etc/fstab


Tell me linux boot sequence flow?
BIOS-MBR-BOOT LOADER - KERNAL - RUNLEVEL

what are inbuild firwall in linux?
IP tables

Selinux            - /etc/selinux/config
check selinux status     - getenforce

TCPwrappers
Understanding hosts.allow and hosts.deny
<services> : <clients> [: <option1> : <option2> : ...]

How to Use TCP Wrappers to Restrict Access to Services
To allow SSH and FTP access only to 192.168.0.102 and localhost and deny all others, add these two lines in /etc/hosts.deny:
sshd,vsftpd : ALL
ALL : ALL

and the following line in /etc/hosts.allow:
sshd,vsftpd : 192.168.0.102,LOCAL

To allow all services to hosts where the name contains example.com, add this line in hosts.allow:
ALL : .example.com

and to deny access to vsftpd to machines on 10.0.1.0/24, add this line in hosts.deny:
vsftpd : 10.0.1.

what is called .scratch pad of computer?
Cache memory is scratch pad of computer

How can you append one file to another in linux?
To append
cat file2 >> file1

Append 2 or more file in 1

Find a file using terminal
find . -name "process.txt"
cat file1 file2 > file3  

How to lock user account in linux?

usermod -L testuser
 passwd -l testuser


Lock or disable pass using passwd cmd
Expire the user account using usermod cmd or chage cmd
changing the shell using nologin cmd ( /sbin/nologin )

What is LDAP?

Installing OpenLDAP
yum -y install openldap openldap-servers openldap-clients
systemctl enable slapd

Configuring LDAP
ldappasswd

LDAP terminology
Entry (or object): every unit in LDAP considered an entry.
dn: the entry name.
o: Organization Name.
dc: Domain Component. For example, you can write likegeeks.com like this dc=likegeeks,dc=com.
cn: Common Name like the person name or name of some object.

which configuration file is required for ldap clients?
ldap.conf

what is the name of main configuration file name for ldap server?
sladap.conf

how will you verify ldap configuration file?
slaptest -u

what daemon is responsible for tracking events on you system?
syslogd-

what cmd can use to review boot messages?
dmesg

what is the name and path of the main system log?
/var/log/messages

what cmd is used to remove the password assigned to A group?
gpasswd -r

How can i check who are the users logged in my system?
users cmd

what is NIC bonding?

mode=0    (balance round robin)
mode=1    (Active backup)
mode=2    (Balance XOR)
mode=3    (Broadcast)
mode=4    (802.3ad)
mode=5    (Balance TLB)
mode=6    (Balance ALB)

 lspci | grep Eth
cd /etc/sysconfig/network-scripts/
eth1
eth2

cd /etc/udev/rules.d/70-persistent-ipoib.rules
see eth1 eth2

before start stop NetworkManager service

eth1 file
ONBOOT="yes"
MASTER=bond0
SLAVE=yes
TYPE=Ethernet

eth2 file
ONBOOT="yes"
MASTER=bond0
SLAVE=yes
TYPE=Ethernet

create bond script
cat ifcfg-bond0
IPADD
NETMASK
GATEWAY
USERCTL=no
BOOTPROTO=none
ONBOOT=yes
BONDING_OPTS=mode=0 miimon=100        every 100ms check other interface working?

then restart the network service

check ip a

cat /proc/net/bonding/bond0    it will display both bonding interface


change mode
BONDING_OPTS=mode=1 miimon=100 primary=eth0

restart network service

How to upgrade centos 7 to 8?

https://www.tecmint.com/upgrade-centos-7-to-centos-8/

Step 1: Install the EPEL Repository

# yum install epel-release -y
Step 2: Install yum-utils Tools
# yum install yum-utils

Thereafter, you need to resolves RPM packages by executing the command.
# yum install rpmconf
# rpmconf -a

Next, perform a clean-up of all the packages you don’t require.
# package-cleanup --leaves
# package-cleanup --orphans

Step 3: Install the dnf in CentOS 7
Now install dnf package manager which is the default package manager for CentOS 8.

# yum install dnf

You also need to remove the yum package manager using the command.
# dnf -y remove yum yum-metadata-parser
# rm -Rf /etc/yum


Step 4: Upgrading CentOS 7 to CentOS 8
We are now ready to upgrade CentOS 7 to CentOS 8, but before we do so, upgrade the system using the newly install dnf package manager.
# dnf upgrade

Next, install CentOS 8 release package using dnf as shown below. This will take a while.
# dnf install http://mirror.centos.org/centos/8/BaseOS/x86_64/os/Packages/{centos-linux-repos-8-2.el8.noarch.rpm,centos-linux-release-8.4-1.2105.el8.noarch.rpm,centos-gpg-keys-8-2.el8.noarch.rpm}

Next, upgrade the EPEL repository.
dnf -y upgrade https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

After successfully upgrading the EPEL repository, remove all the temporary files.
# dnf clean all

Remove the old kernel core for CentOS 7.
# rpm -e `rpm -q kernel`

Next, be sure to remove conflicting packages.
# rpm -e --nodeps sysvinit-tools

Thereafter, launch the CentOS 8 system upgrade as shown.
# dnf -y --releasever=8 --allowerasing --setopt=deltarpm=false distro-sync

Step 5: Install the New Kernel Core for CentOS 8
To install a new kernel for CentOS 8, run the command.
# dnf -y install kernel-core

Finally, install CentOS 8 minimal package.
# dnf -y groupupdate "Core" "Minimal Install"

Now you can check the version of CentOS installed by running.
# cat /etc/redhat-release


What is systemctl ?, how to check running services?
# systemctl --type=service --state=running


How to roll back in linux?
 yum install httpd
httpd -version
yum history
yum history undo 7

File format in window and linux?
FAT32
NTFS

EXT4    
stands for 4th extended file system
introduce in RHE6
Maximum volume size is 1 Ebytes
Maximum file size is 16 TB
Max file name 255 byte
Max num of files 4 billion
cmd to format any partion mkfs.ext4 /dev/sda4
Directory can contain a max of 64,000 subdirectory


xfs
XFS stands extends file file system
intro in RHEL7
Max volume size in 8 Ebytes
Max file size is 8 Ebytes
Max file name is 255 bytes
Max num of files 2^64
cmd format any partition mkfs.xfs. /dev/sda4
no limit for directory

Differnce b/w RHEL8, 7 and 6?






How to create local REPO?
createrepo    create s/w pacakage
yum-utils    manage repo

mkdir /REPO    Create a dir
cp -vR * /REPO    copy all file of repository to /REPO folder

vi /etc/yum/repo.d/yum.repo    create yum.repo file in yumr repo.d
[localrepo]
name=centOS
baseurl=file:///REPO
enabled=1
gpgcheck=0

createrepo -v /REPO
yum repolist

OR Download a local copy of the official CentOS repositories to your server.

sudo reposync -g -l -d -m --repoid=base --newest-only --download-metadata --download_path=/var/www/html/repos/

sudo reposync -g -l -d -m --repoid=centosplus --newest-only --download-metadata --download_path=/var/www/html/repos/

sudo reposync -g -l -d -m --repoid=extras --newest-only --download-metadata --download_path=/var/www/html/repos/

sudo reposync -g -l -d -m --repoid=updates --newest-only --download-metadata --download_path=/var/www/html/repos/

the options are as follows:
    –g – lets you remove or uninstall packages on CentOS that fail a GPG check
    –l – yum plugin support
    –d – lets you delete local packages that no longer exist in the repository
    –m – lets you download comps.xml files, useful for bundling groups of packages by function
    ––repoid – specify repository ID
    ––newest-only – only download the latest package version, helps manage the size of the repository
    ––download-metadata – download non-default metadata
    ––download-path – specifies the location to save the packages

createrepo /var/www/html/repos


How to check already installed pacakage dependencies?
systemctl list-dependencies graphical.target | target

switch to another target
systemctl isolate target.target

systemctl get-defaults

halt    means bring down all services but not power off
vi /etc/grub2.cfg     its generated by system, whenever your system boot up, it will read properly, what to load, what to not load, what infra, kernal, when you want to reboot you will come here and you edit it like in rescue/emergency mode

vi /etc/dracut.conf    this default file related to your kernal, all changes you want to do in your kernal you can do it here.

If you want to check more about kernal and dracut check man page

man dracut.bootup


Rescue mode is equivalent to single user mode and requires the root password.
Rescue mode - when it is unable to complete a regular booting process.
but it does not activate network interfaces and multiple users mode

Emergency mode provides the most minimal environment.
when the system is unable to enter rescue mode.
In emergency mode, the system mounts the root file system as read-only,
does not attempt to mount any other local file systems,
does not activate network interfaces.

Bootup into Emergency mode(target) - during boot - grub2 - press the e
Add parameter at the end of the linux16 line :
systemd.unit=emergency.target
Ctrl+a , Ctrl+e
Ctrl+x

Bootup into Rescue mode(target) - GRUB2 menu option - selected boot into rescue - press the e key
Add parameter at the end of the linux16 line :
systemd.unit=emergency.target
Ctrl+a , Ctrl+e
Ctrl+x

Switch to Emergency mode(target)
# systemctl emergency

Switch to Rescue mode(target)
# systemctl rescue

To prevent systemd from sending informative message:
# systemctl --no-wall emergency
# systemctl isolate emergency.target


how to create any service in Linux?
go to /etc/systemd/system/prometheus.service

To use the newly created service, reload systemd.

# sudo systemctl daemon-reload


No comments:

Post a Comment