Welcome to Naveen's & Padma's home

Main Content

 

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

 

Modern Linux quick reference

These commands are useful on current Linux systems, especially Ubuntu, Debian, Fedora, and other systemd-based distributions. Always understand a command before running it with sudo, and be extra careful with commands that delete files, change firewall rules, or restart services.

System information

  • Kernel and machine: uname -a
  • Distribution details: cat /etc/os-release
  • CPU and memory summary: lscpu and free -h
  • Block devices and mount points: lsblk -f
  • Disk free space: df -h

Services and logs

  • Check service status: systemctl status nginx
  • Start, stop, or restart a service: sudo systemctl restart nginx
  • Enable a service at boot: sudo systemctl enable nginx
  • View recent system logs: journalctl -xe
  • Follow logs for one service: journalctl -u nginx -f
  • Show logs since today: journalctl --since today

Network checks

  • Show IP addresses: ip addr
  • Show routes: ip route
  • Show listening ports: ss -tulpen
  • Test DNS lookup: resolvectl query example.com
  • Trace network path: tracepath example.com
  • Test a web endpoint: curl -I https://example.com

Files, permissions, and search

  • Find files by name: find /path -name "*.log"
  • Search text recursively: grep -R "error" /var/log
  • Show permissions: ls -la
  • Change owner: sudo chown user:group file.txt
  • Change permissions: chmod 640 file.txt
  • Archive a folder: tar -czf backup.tar.gz folder/

Package management

  • Ubuntu/Debian update package lists: sudo apt update
  • Ubuntu/Debian upgrade installed packages: sudo apt upgrade
  • Ubuntu/Debian find a package: apt search package-name
  • Fedora update packages: sudo dnf upgrade
  • Fedora find a package: dnf search package-name

Security checklist

  • Use SSH keys and disable password login for internet-facing servers when possible.
  • Keep only required ports open in the firewall.
  • Review active users: getent passwd
  • Check recent logins: last
  • Check failed login attempts on systemd systems: journalctl _COMM=sshd --since "24 hours ago"
  • Keep regular backups and test restoring them before there is an emergency.

Helpful habits

  • Use --help for quick syntax, for example du --help.
  • Use man command for detailed documentation, for example man systemctl.
  • Before deleting files, run a safer listing command first to confirm the path.
  • When troubleshooting, record the exact command, time, output, and change made.

DevOps command reference

These are practical commands for server administration, application deployment, container troubleshooting, CI/CD runners, and cloud-hosted Linux systems. Replace service names, namespaces, container names, and paths with values from your environment.

Git and release work

  • Show current branch and changes: git status --short --branch
  • Review recent commits: git log --oneline --decorate --graph -n 20
  • Compare working changes: git diff
  • Compare staged changes: git diff --cached
  • List tags for releases: git tag --sort=-v:refname | head
  • Create an annotated release tag: git tag -a v1.2.3 -m "Release v1.2.3"

Docker and containers

  • List running containers: docker ps
  • List all containers: docker ps -a
  • Follow container logs: docker logs -f container_name
  • Open a shell in a container: docker exec -it container_name sh
  • Show image disk usage: docker system df
  • Clean unused images and containers: docker system prune
  • Build an image: docker build -t app-name:local .
  • Run a local container with a port mapping: docker run --rm -p 8080:80 app-name:local

Kubernetes quick checks

  • Show cluster access: kubectl cluster-info
  • List namespaces: kubectl get ns
  • List pods in a namespace: kubectl get pods -n namespace
  • Describe a failing pod: kubectl describe pod pod-name -n namespace
  • Follow pod logs: kubectl logs -f pod-name -n namespace
  • Check events sorted by time: kubectl get events -n namespace --sort-by=.lastTimestamp
  • Restart a deployment: kubectl rollout restart deployment/app-name -n namespace
  • Check rollout status: kubectl rollout status deployment/app-name -n namespace

CI/CD runner troubleshooting

  • Check runner service status: systemctl status actions.runner
  • Watch runner logs: journalctl -u actions.runner -f
  • Find long-running build processes: ps aux --sort=-etime | head -20
  • Check temporary disk usage: du -h --max-depth=1 /tmp | sort -h
  • Show environment variables for a process: tr '\0' '\n' < /proc/PID/environ

Web server and TLS checks

  • Test an HTTP endpoint: curl -I https://example.com
  • Show response timing: curl -w "@curl-format.txt" -o /dev/null -s https://example.com
  • Check TLS certificate dates: openssl s_client -connect example.com:443 -servername example.com < /dev/null 2>/dev/null | openssl x509 -noout -dates
  • Test Nginx config: sudo nginx -t
  • Reload Nginx safely: sudo systemctl reload nginx
  • Test Apache config: sudo apachectl configtest

Performance and incident response

  • Live CPU and memory view: top or htop
  • Show I/O pressure: iostat -xz 1
  • Show network connections: ss -tunap
  • Find open files for a process: lsof -p PID
  • Trace a command: strace -f -o trace.log command
  • Show failed systemd units: systemctl --failed
  • Find recently changed files: find /path -type f -mtime -1 -ls

Secrets and safety

  • Never paste production secrets into public chat tools, tickets, screenshots, or logs.
  • Use environment files with restricted permissions: chmod 600 .env
  • Search for accidental keys before committing: git diff --cached | grep -Ei "api[_-]?key|secret|token|password"
  • Rotate any secret that was committed, logged, emailed, or shared by mistake.