Linux Commands Cheat Sheet

System Administration

System Information

uname -a
uname -r

IP Address Management

ifconfig eth0 #show specific interface On Centos
ifconfig ens3 #show specific interface On Ubuntu
ip addr show ens3 #show specific interface
hostname -I|cut -d" " -f 1 #current ip of system/node
hostname -I | awk {'print $1'}

Disk Information

Display All FileSystems

df -ah

Check Size Of a Directory On Disk

du -sh /directory/

File Management

Find A File By filename

find /direcotry/  -name 'filename'

Delete Files Older Than X DAYS

find /path/to/directory/ -mindepth 1 -mtime +7 -delete

Delete Files Newer Than X DAYS

find /path/to/directory/ -mindepth 1 -mtime -7 -delete

Lists all certificates without accepting variables from all sites

WEBSITE_NAMES=$(ls /etc/nginx/sites-enabled)
WEBSITE_LOCATION=/etc/nginx/sites-enabled
sleep 5
for WEBSITE in $WEBSITE_NAMES;
do
    if [ -f  "$(find $WEBSITE_LOCATION/$WEBSITE  -name 'ssl_certificate')" ]; then
        echo "\e[32m$WEBSITE Certificate Exists"
    else
        echo "\e[31m$WEBSITE Certificate DOES NOT exist"
    fi
    if [ -f "$(find $WEBSITE_LOCATION/$WEBSITE -name 'ssl_certificate_key')" ]; then
        echo "\e[32m$WEBSITE Cert Key Exists"
    else
        echo "\e[31m$WEBSITE Cert Key DOES NOT exist"
    fi
done

Searches for a specific website name

WEBSITE=/etc/nginx/sites-enabled/$1/
CERT=$(find /etc/nginx/sites-enabled/$1/ -name 'ssl_certificate')
KEY=$(find /etc/nginx/sites-enabled/$1/ -name 'ssl_certificate_key')
#sleep 5
for EACH_CERT in $WEBSITE;
do
    if [ -f  "$CERT" ]; then
        echo "\e[32m$1 Certificate Exists"
    else
        echo "\e[31m$1 Certificate DOES NOT exist"
    fi
    if [ -f "$KEY" ]; then
        echo "\e[32m$1 Certificate Key Exists"
    else
        echo "\e[31m$1 Certificate KEY DOES NOT exist"
    fi
done

Service Management

Display All Services

service  --status-all

Display Running Services

service --status-all | grep '\[ + \]'

Service Status

systemctl status service_name

Process information Checking

ps aux | grep service_name
top
htop

Disk Management

Creating Persistent Disk On GCP

gcloud compute disks create block-s1 --size=100GB --type pd-balanced --zone europe-west4-a

Attaching Dicks On Node

gcloud compute instances attach-disk gke-kubedemo-default-pool-3ab25d00-6ljj  --disk block-s1 --zone europe-west4-a

Format Dicks On Node

sudo mkfs.ext4 -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/sdb

Create Directory Mount-point

sudo mkdir /mnt/disk1

Mount External Disk to Mount-point

sudo mount -o discard,defaults /dev/sdb /mnt/disk1

Writing Changes to /etc/fstab

sudo echo "/dev/sdb    /mnt/disk1    ext4    defaults    0 1" | sudo tee -a /etc/fstab

SSH Public And Private Keys Creation

ssh-keygen -t rsa -b 4096 -C "root@example.com"
ssh-keygen -t ed25519 -f ./keys/root_id_ed25519
ssh-copy-id -i root_id_rsa root@example.com -f

Sed Command

Changing A Word To All Files Containing It, Within A Directory

grep -rl 'apples' /dir_to_search_under | xargs sed -i 's/apples/oranges/g'

Docker Container Management

File Copy From Container To Host

docker cp container_name:/path/to/container /path/to/host

File Copy From Host To Container

docker cp /file/path/to/host/foo.txt container_name: /file/path/to/foo.txt

Populate Kubernetes Volume (or PVC) With Data

cd html && tar -cf - * | kubectl exec -i -n production nginx-0 -- tar xf - -C /var/www/html

Hope this cheat sheet will be useful to some people.