Linux stuff

From regional-training

cheats

  • test copy entire directory from one machine to another
cd /receiving directory; ssh machine "cd /etc/openldap && tar cf - ." | tar tvf -
  • actually copy entire directory from one machine to another
cd /receiving directory; ssh machine "cd /etc/openldap && tar cf - ." | tar txf -
  • recursive file list of attributes with full path
ls -lrt -d -1 "$PWD"/{*,.*}   *
  • use find to display full path of all directories with attributes
find /PATH/... -type d -ls
  • find files modified after date
find /path/to/dir -newermt "yyyy-mm-dd"
  • disk performance write
dd bs=1G count=1 if=/dev/null of=zero
  • disk performance read
dd bs=1G if=zero of=/dev/null

os details

  • architecture
uname -m
  • Linux kernel details
uname -a
  • Debian version
lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 9.13 (stretch)
Release:	9.13
Codename:	stretch

rpm search

rpm -q --filesbypkg mariadb-server

crontab

TZ=UTC
SHELL=/bin/bash
# Restart because machine acquires swap space over time @1503Z which is 0403EDT or 0303EST
3 * * * * [ "$(date +\%H)" == "15" ] && echo "$(date +\%Y\%m\%dT\%H:\%M:\%S.\%N\%z)" >> /var/log/restart.log && /home/volumes/restart.sh
# check that ip add is obtained, ssh is listening and mediawikis are listening - else reboot
*/15 * * * * /home/volumes/check-running.sh >> /var/log/check-running.log
  • disable email, add this to the top of crontab
MAILTO=""

You may also log cron job output with /usr/bin/logger and specify the subsystem via -t e.g.

* * * * * /root/check.sh 2>&1 | /usr/bin/logger -t check

scripts

backups.sh

TODO - rewrite this because backups are now automated,

  • Currently this script is run and then backups are done manually.
#!/bin/bash
echo 'stopping gitblit'
systemctl stop gitblit
echo 'stopping nexus'
systemctl stop nexus
echo 'stopping apache2'
systemctl stop apache2
echo 'stopping mariadb'
systemctl stop mariadb
echo 'sleep 15'
sleep 15
echo 'umount /mnt/repos'
umount /mnt/repos
umount -f -l /dev/loop/repos
cd /home/volumes
echo 'sudo find . -mount -depth -print | cpio -dmp /mnt/Backups/20191112-volumes'
touch /home/volumes/backups.lock

echo 'do not forget to sudo rm /home/volumes/backups.lock'
  • followed by cleanup
sudo rm /home/volumes/backups.lock

restart.sh

This script is used to restart the system. Note it is in two parts just to let things sync and settle.

#!/bin/bash
echo 'shutting down services'
systemctl stop apache2 mariadb gitblit nexus
echo 'rebooting server.arising.com.au'
systemctl reboot

check-running.sh

This script is run every 5 minutes to ensure that the corporate services are functioning and accessible, else the system is rebooted. There is a grace window of 15 minutes to allow engineering fixes should something be really wrong. It takes quite some time for nexus to start - so we need a grace period anyway; 15 minutes is enough time for me to get in and to set the backups.lock or edit the check-running.sh script, or change the crontab entry to prevent an infinite loop if something is broken.

#!/usr/bin/bash
hostname=$(hostname)
now=$(date +%Y%m%dT%H%M)

BACKUPS_LOCK_FILE=/home/volumes/backups.lock

# do not restart when backup are running
if [ -f "$BACKUPS_LOCK_FILE" ] 
then
	echo "$now $hostname: backups are running - skipping check-running.sh"
	exit
fi

# check uptime and do not restart if less than 15 minutes
UPTIME=($(uptime))a
regex="([0-9]+) min"
if [[ $UPTIME =~ $regex ]]
then
	matched=${BASH_REMATCH[1]}
	if [ $matched -lt 15 ]
	then	
		echo "$now $hostname: started $matched min(s) ago which is less than 15 minutes - ignoring"
		exit
	fi
fi

# check that the internet is connected
RESULT=($(ip add | grep inet.*250 | awk '{ print $2 }' | awk -F . '{ print $4 }' ))
if [ $RESULT != "250/24" ]
then
	echo "$now $hostname: internet not connected restarting"
	/bin/bash /home/volumes/restart.sh
fi

# check that ssh is listening
RESULT=($(netstat -anl | awk '{ print $4 }' | grep 0.0.0.0:22 | wc -w ))
if [ $RESULT -lt "1" ]
then
	echo "$now $hostname: ssh not listening"
   	/bin/bash /home/volumes/restart.sh
fi

# check that mariadb is running
RESULT=($(netstat -anl | grep mysql | awk '{ print $9 }' | awk -F / '{ print $4 }'  ));
if [ "$RESULT" != "mysqld.sock" ]
then
	echo "$now $hostname: mariadb is not listening"
	/bin/bash /home/volumes/restart.sh
fi

# check that all the wikis are listening
RESULT=($(netstat -anl | awk '{ print $4 }' | grep :80 | wc -w ))
if [ $RESULT -lt 5 ]
then
	echo "$now $hostname wikis not running properly $RESULT"
	/bin/bash /home/volumes/restart.sh
fi

Installing Linux in an NTFS file

SyncThing

Syncrhonizing files between two systems

bash cheats

display key codes

  • run
showkey

Did you know that pressing

cd PgDn

is almost the same as

cd (tilde)

(My tilde key is not working and I ran showkey and noticed that the tilde is the last character sequence sent with PgDn - lol)

replacing swap partition with file

You can make a /swapfile and use it instead of a dedicated partion.

  • display swap
 swap -s
  • e.g.
 Filename  Type     Size     Used  Priority
 /dev/sda5 parition 15624188 0     -2
  • disable swap via
swapoff /dev/sda5
  • remove from fstab
 vi /etc/fstab
  • create a swapfile (of 1G byte)
dd if=/dev/zero of=/swapfile bs=1024 count=1048576
  • change the mode for rw by root only
chmod 0600 /swapfile
  • install it
mkswap /swapfile
  • check it's running
swap -s

tainted kernel

TO see if you are tainted:

cat /proc/sys/kernel/tainted

To decode if you are:

for i in $(seq 18); do echo $(($i-1)) $(($(cat /proc/sys/kernel/tainted)>>($i-1)&1));done

Refer to the following table:

*https://www.kernel.org/doc/html/latest/admin-guide/tainted-kernels.html

Linux Device Drivers

categories