Useful Commands in Linux and Unix

From Luis Gallego Hurtado - Not Another IT guy
Jump to: navigation, search


Cheatsheets

Hardware Information

  • Displays information about the CPU architecture
lscpu
https://www.howtoforge.com/linux-lscpu-command/
  • See Running processes
ps aux
  • See top running processes and free memory
top
Click "E" to change the memory unit: KB, MB, GB.
  • See Virtual Memory stats updated every second
vmstat 1

Disk and filesystem

  • Get file content in directory
ls –lah
  • Get directory size
du –h
  • Get information of filesystem
df –h
  • Create an empty file of 1024KBs (1MB)
dd if=/dev/zero of=file.txt count=1024 bs=1024
  • Automate deletion of partition table and creation of partition on $device_path
( 
echo o # Clear the in memory partition table 
echo n # Add a new partition 
echo p # Primary partition 
echo 1 # Partition number 
echo   # First sector (Accept default: 1) 
echo   # Last sector (Accept default: varies) 
echo w # Write changes 
) | sudo fdisk $device_path

Logical Volumes

https://www.digitalocean.com/community/tutorials/an-introduction-to-lvm-concepts-terminology-and-operations https://www.thegeekstuff.com/2010/08/how-to-create-lvm/

  • Creating physical volume for $device_path
pvcreate $device_path
  • Creating volume group for $device_path
vgcreate $VOLUME_GROUP_NAME $device_path
  • Creating logical volume thin pool $LOGICAL_VOLUME_POOL_NAME in group $VOLUME_GROUP_NAME
lvcreate -l 100%FREE --thinpool $LOGICAL_VOLUME_POOL_NAME $VOLUME_GROUP_NAME
  • Creating logical volume $LOGICAL_VOLUME_NAME in group $VOLUME_GROUP_NAME
lvcreate -l 100%FREE --thin -n $LOGICAL_VOLUME_NAME $VOLUME_GROUP_NAME/$LOGICAL_VOLUME_POOL_NAME
  • Formatting logical volume $LOGICAL_VOLUME_NAME in ext4
mkfs.ext4 /dev/$VOLUME_GROUP_NAME/$LOGICAL_VOLUME_NAME
  • Creating mount point at $mount_path
mkdir -p -m777 $mount_path 
mount /dev/$VOLUME_GROUP_NAME/$LOGICAL_VOLUME_NAME $mount_path 
echo "/dev/$VOLUME_GROUP_NAME/$LOGICAL_VOLUME_NAME $mount_path ext4 defaults,nofail 0 0">>$FSTAB_FILE_FULL_PATH

Networking

  • Check open ports
netstat -tulpn | grep LISTEN
lsof -i -P -n | grep LISTEN
  • Time synchronisation with chrony
    • On demand synchronisation
chronyd -q 'server <IP> iburst'
    • Show details of sources and stats
chronyc sources
chronyc sourcestats