- To check if SSH service is started
(sudo) service ssh status
- To check if UFW service is started
(sudo) service ufw status
- To enable UFW
sudo ufw enable
- To check UFW port rules
sudo ufw status numbered
- To change the default port for SSH
change #Port 22 to Port 4242 or any other port (yes remove that #)
sudo nano /etc/ssh/ssh_config sudo service ssh restart
- To check if the default port is set
sudo grep Port /etc/ssh/ssh_config
- After Step 4, to delete a rule numbered in UFW
sudo ufw delete <number>
- To add back a rule e.g. 4242
sudo ufw allow <4242>
- To check if sudo program is properly installed
sudo
a help message should pop up - To view partition
lsblk
- To add a
sudo adduser <user>
- To verify if is created
getent passwd <user>
- To create a new
sudo addgroup <group>
- To add into a group
sudo adduser <user> <group> OR sudo usermod -aG <group> <user>
- To check if is in a
getent <group> <user>
- To view all users in a group
getent group
- To switch user
sudo su - <user>
- To check current hostname
hostnamectl
- To change hostname to <new_hostname>
hostnamectl set-hostname <new_hostname> sudo nano /etc/hosts 127.0.0.1 localhost 127.0.1.1 <new_hostname>
- To go back to login page
exit
- To reboot VM
sudo reboot
- To change password policy
sudo nano /etc/pam.d/common-password
reboot to take effect - To change password expiration
sudo nano /etc/login.defs
reboot to take effect - What is a cron job?
A cron job is a Linux command used for scheduling tasks to be executed sometime in the future. This is normally used to schedule a job that is executed periodically – for example, to send out a notice every morning.
- To get IP address
ip addr
if want to get exact one,ip addr | grep -o -P '(?<=inet).*(?=brd)'
- To connect use command
ssh <username>@<VM's ip address> -p <port number>
E.g.ssh [email protected] -p 4242
means connect to user shum at the IP address of 1.2.112.231 using the port 4242 - To exit connection
exit
- To set up cron job, add a
<name>.sh
(monitoring.sh for this project) file in/usr/local/bin/
for a bash script - In Born2BeRoot, place the following bash script in the created .sh file
#!/bin/bash wall $'#Architecture: ' `hostnamectl | grep "Operating System" | cut -d ' ' -f5- ` `awk -F':' '/ ^model name/ {print $2}' /proc/cpuinfo | uniq | sed -e 's/^[ \t]*//'` `arch` \ $'\n#CPU physical: '`cat /proc/cpuinfo | grep processor | wc -l` \ $'\n#vCPU: '`cat /proc/cpuinfo | grep processor | wc -l` \ $'\n'`free -m | awk 'NR==2{printf "#Memory Usage: %s/%sMB (%.2f%%)", $3,$2,$3*100/$2 }'` \ $'\n'`df -h | awk '$NF=="/"{printf "#Disk Usage: %d/%dGB (%s)", $3,$2,$5}'` \ $'\n'`top -bn1 | grep load | awk '{printf "#CPU Load: %.2f\n", $(NF-2)}'` \ $'\n#Last boot: ' `who -b | awk '{print $3" "$4" "$5}'` \ $'\n#LVM use: ' `lsblk |grep lvm | awk '{if ($1) {print "yes";exit;} else {print "no"} }'` \ $'\n#Connection TCP:' `netstat -an | grep ESTABLISHED | wc -l` \ $'\n#User log: ' `who | cut -d " " -f 1 | sort -u | wc -l` \ $'\nNetwork: IP ' `hostname -I`"("`ip a | grep link/ether | awk '{print $2}'`")" \ $'\n#Sudo: ' `grep 'sudo ' /var/log/auth.log | wc -l`
- To add the rule that script would execute without sudo password
sudo visudo
Add this lineyour_username ALL=(ALL) NOPASSWD: /usr/local/bin/monitoring.sh
- To edit the timing for cron job interval
sudo crontab -u root -e
The scheduled tasks are structured as[minute] [hour] [day_of_month] [month] [day_of_week] [command_to_run]
E.g.*/10 * * * * /usr/local/bin/monitoring.sh
for it to run every 10 mins - By default, cron job cannot run in second interval (fastest is once a minute). To bypass that limiation and have cron job to run in seconds interval, make cron job with the same cron job but the second one delayed by a certain interval.
E.g. to run monitoring.sh at 20 seconds interval,a. * * * * * bash /usr/local/bin/monitoring.s (at the first minute) b. * * * * * sleep 20; bash /usr/local/bin/monitoring.sh_ (at the first minute also but sleep for 20 seconds first before running the script) c. * * * * * sleep 40; bash /usr/local/bin/monitoring.sh_ (at the first minute also but sleep for 40 seconds to let a and b run first) In total it would have run 3 same scripts in a minute, each delayed by 20s than the previous to look as if the cron job is running 20s once.
- To stop cron job without editing the crontab
sudo systemctl stop cron
. To enable backsudo systemctl start cron