Wednesday, January 18, 2023

Learning Linux - Operate Running Systems(Part-3)

In part 2 of this learning Linux basics, we tried constructing basic shell scripts using the condition, Loops, etc.

In the part 3 series of Learning Linux, We will try to perform the below Linux operations,

    1. boot, reboot, shutdown

    2. Change Linux OS operating modes

    3. Gain root access after interrupting the boot process

    4. troubleshoot bootloaders


- Boot, reboot, and shutdown a system safely:

    Reboot & shutdown: systemctl restart / systemctl poweroff

    schedule shutdown/reboot: shutdown 2:00 'shutting down for maintenance at 2 AM morning'

    shutdown -r 2:00 'rebooting  for maintenance at 2 AM morning'

- Boot or change the system into different operating modes

    Operating Modes(Targets)

- Graphical.target

- Multi-user.target

- Emergency.target

- Rescue.target

     Change Mode: systemctl set-default multi-user.target, 

     Switch Mode: systemctl isolate graphical. target

- Interrupt the boot process in order to gain access to a system

    Red Hat 8: 

    - Open GRUB Edit mode while booting

       


    - In the edit mode, Insert text rd.break at the end of the Linux GRUB syntax. CTL + X to proceed with the boot

   

    - You will be in Emergency target mode with swich_root login. The root file system will be default mounted as ro(read only). To check type: mount | grep /sysroot

    - You must remount the root file system with rw(read write) mode to change the root password. Run the below command to remount in rw mode: mount -o remount rw /sysroot.

    - Type cd /sysroot to change directory to root from Swich_root. 

    - Now you should be able to change root password. Type passwrd root 

    -  then at the end we must create a special file touch /.autorelabel . reboot


    Red Hat 9:

    - Start GRUB loader in edit mode and change crash kernel mode from ro to rw. At the end of Kernel syntax, we will add string: init=/bin/bash


    - Now, press CTRL + X to continue with boot process

    - In the next couple of seconds, you should be already in with root access. Type passwrd root to change the root password and type: touch /.autorelabel and exec /sbin/init to continue the boot process to load the OS.

- Install, configure and troubleshoot bootloaders
           When you in a situation where GRUB is unable to load the OS or getting errors. You might have to rebuild the GRUB config to fix the boot issue. Please follow the below steps

  1. Boot from bootable USB drive and start into 'Troubleshooting' mode
  2. you're in rescue mode and you should be prompted to try couple of options like 1. Continue: This will try to search for root filesystem and mount it for you, 2. Read-Only mode: Mount root fs in ro, 3. Skip to shell, 4. Quit(reboot).
  3. Select 1, Press 'Enter' and type: chroot /mnt/sysroot to get into root access mode
  4. Now, To Generate new GRUB config type: grub2-mkconfig -o /boot/grub2/grub.cfg (BIOS), grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg(EFI)
  5. Now, GRUB should be install on the first sector of OS disk. you need to find the physical disk holding the /boot and install the grub conf there.

lsblk (to find the block devices)

                grub2-install /dev/sda ( This will place the GRUB in first sector of disk /dev/sda) OR 

dnf reinstall grub2-efi grub2-efi-modules shim(EFI) 

  


    6. exit from troubleshoot mode to reboot the system

- Modify the GRUB configuration file 

    Open /etc/default/grub file in vim with sudo access. Modify the required parameters and save the file 

  •     grub2-mkconfig -o /boot/grub2/grub.cfg (BIOS), grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg(EFI)
  • reboot

- Diagnose and manage processes

Process Management: 

  • Examples: ps, ps aux (ax=all processes, u=user oriented format), 
  • ps u -U rahul ( All processes running by user rahul), 
  • ps lax (show nice values for processes), 
  • ps faux (show the parent of process), 
  • top,  pgrep -a syslog (process grep),  
  • nice -n <value -20/19> process id/name
  • renice <value> process id(sudo renice 7 8290), 
  • lsof -p 1(show the open files used by process id), Example: sudo lsof | grep sshd |  grep -i reg | sed 's/.* //g'(reg files used by ssd),
  • sudo lsof /var/log/messages(what process using the file)

        Process Signals: 

  • kill -l (list the valid signals) , 
  • pkill -KILL bash ( Kill the matching processes), 
  • ctrl + z (Pause the open program/process) and fg will bring it back(fg - foreground), 
  • sleep 180 & (Make the program run in background) and fg <id> will bring it back., 
  • bg 1 (make the first background program to run), 

- Locate and analyze system log files

        rsyslog = rocket-fast system for log processing(Default daemon for linux OS)

           Following log file: example tail -f /var/log/messages

        Journalctl

  • Example: journalctl /bin/sudo(Logs for a program), 
  • journalctl -u sshd.service(Log reported by a unit - service), 
  • journalctl -f(follow mode), 
  • journal -p err ( filter the logs by priorities like info, warning, err, crit), 
  • sudo journalctl -p info -g '^c' (g = grep, search for logs starting letter c),
  • last (who logged in), lastlog  

-> echo "Thank you :)"