Linux boot loader

grub2 (Boot) commands:

  • grub2-*
  • grub2-mkconfig
  • Configs:
    • /boot/grub2/grub.cfg
    • /etc/default/grub
    • /etc/grub.d/*

Backup Master Boot Record (MBR):

  • MBR record = 512 bytes
    • Bootstrap = 446 bytes
    • Partition table = 64 bytes
    • Signature = 2 bytes
  • Use 446 bytes to overwrite or restore your /dev/sda   MBR boot code:
    • dd   if=/dev/sda    of=/mbr.bak   bs=446  count=1         (Backup)
    • dd   if=/mbr.bak   of=/dev/sda    bs=446  count=1         (Restore)
  • Use 512 bytes to overwrite or restore the full MBR.
    • dd   if=/dev/sda    of=/mbr.bak   bs=512  count=1         (Backup)
    • dd   if=/mbr.bak   of=/dev/sda    bs=512  count=1         (Restore)

Recovery modes:

  • Rescue mode/Single user mode:
    Rescue mode provides a convenient single-user environment and allows you to repair your system in situations when it is unable to complete a normal booting process. In rescue mode, the system attempts to mount all local file systems and start some important system services, but it does not activate network interfaces or allow more users to be logged into the system at the same time. In Red Hat Enterprise Linux 7, rescue mode is equivalent to single user mode and requires the root password.

    • Type ‘e’ at the GRUB boot loader
    • Append either systemd.unit=rescue.target or linux rescue,
      • e.g: … ro crashkernel=auto rhgb quit systemd.unit=rescue.target
    • Then press ‘ctrl+x’ to continue.
  • Emergency mode:
    Emergency mode provides the most minimal environment possible and allows you to repair your system even in situations when the system is unable to enter rescue mode. In emergency mode, the system mounts the root file system only for reading, does not attempt to mount any other local file systems, does not activate network interfaces, and only starts few essential services. In Red Hat Enterprise Linux 7, emergency mode requires the root password.

    • Type ‘e’ at the GRUB boot loader
    • Append either systemd.unit=emergency.target or linux emergency,
      • e.g: … ro crashkernel=auto rhgb quit systemd.unit=emergency.target
    • Then press ‘ctrl+x’ to continue.
Advertisements
Posted in LFCS, Linux

sudo, ssh and scp commands

sudo:

  • sudo bash -c “echo ‘user ALL=(ALL) NOPASSWD: ALL’ > /etc/sudoers.d/user
  • sudo -i                    (Sudo to root)
  • sudo su user          (Sudo to user which don’t have password)
  • su – root OR su –    (Switch to root)
  • su user OR su – user
  • sudo ls -l /etc/
  • Configs:
    • /etc/sudoers.d/*
    • /etc/sudoers

scp:

  • scp file root@host:/tmp                  (Copy file onto remote host)
  • scp -r dir root@host:/tmp               (Copy dir onto remote host)
  • scp root@host:/tmp/file /tmp/         (Copy file from host to localhost)
  • scp -r root@host:/tmp/dir/ /tmp/   (Copy dir from host to localhost)

ssh:

  • ssh user@host;
  • ssh -l user host
  • ssh -i   ~/.ssh/id_rsa    user@host
  • ssh-keygen                 (Generate Public and Private key)
  • Config files:
    • /etc/ssh/*
      • /etc/ssh/sshd_config
      • /etc/ssh/ssh_config
    • $HOME/.ssh/*
      • id_rsa                        (private key)
      • id_rsa.pub                (public key)
      • authorized_keys     (public keys list)
      • known_hosts           (Hosts list to allow login)
Posted in LFCS, Linux

Kernel parameters, module and resource limits

Configure Kernel parameters at run-time:

  • sysctl -a
  • sysctl kernel.pid_max; cat /proc/sys/kernel/pid_max
  • sysctl kernel.pid_max=3000; echo 3000 > /proc/sys/kernel/pid_max
  • sysctl -p               (reloads the config param from /etc/sysctl.conf )
  • sysctl –system   (reload the config param from /etc/sysctl.d/99-sysctl.conf)
  • Configs:
    • /etc/sysctl.conf 
    • /etc/sysctl.d/*.conf

Resource limits:
Hard
: Maximum or upper limit value that can one be set by root
Soft: The current value that a user can modify up to hard limit

  • ulimit -a
  • ulimit -n|-u   [-H|-S]
  • ulimit -n value; ulimit -n hard
  • echo -e “user \t hard \t soft \t nproc \t 3000” >> /etc/security/limits.d/user.conf
  • Config:
    • /etc/security/limits.conf
    • /etc/security/limits.d/*.conf
    • -n: open files
    • -u: max user processes

List, Add or Remove modules from Kernel:

  • lsmod
  • modinfo dummy
  • modprobe module; insmod /tmp/module.ko
  • modprobe -r module; rmmod module
  • depmod
  • /lib/modules/$(uname –r)/
  • Configs:
    • /etc/modprobe.d/*.conf
Posted in LFCS, Linux

Systemd and SysVinit commands

systemd:

  • systemctl list-unit-files –state=enabled|disabled
  • systemctl list-units -t service –all|–state=active|running|loaded|failed
  • systemctl is-enabled service
  • systemctl get-default
  • systemctl reboot|shutdown
  • systemctl start gdm; telinit 5     (Starts graphical interface)
  • hostnamectl status|set-hostname               (List and set hostname)
  • timedatectl status|set-time|set-timezone  (List and set date/time)
  • Configs
    • /etc/systemd/system/
    • /lib/systemd/system/

SysVinit:

  • chkconfig   –add|del  foo       = systemctl daemon-reload
  • chkconfig  –list                         = systemctl list-unit-files –type=service
  • chkconfig   foo    on|off          = systemctl enable|disable foo
  • service foo start|stop|status = systemctl start|stop|status foo
  • Config:
    • /etc/init.d/
    • /etc/rc.d/init.d/
  • Levels:
    • service –status-all
    • 0 = Shutdown | Poweroff
    • 1 = Single User Mode
    • 2 = Multiple users, with text login and no NFS
    • 3 = Multiple users with text login, NFS & Network
    • 4 = Not used
    • 5 = Multiple users with graphical login, NFS & Network
    • 6 = Reboot
Posted in LFCS, Linux

Disk partitioning, formatting, usage and mount

Disk Partitioning:

  • Manage disk partitions:
    • fdisk /dev/sda
    • parted /dev/sda 
    • partprobe -s  /dev/sda                         (Reload partition table)
  • List disk partitions:
    • fdisk -l        [/dev/sda]
    • parted -l     [/dev/sda]
    • lsblk            [/dev/sda]
    • cat /proc/partitions
    • blkid  /dev/sda                          (List disk partition attributes)                            

Disk formatting:

  • mkfs -t ext4|xfs imgfile | /dev/sda1
  • fsck -r -t ext4|xfs /dev/sdb1
  • dumpe2fs /dev/sda1
  • tune2fs -l /dev/sda1; tune2fs -i|-c|-m 10 /dev/sdb1
  • xfsdump; xfsrestore; xfs-freeze
  • e4defrag -c /dev/sdb1; e4defrag /var/log

Filesystem / Disk Usage:

  • df -hT        (Show disk usage and filesystem mount)
  • du -csh /tmp/*      (Show size of /tmp directory)
  • du -hxd 1 /;  du -cshx /* -exclude=proc (Show size of / directories)

mount and umount:

  • mount       -> /proc/mounts
  • mount -a   ->  /etc/fstab
  • mount -o remount /dev/sdb1 -> /etc/fstab:/etc/sdb1
  • mount /dev/sdb1 /mnt; mount /imgfile /mnt
  • umount /mnt; umount /dev/sda; umount /imgfile
  • mount -o loop /dev/loop1 /mnt/loop
  • mount -o ro,noexec /imgfile /mnt
  • mount -t tmpfs -o size=1G none /mnt/tmpfs
  • mount -t tmpfs -o size=200M tmpfs /mnt/tmpfs
  • /etc/fstab   /dev/sda1   /mnt   ext4   default   0   0
  • /etc/fstab   /imgfile      /mnt   ext4    loop       0   0
  • /etc/exports -> (Mount NFS directory)

/proc/ options:

  • ls -al /proc/ | grep ^[-] | sort
  • cat /proc/meminfo; free -mh
  • cat /proc/swpas; free -mh
  • cat /proc/partitions
  • cat /proc/cpuinfo
  • cat /proc/mounts
  • cat /proc/version
  • cat /proc/filesystems

 

Posted in LFCS, Linux

Linux job scheduling with cron and at

at  (Press ctr+d when done adding the job)

  • at now + 5 minutes|hours|days|weeks|months|years 
  • at -f script.sh 2:30 PM 10/21/2017
  • atq             (List jobs)
  • atrr  id      (Remove jobs)

crontab  [*mins(0-59)   *hrs(0-23)   *day(1-31)   *mon(1-12)    *dayofweek(0-6)]

  • crontab -l; crontab -r; crontab -e
  • crontab -u user -l | -e
  • crontab job.sh
  • @reboot, @hourly, @daily, @weekly, @monthly, @yearly
  • 0 5,17 * * * $HOME/script.sh         (Daily at 5 am and 5 pm)
  • */10 * * * * $HOME/script.sh         (every 10 mins)
  • * */4 * * *   $HOME/script.sh         (every 4 hours)
  • cat /etc/crontab; /etc/cron.d
  • More examples:
    https://tecadmin.net/crontab-in-linux-with-20-examples-of-cron-schedule/#

P.S. at command scheduled jobs only once, whereas crontab is used for running the jobs repeatedly.

Posted in LFCS, Linux

VMware guest Linux – boot off iso installation rescue media image

1. Click “Hardware > CD/DVD (ID) > Use ISO image file” and select an installation iso image such as “CentOS-7-x86_64-DVD-1611.iso” in VMware player.

2. Edit vm-name.vmx file in Vmware virtual machine image folder and the following parameter to show the Boot Menu screen for 5 seconds.

  • bios.bootdelay = 5000
  • Press F2 key to enter into SETUP and configure to boot from CD-ROM Drive.

3. When the image is booted from iso image, then select

  • Troubleshooting > Rescue a CentOS Linux system > press Enter

4. Once the system is booted in rescue mode then run the following to mount:

  • chroot /mnt/sysimage

5. Once done, then type exit to reboot.

Posted in LFCS, Linux