My Linux reference commands
Linux ufw firewall commands on 18.04 LTS
- View status: sudo ufw status verbose
- Enable firewall: sudo ufw enable
- Disable firewall: sudo ufw disable
- Block a machine in the firewall: sudo ufw deny from 10.20.10.10
- Block a subnet in the firewall: sudo ufw deny from 10.20.10.0/24
- Enable http: sudo ufw allow http
- Allow both http and https: sudo ufw allow proto tcp from any to any port 80,443
- Enable http port : sudo ufw allow 80
- Enable ssh: sudo ufw allow ssh
- Enable traffic from specific hosts or subnet for a particular port: sudo ufw allow from 10.20.20.0/24 to any port 22
Process CPU usage output
Process Output:
CMD : ps -eo user,pcpu,vsize,pid,cmd | sort -k 1 -nr | head -5 = USER %CPU VSZ PID CMD
Large / big files .
To find the largest 10 files (linux/bash):
find . -type f -print0 | xargs -0 du | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}
To find the largest 10 directories:
find . -type d -print0 | xargs -0 du | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {}
Only difference is -type {d:f}.
Monitoring hanging processes
#!/bin/bash
PROCESS=`ps auxw | grep java | grep -v grep`
if [ -z $PROCESS ]; then
echo "Process GMC not running" | mail -s "Alert" yourmail@address.com
fi
My first step would be to run strace on the process, best
strace -s 99 -ffp 12345
Unix Commands
Find the top 10 large unix directories
du -a /var | sort -n -r | head -n 10
If you want to have more human readable output try (GNU user only):
$ cd to where ever you want to
$ du -hsx * | sort -rh | head -10
help :
* du command -h option : display sizes in human readable format (e.g., 1K, 234M, 2G).
* du command -s option : show only a total for each argument (summary).
* du command -x option : skip directories on different file systems.
* sort command -r option : reverse the result of comparisons.
* sort command -h option : compare human readable numbers. This is GNU sort specific option only.
* head command -10 OR -n 10 option : show the first 10 lines.
The above command will only work of GNU/sort is installed. Other Unix like operating system should use the following version (see comments below):
for i in G M K; do du -ah | grep [0-9]$i | sort -nr -k 1; done | head -n 11
find / -xdev -size +100000 -ls | sort -nrk 7 | head
To check the sftp session on a server
- ps -ef | grep '[s]shd' | grep -v ^root
- ps -ef | grep '[s]shd'
SFTP:
- To check if there was open traffic on port 22: netstat -atn | grep ':22'
- sshd logs are generally located at '/var/log/auth.log'
sshd sessions
- command: ps -ef | grep '[s]shd' | grep -v ^root
- command: ps -ef | grep '[s]shd:.*@naveen’ | grep -v ^root
Directory size command
command: du -m /some/path | sort -nr | head -n 20
Audit commands
- command: sudo ausearch -m LOGIN --start today -i
Here's how to view the used memory
- command: ps ax -o rss | awk '{s+=$1}; END {print "Used Memory: "s" KB"}'
Heapsize commands:
The above command shows the default sizes if -Xms, -Xmx are not used
$ java -XX:+PrintFlagsFinal -version | grep HeapSize
uintx ErgoHeapSizeLimit = 0 {product}
uintx HeapSizePerGCThread = 87241520 {product}
uintx InitialHeapSize := 127926272 {product}
uintx LargePageHeapSizeThreshold = 134217728 {product}
uintx MaxHeapSize := 2042626048 {product}
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-0ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
Memory usage commands:
-sh-4.2$ ps ax -o rss | awk '{s+=$1}; END {print "Used Memory: "s" KB"}'
Used Memory: 14556140 KB
-sh-4.2$ free -h
total used free shared buff/cache available
Mem: 15G 14G 157M 578M 1.0G 314M
Swap: 4.0G 2.2G 1.8G
sort CPU usage..
[root@server ~]# ps aux --sort -rss
Find the top five used size of folders or directories
du -hs * | sort -rh | head -5