Friday, July 22, 2022

LINUX COMMANDS - III


DISPLAYING DATE IN VARIOUS FORMATS
date +%a,%_2d-%b-%4Y,%_H:%M%_P
Tue, 7-Dec-2021, 1:41-AM
date +%a,%_2d-%b-%Y,%_H:%M%_p 

date +%a," "%d" "%b" "%Y" - "%r
Wed, 19 Jan 2022 - 05:36:48 PM IST


COPY COMMANDS
cp -a //run/media/bhishma/WD-II/IP-1/. /mnt/data/win1/
cp -a -v //run/media/bhishma/WD-II/IP-2/. /mnt/data/win2/
cp -a -v //mnt/data/win1/. /run/media/bhishma/WD-II/IP-1/
/usr/bin/rsync -vuza /mnt/data/win1/. /run/media/bhishma/WD-II/IP-1/
/usr/bin/rsync -vuza /mnt/data/win2/. /run/media/bhishma/WD-II/IP-2/
/usr/bin/rsync -vuza /mnt/data/kdebkp/. /run/media/bhishma/kdebkp/

IPTABLE COMMANDS
iptables -p icmp -h ### IPTABLES COMMAND COMPLETE OPTIONS
systemctl enable iptables ### sets iptables to start on boot
systemctl disable iptables ### stops iptables from start on boot
systemctl start iptables.service
systemctl start ip6tables.service
systemctl stop iptables.service
systemctl --type=service
iptables --list-rules
ip6tables --list-rules
iptables -F ### delete all rules
iptables -X ### DELETE A USER DEFINED CHAIN
iptables -E LOGGING LOGS ### RENAME chain named LOGGING TO LOGS
iptables-save -f /etc/iptables/iptables.rules
iptables-save -f /etc/iptables/ip6tables.rules
journalctl -k | grep "IN=.*OUT=.*" ### LIST LOGS FOR IPTABLES

iptables -P INPUT ACCEPT
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -L -n -v --line-numbers ### List ruleset, numeric format, verbose, display rule line numbers
iptables -F ### delete all rules
iptables -D OUTPUT 2 ### delete input rule number 5
iptables -L INPUT -n --line-numbers
iptables -I INPUT 1 -s ipaddress -j DROP ### inserts new INPUT rule 5
iptables -A INPUT -j DROP ### DROP ALL INCOMING PACKETS
iptables -A OUTPUT -j DROP ### DROP ALL OUTGOING PACKETS

MOVE NON WORKING IPTABLES APP TO TEMP DIR &
COPY WORKING LEGACY APP TO DEFAULT FOLDER

sudo touch /run/xtables.lock
sudo chmod a+r /run/xtables.lock

#!/bin/bash
mv /usr/sbin/iptables /usr/sbin/iptables-broken
cp /usr/sbin/iptables-legacy /usr/sbin/iptables
mv /usr/sbin/iptables-restore /usr/sbin/iptables-restore-broken
mv /usr/sbin/ip6tables-restore /usr/sbin/ip6tables-restore-broken
mv /usr/sbin/iptables-save /usr/sbin/iptables-save-broken
mv /usr/sbin/ip6tables-save /usr/sbin/ip6tables-save-broken
cp /usr/sbin/iptables-legacy-restore /usr/sbin/iptables-restore
cp /usr/sbin/ip6tables-legacy-restore /usr/sbin/ip6tables-restore
cp /usr/sbin/iptables-legacy-save /usr/sbin/iptables-save
cp /usr/sbin/ip6tables-legacy-save /usr/sbin/ip6tables-save

IPTABLES RULE TO LOG BOTH INCOMING & OUTGOING PACKETS
iptables -N LOGS1
iptables -I INPUT 4 -j LOGS1
iptables -I OUTPUT 4 -j LOGS1
iptables -A LOGS1 -m limit --limit 2/min -j LOG --log-prefix "IPTbmv-Dropped1: " --log-level 4
iptables -A LOGS1 -j DROP

DROP OR ACCEPT TRAFFIC FROM MAC ADDRESS EXAMPLE
iptables -A INPUT -m mac --mac-source 00:0F:EA:91:04:08 -j DROP
iptables -A INPUT -p tcp --destination-port 22 -m mac --mac-source 00:0F:EA:91:04:07 -j ACCEPT ### ACCEPT TCP PORT 8080 FROM MAC source

NETWORK COMMANDS
ip address show
which tcpdump ### FIND IF TCPDUMP IS INSTALLED IN SYSTEM
pacman -S tcpdump ### INSTALL TCPDUMP
tcpdump -D ### PRINT LIST OF NETWORK INTERFACES THAT TCPDUMP CAN USE
ss -lntu ### LIST OPEN TCP & UDP NETWORK PORTS
ip neigh ### LIST NEIGHBOUR DEVICES AND ITS MAC ADDRESSES
pacman -S net-tools ### INSTALLS NETSTAT & OTHER COMMANDS
pacman -Sy dnsutils ### INSTALLS NSLOOKUP & DIG & OTHER COMMANDS
ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
ip -4 addr show dev wlp2s0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
ip -4 addr show | grep -v '127.0.0.1'
ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1'
ip addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1'
ip addr show | grep -oP 'inet \K[^/]+' | grep -v '127.0.0.1' ### BEST COMMAND SHOWS IP ONLY
ip route get 8.8.8.8 | grep -oP 'src \K[^ ]+'
ip route | grep -oP 'src \K[^ ]+'
ip route get 8.8.8.8 | grep -oP 'dev \K[^ ]+'
ip addr | grep "scope global" | sed 's:^[^0-9]*::' | sed 's:/.*$::'
ip addr | grep "scope global" | grep -oP 'inet \K[^/]+'
ip a | grep "scope global" | grep -Po '(?<=inet )[\d.]+'
ip address show $(ip route | grep "^default " | head -n1 | grep -Po "(?<=dev )[^ ]+") | grep -Po "(?<=inet )[^ /]+"
nmcli device show | awk '/4.A/ {print $2}' | grep -v '127.*' | cut -d / -f1

DISK | DIRECTORY | FILE COMMANDS
blkid ### lists UUID of all disk resources
ls -al /dev/disk/by-uuid/ ### lists UUID of all disk resources
find . -type f -name "*dillo*" ### FIND FILE WITH NAME dillo (START FROM \ DIRECTORY)
rm -rf folder-name ### deletes all subfolders & files recursively
find . -type f -name pycharm.svg -exec rm {} \; ### FIND & DELETE ALL FILES WITH NAME "pycharm.svg"
df -h
df -hT
fdisk -l
parted -l
findmnt ### POWERFUL TOOL TO FIND MOUNTED FILESYSTEMS.
chown bhishma:bhishma . ### CHANGE OWNERSHIP FROM ROOT TO BHISHMA FOR CURRENT DIRECTORY
chown owner_name file_name
chown -c master file1.txt ### CHANGES OWNER TO MASTER | -C: REPORTS AFTER OWNER CHANGE IS DONE.
chown :group1 file1.txt ### TO CHANGE OWNERSHIP OF A GROUP
chmod +rwx . ### CHANGE PERMISSIONS OF CURRENT DIRECTORY
chmod [class][operator][permission] file_name
chmod [ugoa][+or-][rwx] file_name
ls -al /dev/disk/by-uuid/ ### lists UUID of all disk resources
UUID=6cce8255-bf46-4b4a-a071-019472c7cc75 /mnt/data ext4 defaults 0 2 ### EDIT /etc/fstab FILE TO AUTO MOUNT DISK AT BOOT
sudo dd bs=4M if=/mnt/data/downloads/archbang-autumn-0412-x86_64.iso of=/dev/sdb conv=fdatasync status=progress ### CREATE USB BOOTABLE FROM ISO
dd bs=4M if=/mnt/data/downloads/iso/manjaro-xfce-21.1.6-minimal-211017-linux513.iso of=/dev/sdb conv=fdatasync status=progress

AUTO MOUNT A DATA DISK
blkid ### lists UUID of all disk resources
ls -al /dev/disk/by-uuid/ ### lists UUID of all disk resources
mkdir /mnt/data ### CREATE NEW MOUNT POINT FOLDER
groupadd grpdata ### CREATE NEW GROUP
usermod -aG grpdata kalki (KALKI IS NAME OF USER TO BE ADDED) ### ADD USER KALKI TO GROUP grpdata
chown -R :grpdata data ### CHANGE OWNERSHIP OF MOUNT POINT
vi /etc/fstab ### OPEN FSTAB CONFIG FILE
UUID=6cce8255-bf46-4b4a-a071-019472c7cc75 /mnt/data ext4 defaults 0 2 ### ADD LINE TO FSTAB FILE END
findmnt --verify ### TEST THE FSTAB BEFORE REBOOTING

PACMAN INSTALL/UNINSTALL COMMANDS
pacman -Syyu ### DOWNLOAD & INSTALL SYSTEM UPDATE FOR ARCH LINUX
pacman -S epiphany ### DOWNLOADS & INSTALLS OTTER BROWSER & DEPENDENT PACKAGES
pacman -Rns dillo ### UNINSTALL DILLO WEB BROWSER WITH INSTALLED PACKAGES

SYSTEM CONFIG COMMANDS
systemctl --type=service
systemctl --type=service --state=running
sudo -i
systemctl daemon-reload
lsblk -a ### LISTS ALL STORAGE DEVICES ON SYSTEM
lsusb ### LISTS ALL USB DEVICES
dmidecode -t bios ### LIST BIOS INFORMATION
pacman -Syu ### UPDATES ALL INSTALLED PACKAGES
pacman -S opera ### INSTALL OPERA BROWSER
dmidecode -t memory ### MEMORY HARDWARE INFORMATION
dmidecode -t memory | grep -i size ### MEMORY HARDWARE INFORMATION - SIZE
dd if=/dev/zero of=1G.test bs=1 count=0 seek=1G ### CREATE 1GB FILE FOR TESTING
rm ./path/to/the/file/file_1.txt ### DELETE A FILE
rm -d directory ### DELETE A DIRECTORY
ps aux | grep midori
ps aux | grep firefox
ps aux --sort -pcpu | grep firefox
ps aux --sort -pmem | head -n 16 ### DISPLAY TOP 16 MEMORY CONSUMING PROCESSES
ps aux --sort -pcpu | head -n 10 ### DISPLAY TOP 10 CPU CONSUMING PROCESSES
ps auxk -pcpu | head -n 10 ### DISPLAY TOP 10 CPU CONSUMING PROCESSES
ps -eo user,pid,%cpu,%mem,command --sort -pcpu | grep midori | head -20 ### SORTS OUTPUT DESCENDING
ps -eo user,pid,%cpu,%mem,command --sort=%mem ### SORTS OUTPUT ASCENDING
ps aux --sort -pcpu | grep webkitwebprocess | head -n 10
ps aux --sort -pcpu | grep webkit2gtk | head -n 10
kill [SIGNAL name/number] [PID/process name]
kill -9 3124
killall -9 firefox


BASH SCRIPTING COMMANDS
chmod u+x deploy.sh to make BASH script executable
echo -e "--------------CPU/Memory Usage----------------"
echo -e "Memory Usage:\t"`free | awk '/Mem/{printf("%.2f%"), $3/$2*100}'`
echo -e "Swap Usage:\t"`free | awk '/Swap/{printf("%.2f%"), $3/$2*100}'`
echo -e "CPU Usage:\t"`cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1`
echo ""

AWK SCRIPTING COMMANDS
awk -F':' '{printf "%s%s", $2, (/^Grade/)?"\n":"\t|"}' input.txt ### converts rows to columns
pacman -Qi | grep -E -w -i "name|description|licenses|depends|reason|date"
pacman -Qi | grep -E -w -i "name|description|URL|licenses|Depends|Required|Replaces|Reason" | awk '{ print $1, $2, $3, $4, $5, $6, $7,
$8 }' | awk -F':' '{printf "%-s %s", $2, (/^Install Reason/)?"\n":"\t|"}' | head -n 10
pacman -Qi | grep -E -w -i "name|description|URL|licenses" | awk '{ print $1, $2, $3, $4 }' | awk -F':' '{printf "%-s %s", $2, (/^Licenses/)?"\n":"\t|"}' | head -n 10

CRON JOBS IN LINUX
journalctl -xb -u cronie
systemctl list-timers --all

BASH SCRIPTING - VARIABLES IN LINUX
myname='Terry Clark'
echo $myname
unset myname
export myname=Terry ### CREATES GLOBAL VARIABLES AVAILABLE ACROSS SCRIPTS

TERMINAL CONSOLE KEYBOARD SHORTCUTS
Ctrl + A Go to the beginning of the line you are currently typing on
Ctrl + E Go to the end of the line you are currently typing on
Ctrl + L Clears the Screen, similar to the clear command
Ctrl + U Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H Same as backspace
Ctrl + R Lets you search through previously used commands
Ctrl + C Kill whatever you are running or start a new prompt
Ctrl + D Exit the current shell
Ctrl + Z Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W Delete the word before the cursor
Ctrl + K Clear the line after the cursor
Ctrl + T Swap the last two characters before the cursor
Esc + T Swap the last two words before the cursor
Alt + F Move cursor forward one word on the current line
Alt + B Move cursor backward one word on the current line
Tab Auto-complete files and folder names

MAC ADDRESS
wlp2s0 cc:b0:da:a7:39:2f LAPTOP WIFI
enp3s0 c8:5b:76:68:de:25 LAPTOP LAN
redmi 4a:0b:1d:eb:8f:74 REDMI PHONE


systemd is the newest Linux service manager(System V & Upstart are older ones)
mount command mounts a storage device or filesystem
/usr/local/SOFTNAME = software you compile from source (.tar.gz)
/usr/bin/ = Usually programs get installed to
/opt = can be used for trying software install which can be later removed

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

void auto config

.backups/ .bh .p/ .pcbankdetail/ .profig.os .tmpb/ Alarms/ Android/ DCIM/.thumbnails/ DCIM/100ANDRO/ DemoContent.txt Notifications/ Podcasts...

/* BMV WEB CSS STYLE ENDS */