This is going to be a continuously updated page of useful linux cli snippets or commands.


Sendmail
echo -e "subject: Test Message Subject \n\n Test message body \n | sendmail -v <email>@hotmail.com
RHEL Sudoers.d
cat << EOM >>  /etc/sudoers.d/99-username
> username   ALL=(root)      NOPASSWD:ALL
> EOM
Define a Fancy screen session
$ cat ~/.screenrc 
vbell off
startup_message off
altscreen on
term screen-256color
defscrollback 10000
termcapinfo xterm* ti@:te@:
setenv DISPLAY ':0'
hardstatus alwayslastline
hardstatus string '%{= kR}[ %{R}MY-LAPTOP %{r}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{r}][%{R} %m-%d %{W}%c %{r}]'
Simple mysqldump
mysqldump -u root -p ghost_v2 > ghost-blog-v2_$(date +%Y%m%d_%H%M%S).sql
Test a cron command that would be embedded in a cronjob

ctrl+d to exit at

# at now + 1min
at> mysqldump --databases syspass > /root/syspass-backups/syspass_db_$(date +%Y%m%d_%H%M%S).sql
at> <EOT>

List the scheduled job

at -l

Job will execute and drop out a mail

mailx
Remote rsync to local host
rsync -avzhe ssh pxe.tuk.local:/root/syspass-backups/*.sql /root/syspass-backups
Find Directories not newer than YYYY-MM-DD and delete them
for D in $(find -type d ! -newermt 2019-01-20 | sort); do rm -rf $D; done
Find Files in Current Directory and Move them to dupes/
find . -maxdepth 1 -type f -exec mv -t dupes/ {} +
Strip 1st column of output e.g. line numbers in history
history | awk '{$1=""; print}'

history | sed 's/ *[0-9]* *\(.*\)$/\1/'
Find files with whitespace and rename them with nested for in exec
find . -type f -name "* *" -exec bash -c 'for file in "$1" ; do mv "$file" aure-crazy-$RANDOM.mp4 ; done ' none {} \;
Delete files under a certain filesize
find -size -220M -type f -exec rm -rf {} \;

for F in $(find syspass-backups/ -type f -size -50k); do rm -rf $F; done
Bash Print Numbers
for i in {1..10}; do echo $i; done
Clean up journalctl

journalctl --vacuum-time=1s
Rotate recent logs that can also be cleaned up
journalctl --rotate
Set Journal Max Usage Size

sed -i -e 's/#SystemMaxUse=/SystemMaxUse=500M/g' /etc/systemd/journald.conf
systemctl restart systemd-journald.service
Monitor a directory for changes e.g. Creating a file

inotifywait -e create -c -m -r /home/as/Documents/

Show all cron jobs on a system

for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done

Create directory and cd into it

mkdir newDirectory && cd $_

Write ISO to Bootable USB
lsblk
sudo umount /dev/sdb1
sudo dd bs=4M if=ubuntu-18.04.1-desktop-amd64.iso of=/dev/sdb conv=fdatasync

sudo dd bs=4M if=ubuntu-18.04.1-desktop-amd64.iso of=/dev/sdb status=progress conv=fsync
Record time to write to a disk (512GB)

time dd if=/dev/zero of=/dev/sdb bs=1M count=512000
dd if=/dev/zero of=/tmp/test1.img bs=1G count=1 oflag=dsync

Accidentally unzipped an archive into the wrong dir?
for row in $(unzip -l raspiviv_2.zip | awk 'NR==4, NR==61 { print $4 }'); do rm -rf $row; done

for F in $(tar -tf archive.tar.gz | cut -d"/" -f1 | uniq); do rm -rf $F; done
Force Save Read-only File Opened in VI/VIM

:wq! - Oh no, read-only file, instead of pressing enter:

:w !sudo tee %

o

NMON

nmon -fT -s 120 -c 720

-f - Saves data to file in directory nmon is launched from - Captured in *.nmon file

-T - Collect Top data as well

-s 120 - Collect data every 120 secs

-c 720 - Collect 720 snapshots of data

120 x 720 = 86400
86400 / 3600 = 24 Hours = Run time of capture


The tool will continuously run in the background for capture period when launched in non-interactive mode
A Tool to analyze captured data - This one requires MS Excel, but I'll search for a FOSS one and post when I find it

Jazzed up (but basic) vimrc

curl -o vimrc https://raw.githubusercontent.com/amix/vimrc/master/vimrcs/basic.vim

Enable Swap in VPS
fallocate -l 4G /swapfile
chmod 600 /swapfile 
mkswap /swapfile 
swapon /swapfile
vi /etc/fstab 
/swapfile   none    swap    sw    0   0
vi /etc/sysctl.conf
# Set swapfile swappiness
sysctl vm.swappiness=10
# iNode Caching
vm.vfs_cache_pressure = 50
sudo reboot
swapon -s
Get your current WAN IP & Whois Organisation

ip=$(curl -s http://icanhazip.com) && whois $ip | grep OrgName && echo $ip

Fetch Final URL of File After redirect

curl https://downloads.raspberrypi.org/raspbian_lite_latest -s -L -I -o /dev/null -w '%{url_effective}'
=
http://director.downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-04-19/2018-04-18-raspbian-stretch-lite.zip

Download a file with Curl

curl -o unifi_sysvinit_all.deb http://dl.ubnt.com/unifi/5.6.30/unifi_sysvinit_all.deb

-o Sets output file

Run Multiple Commands on One Line

./kismatic dashboard ; ./kubectl cluster-info

& is the Bash equivalent for ; ( run commands)

&& run commands only when the previous has not caused an error

NetCat

Setup listener #Non-root user cannot setup a port below 1000
nc -lvp 4444
Connect to port

Setup Connection
nc -z -v 10.1.1.1 4444
Connection to 10.1.1.1 4444 port [tcp/*] succeeded!

iPerf3

Install iperf3
sudo apt install iperf3

Start Server - Data is 'pulled' in the server direction so data is ingress
sudo iperf3 -p 84 -s

-p = Port to listen on

-s = Run iPerf in server mode

Start Client - Data is 'served' from the client so data is egress
iperf3 -p 84 -c 192.168.0.2 -t 180 -i 30

-p = Port to connect on

-c = Server to connect to

-t = Time to run test

-i = Interval to report results

Output standardout of command to terminal and file

systemctl --state running | tee running-systemctl.log

CPU Stress Test
sudo apt-get install stress
stress -c 50
DIG DNS

dig +short scfp-vcsa-vc-01.scfpdemo.local @172.22.7.34 - Just outputs the resultant ip address of the requested hostname

TCPDUMP

tcpdump -i tun0 port 53 - Capture packets on interface tun0 port 53

Extended TCPDUMP

tcpdump -vv -f -i tun0 -n "host 10.1.1.7 and port 443" - Capture packets on tun0 to or from host 10.1.1.7 on port 443. Don't show dns resolved names and print verbose

NMAP

nmap -sP 192.168.0.0/24 - Ping Scan Subnet

nmap -sP 10.196.192.0/28 | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" - Ping scan the subnet and dump out just the IP's detected

nmap -O <ip address> - Discover OS

SSH Tunneling

Fantastic Simple Instructions

  1. Before connecting to SSH host - In putty, Dynamic port forward 1024
  2. In firefox set socks host proxy to: localhost, port 1024
  3. Profit

For Linux:- This creates a background process
ssh -D 1024 -f -C -q -N username@servername.com

Using Screen
screen # Create a new screen session
# Disconnect session and leave it running: 'ctrl+a' release keys, then 'd'
screen -r # Return to existing session
screen # Create another session
screen -ls to display sessions (Attached or Detached)
screen -r <number of session> # Reconnect specific session if more than one
# Kill a session from another session or normal shell
screen -S 7547 -X quit
Create a Local YUM repo from a list of RPM's
mkdir /home/stack/cisco-apic-repo
tar -xf dist-rpms-2.3.1-newton-20170612.tar -C /home/stack/cisco-apic-repo
chown -R root:root /home/stack/cisco-apic-repo
yum install -y createrepo.noarch
createrepo /home/stack/cisco-apic-repo/
chmod -R o-w+r /home/stack/cisco-apic-repo/
cat <<'EOF' >> /etc/yum.repos.d/cisco-apic-repo.repo
[local]
name=Cisco APIC Repo
baseurl=file:///home/stack/cisco-apic-repo
enabled=1
gpgcheck=0
EOF
yum makecache fast
yum repolist
yum --disablerepo="*" --enablerepo="local" list available
Sudo Last Command

sudo !!

Re-run last command replacing word
cat /etc/sysconfig/network-scripts/ifcfgp-enp9s0
^cat^vi
Recursively grep files for a string in sub-directories
grep -r <searchString> <dir>
grep -r OS_USERNAME ~
Easy Output of a config file removing whitespace and comments

cat <file> | grep -vE '^$|^#'

Grep a line based on exact match word

ip a | grep -w inet

Grep for unique IP Addresses

$ cat /var/log/iptables.log | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | sort | uniq

Grep for MAC Addresses

cat /var/log/messages | grep -i '[0-9A-F]\{2\}\(:[0-9A-F]\{2\}\)\{5\}'

Pack a directory excluding directories

tar --exclude='./aci_files' --exclude='./aci_patch' --exclude='./images' -cvf stack.tar .

Pack/unpack a directory into/out of a tar + compress -z
#!/bin/bash
sudo tar -cvzf ~/backups/ghostBackup_$(date +%Y%m%d_%H%M%S).tar.gz /data/

# To Extract tar
# sudo tar -xvzf *.tar.gz -C ~/restores/
SCP files to another server
#!/bin/bash
scp -i "keys/awskp.pem" ~/backups/*.gz centos@ec2-52-208-72-152.eu-west-1.compute.amazonaws.com:~/backups/
Calculate size of a directory
ubuntu@ip-10-1-1-154:~/shScripts$ sudo du -hs /data/
17M	/data/
List files, exclude directories, print filenames
ls -l | grep ^- | awk '{print $9}'
Advanced Output of a config file removing lines starting with: '# ; $'
cat /etc/openvpn/server.conf | egrep -v "^\s*(#|;|$)"
Clear contents of a text file
cat /dev/null > /var/spool/mail/root
Write multi-line text to a file
cat <<'EOF' >> ~/shScripts/grepIPAddr.sh
// code goes here
EOF
SSH Between Hosts Made Simple

1. Create (if not already) ~/.ssh/config
2. Insert (& amend to suit) config:

host pizero
    HostName 192.168.0.3
    Port 22
    User pi

host ubrt
    HostName 192.168.0.1
    Port 22
    User admin

3. Copy keys to target hosts: $ ssh-copy-id pi@192.168.0.3
4. SSH to target hosts by shortname e.g. ssh pizero (You can also bash complete after entering example: pi<tab>)

Simple BASH For Loop to list directory sizes on /

for i in $(ls /); do du -sh $i; done
for D in $(find . -maxdepth 1 -type d); do du -sh $D; done

Change Users login Shell

sudo usermod -s /bin/bash <username>

Check Users Groups

id <username> | groups <username>

Add User to Sudoers

usermod -aG sudo <username>
-a = Append
-G = Add Secondary Group

Overwrite existing groups

usermod -G as,sudo as

Set password for new user

passwd ubuntu

Useradd

Create user and home directory
useradd -m ubuntu

Restore SELinux Contexts

Firewalld failing to start - n.b. This will restore the whole OS

sudo restorecon -rv /
Check for Open Ports
lsof -iTCP -sTCP:LISTEN -P -n

Useful Linux Commands