Skip to content

Instantly share code, notes, and snippets.

@sorquan
Last active September 16, 2021 21:34
Show Gist options
  • Save sorquan/a17b52047710d681740688e76b9dc6ed to your computer and use it in GitHub Desktop.
Save sorquan/a17b52047710d681740688e76b9dc6ed to your computer and use it in GitHub Desktop.
Some general commands for GNU/Linux

Format output with delimiter

ls /var/ | xargs | sed 's/ /,/g'

Add text to end of file if it not exists in the file

sed -e '/option=/{s/.*/option=value/;:a;n;:ba;q}' -e 'aoption=value' /dir/filename

Count inodes

find / -printf "%h\n" | cut -d/ -f-2 | sort | uniq -c | sort -rn

Grub CLI loading

1. Find boot partition with vmlinux and initrd

ls, ls (hd0,gptX)

2. Select boot part

set root=(hd0,gptX)

3. Tell grub vmlinux path and arguments

linux /vmlinux-(some-version-of-kernel) root=/dev/sdX ro

4. Tell grub initrd path

initrd /initrd-(some-version-of-kernel)

5. Load system

boot

Using iscsiadm

1. Discover targets under portal IP address

iscsiadm -m discovery -t st -p <iSCSI IP address>:<iSCSI port>

2. Configure CHAP authentification

iscsiadm -m node -T <volume IQN> -p <iSCSI IP address>:<iSCSI port> -o update -n node.session.auth.authmethod -v CHAP
iscsiadm -m node -T <volume IQN> -p <iSCSI IP address>:<iSCSI port> -o update -n node.session.auth.username -v <CHAP user name>
iscsiadm -m node -T <volume's IQN> -p <iSCSI IP address>:<iSCSI port> -o update -n node.session.auth.password -v <CHAP password>

3. Login

iscsiadm -m node -T <volume's IQN> -p <iSCSI IP Address>:<iSCSI port> -l

4. Login to all without authentification

iscsiadm -m node –l

5. Logout

iscsiadm -m node -T <target_iqn> -p <iSCSI IP address>:<iSCSI port> -u

6. Display all data for a given node record

iscsiadm -m node -T <target_iqn> -p <iSCSI IP address>:<iSCSI port>

Extend swap partition

1. Unload and disable swap

swapoff /dev/vg_system/swap

2. Extend swap logical partition

lvresize /dev/vg_system/swap -L +2G

3. Recreate swap on extended partition

mkswap /dev/vg_system/swap

4. Activate new swap partition

swapon -a

5. Check fstab for correct UUID or swap partition name and correct it if needed

Create swap partition/file and activate it

1. Write swap signature to disk or partition

mkswap /dev/sdb

2. Enable swap

swapon -a

3. Write info about swap to fstab

/dev/sdb none swap defaults 0 0

Multithreaded copying files

ls -d /root/folder/ | xargs -n1 -P10 -I{} rsync -Pa {} dest.server:/folder/
ls -d /root/folder/ | xargs -n1 -P10 -I{} scp -r {} dest.server:/folder/

Copy many files between servers with copmression in gz

tar -czf - /manyfiles | ssh dest.server 'tar -xzf - -C /manyfiles'

Show 10 largest directories in current directory

du -sh * | sort -rh | head -n10

Truncate deleted log file without restarting of service

1. Find the PID of the process that opened the file

lsof | grep deleted

2. Go to file descriptor catalog of process

cd /proc/<PID>/fd

3. Find descriptor of deleted file

ls -l | grep deleted

4. Truncate it

> 1

Clear swap

1. Check space:

free -m

2. Disable swap:

swapoff -a

3. Wait approx 30 sec (use free -m to see the amount of swap used/available decrease over time)

4. Enable swap:

swapon -a

Install vnc server with gdm to connect multiple users

1. Install tigervnc-server and xinetd

dnf -y install tigervnc-server xinetd

2. Enable "X Display Manager Control Protocol" (XDMCP)

sed -i 's/\[xdmcp\]/&\nEnable=true/' /etc/gdm/custom.conf

3. Create vnc server service file for xinetd

cat << EOF > /etc/xinetd.d/vncserver
service vncserver
{
disable = no
socket_type = stream
protocol = tcp
group = tty
wait = no
user = nobody
server = /usr/bin/Xvnc
server_args = -inetd -query localhost -geometry 1024x768 -depth 32 -once -securitytypes=none
}
EOF

4. Create name for vnc service port

echo -e 'vncserver\t5900/tcp\t\t# VNC xinet GDM base' >> /etc/services

5. Allow vnc service port in firewall

firewall-cmd --add-service=vnc-server --permanent && firewall-cmd --reload

6. Restart gdm for apply new config

systemctl restart gdm.service

7. Enable and start xinetd

systemctl enable --now xinetd.service

Replace drive in mdadm RAID array

1. Find the failed disk (failed disk will be marked with the letter F)

cat /proc/mdstat

2. Mark it failed

mdadm /dev/md0 --fail /dev/sdc

3. Remove from array

mdadm /dev/md0 --remove /dev/sdc

4. Replace failed disk and then check and check its presence in the system

fdisk -l

5. Copy layout from any disk in the array to a new disk

sfdisk -d /dev/sdb | sfdisk /dev/sdc

6. Add new disk to array

mdadm --add /dev/md0 /dev/sdc

7. Watch rebuild process in realtime

watch cat /proc/mdstat

Rename root VG and LV

1. Rename the root Volume Group (VG) using vgrename:

# vgrename system_old system

2. Rename the root Logical Volume (LV) with lvrename:

# lvrename /dev/system_old/root_old /dev/system/root

3. Once VG and LV names changed, update same in /etc/fstab:

# vim /etc/fstab
/dev/mapper/system-root  /  xfs  defaults  1  1
/dev/mapper/system-swap  none  swap  defaults 0 0

4. Modify VG/LV names in /etc/default/grub to use new names:

# vim /etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/system-swap rd.lvm.lv=system/root rd.lvm.lv=system/swap rhgb quiet"

5. Activate the VG:

# vgchange -ay

6. Reload LV meteadata. Do it for all logical volumes:

# lvchange /dev/system/root --refresh
# lvchange /dev/system/swap --refresh

7. Take backup of all initramfs files and regenerate it:

# for i in $(ls /boot/initramfs-*.img); do cp $i $i.bak; done
# dracut -v -f --regenerate-all

8. Take backup and generate a grub2.cfg file using grub2-mkconfig:

  • BIOS-based machines:

# cp /boot/grub2/grub.cfg /boot/grub2/grub.cfg.bak
# grub2-mkconfig -o /boot/grub2/grub.cfg
  • UEFI-based machines:

# cp /boot/efi/EFI/redhat/grub.cfg /boot/efi/EFI/redhat/grub.cfg.bak
# grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg

9. If you get below errors while generating grub configuration file:

/usr/sbin/grub2-probe: error: failed to get canonical path of ‘/dev/mapper/  system-root’.

Then boot the system to rescue mode and rebuild grub2 using following command:

  • BIOS-based machines:

# chroot /mnt/sysimage
# grub2-mkconfig -o /boot/grub2/grub.cfg
  • UEFI-based machines:

# chroot /mnt/sysimage
# grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg

Reset root password

  1. Press e on grub screen
  2. Append rd.break to end of linux line and press ctrl-x
  3. mount -o remount,rw /sysroot/
  4. chroot /sysroot/
  5. passwd
  6. touch /.autorelabel

Rescue mode mount

1. First boot the system into Rescue Mode from the media. For best results use media that matches the version and service pack of the system.

2. Once it's finished loading, log in as root.

3. Mount the root partition or logical volume to /mnt

# mount /dev/<device name> /mnt

If it's not known where the root file system is, it needs to be guessed until found by mounting each possibility and checking its contents. Run the following command to list all available partitions:

# cat /proc/partitions

List all available logical volumes with this command:

# lvdisplay

After mounting each one check its contents by listing the mounted directory. For example if mounted to /mnt run:

# ls /mnt

Some directories that should be listed to verify it is a root file system are /root /boot and /home.

4. Once mounted use the following for-loop to rbind the virtual file systems:

# for i in proc sys dev; do mount --rbind /$i /mnt/$i ; done

Alternatively, mount them each separately rather than using the for-loop:

# mount --rbind /proc /mnt/proc
# mount --rbind /sys /mnt/sys
# mount --rbind /dev /mnt/dev

5. After the root file system and virtual file systems are mounted run this command to chroot in:

# chroot /mnt

After chrooted in it is a best-practice to run this command to make sure all other necessary partitions are also mounted:

# mount -a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment