Skip to content

luqmannn/command-line-magic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 

Repository files navigation

Introduction

Let's face it, we are mere mortals that keep forgetting everytime we learn something new. So that's why I created this simple cheatsheet. Every time I forget something important, I want to refer back in one single place instead of scouring the Internet for the same answer that I did before. Hope this will help.

I plan to use this repo as sort of my knowledge base notes and it will continue to grow larger from now on. Need to restructure this repo since there's so many new tools I need to use such as Openstack and Openshift. Maybe a separate new repo for both of this tool since it is very powerful and capable to create entirely new infrastructure and deployment place for application from scratch.

System magic

Passwd

paste <(cat /etc/passwd | cut -d: -f1) <(cat /etc/passwd | cut -d: -f3) <(cat /etc/passwd | cut -d: -f4) <(cat /etc/passwd | cut -d: -f5) | column -s $'\t' -t
  • Show only the username, user id, group id and comment/description from passwd file.

Df

df -h | awk '/^C:\\/ {print $3 "/" $2}'
  • Show C: disk usage, use in WSL.
df -h -t ext4 -t xfs | awk '{print $1, $3 "/" $2}' | column -t
  • Show disk usage for disk using ext4/xfs filesystems.

Free

free -h | awk '/^Mem:/ {print $3 "/" $2}'
  • Show memory used/total.

Find

find . -printf '%s %p\n'| sort -nr | head -10
  • Show top 10 largest files (descending order, starting from the current folder, recursively).

Du

du -hsx * | sort -rh | head -10
  • Show top 10 largest folders.

Ps

ps axch -o cmd:15,%mem --sort=-%mem
  • Show most memory intensive process.
ps axch -o cmd:15,%cpu --sort=-%cpu
  • Show most CPU intensive process.

Apt-mark

apt-mark showmanual
  • Show explicit installed packages.

Echo

echo $PATH | tr : \\n
  • Show current PATH environment variable.

Systemctl

systemctl list-unit-files --state=enabled --no-pager
  • Show all installed services.

User management magic

Useradd

sudo useradd -d /home/miku -s /bin/bash -m miku -c "Add comment"
  • Add a new user, specifices home directory, default shell, creates home directory for new user if it does not exist and adds a comment/description.
  • This is the long way, can be done much faster using adduser :P

Setfacl

sudo setfacl -R -m u:username:rX /var/logs
  • Grant specific user read only access to the directory belongs to other user, for read and list out all the content in the directory without changing owner and ownership of the directory.
sudo setfacl -R -x u:username /var/logs
  • Remove ACL access given previously.

Getfacl

getfacl /var/logs
  • Check the directory whether it has user set by setfacl

Usermod

usermod -aG admin miku
  • Modify the user by adding it to group admin.

LVM and disk management magic

Fdisk

fdisk -l
  • List partitions
fdisk /dev/sdX
  • Start the partition process:
    • m for help
    • n create a new partition
    • t change partition type
    • w write changes to the disk
    • q to quit
    • i print information about a partition

Pvcreate

pvcreate /dev/sdX?
  • Create physical volume. Check using pvs command.

Vgcreate

vgcreate vg-anyname /dev/sdX?
  • Create volume group. Check using vgs command.

Lvcreate

lvcreate -L sizeG -n lv-anyname vg-anyname
  • Create logical volume with the specified size. Check using lvs command.
lvcreate -l 100%FREE -n lv-anyname vg-anyname
  • Create logical volume by using remaining free space from volume group, in this case all available space (100%).
  • This can also fix error volume group vg-anyname has insufficient free space (14799 extents): 14800 required when creating logical volume.

Mkfs

mkfs.xfs /dev/vg-anyname/lv-anyname
  • Create xfs file system on the LVM disk

Mount

cd / ; mkdir -p newdir ; mount /dev/vg-anyname/lv-anyname newdir/
  • Change to root directory, create new directory named newdir and mount newly formatted disk on newdir directory.

Fstab

/dev/mapper/vg-anyname/lv-anyname   /newdir     xfs     defaults    0 0
  • Edit fstab file and the parameter above to make sure the mounted disk is boot persistent.
  • Check the disk using fdisk -l.
  • Save and exit the file.

Umount

umount /dev/mapper/vg-anyname/lv-anyname
  • Pretty much the opposite of mount. Don't forget to mount back

Network magic

Ip

ip a show scope global dynamic | awk '$1 == "inet" {print $2, $4}'
  • Show IP and broadcast address.

Curl

curl -4 https://icanhazip.com
  • Show my external IP address.

Text and data manipulation magic

Sed

sed 's/"//g'
  • Remove all double quotes on each line.
sed 's/[^ ]* //'
  • Remove everything before the first space.
sed 's/,$//'
  • Remove last comma on each line.
sed -e 's/\[[^][]*\]//g'
  • Remove bracket and everything between a pair of bracket.
sed 's/,/ /g'
  • Replace all commas with spaces.

Awk

awk '{print $0}'
  • Print all column.
awk '(NR>1)'
  • Print all line except first.
awk 'NF'
  • Remove empty lines from list result.
awk '{$1=$1};1
  • Remove all leading, trailing whitespaces and tabs from each line.
awk 'NR==2{ print; }'
  • Grep specific line numbers.

Grep

grep -oP '\[\K[^\]]+'
  • Extract everything inside brackets.
grep -m1 'searchstring' file_name
  • Find only first occurence in a file.

Multimedia magic

FFmpeg

ffmpeg -i video_name.mp4 -i audio_name.m4a -c copy -map 0:v -map 1:a output_name.mp4
  • Adding audio to a video without re-encoding.

Yt-dlp

yt-dlp --extract-audio --audio-format mp3 https://ytvideolink
  • Extract audio track from Youtube video and store as mp3.

Security magic

Nmap

nmap -sC -sV -p- -oN output-name ip-address
  • Scan all of target port using default script, version detection and output the result normally.
nmap --script=http-enum -p80 ip-address
  • Enumerate specific port using http-enum script.

Gobuster

gobuster dir -u http://ip-address -w /usr/share/dirbuster/wordlists/directory-list-2.3-medium.txt
  • Search name of the hidden directory on the web server with wordlist.

Enum4linux

enum4linux -M -G -S ip-address
  • Enumerate machine list, group and user list with share list.

Smbclient

smbclient //ip-address/Anonymous
  • Mount and connect to the sharename (Anonymous).

Hydra

hydra -l ssh-username -P /usr/share/wordlists/rockyou.txt ssh://ip-address
  • Bruteforce SSH password with wordlist from known SSH username.

Openstack magic

Provision new disk to a server

source <rc-file-download-from-osp-project>
openstack server list --fit
openstack server show <server-id> --fit
openstack volume list --fit
openstack volume create --size <num> <volume-disk-anyname>
openstack volume list --fit
openstack server add volume <server-id> <volume-id>
openstack server show <server-id> --fit

Poweroff hypervisor host

source overcloudrc
openstack hypervisor list --fit | grep <hypervisor-host>
openstack server list --all-project --host <hypervisor-host> --fit
source stackrc
openstack baremetal node list --fit | grep <hostname>
openstack baremetal node show <hostname> --fit
openstack server list --fit | grep <hostname>
ssh user@ip-from-previous-command
sudo su -
shutdown now

Check server, network and subnet details

openstack server list --fit
openstack network list --fit
openstack subnet list --fit

Check image, volume and flavour details

openstack image list --fit
openstack volume list --fit
openstack flavour list --fit

About

Simple cheatsheet to some linux commands

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published