Misc Linux tools cheatsheet
Less
Less without line wrapping
less -S
Colorful less
less -r
Less: leave uncleared output on exit
export LESS="-X"
Screen
screen -S new_screen_name # create named screen
# detach with Ctrl+a, d
screen -ls # list screens
screen -r 5050 # reattach
screen -d -r byname # reattach not-detached session
Nmap
Discover hosts, MACs, hostnames with ping scan
sudo nmap -sP 192.168.0.1/24
Check if TCP port is open
sudo nmap -sS -p22 192.168.0.50 # SYN scan
sudo nmap -sY -p22 192.168.0.50 # open/filtered/closed
Scan port range
nmap -p 1-65535 localhost
Scan OS and detect services
nmap -A -T4 192.168.0.49
Detect service versions
nmap -sV 192.168.0.49
CVE detection
nmap -Pn --script vuln 192.168.0.49
Shell
Write file without text editor: cat + EOF
cat << 'EOF' > task.xml
EOF
Turn on strict mode in bash
set -euxo pipefail
xargs
Run command on each line:
ls -1 *.sh | xargs -I %s echo "mv '%s' '%s.bak'"
Disk usage of subfolders
du -sch .[!.]* * 2>/dev/null | sort -h
Linux Rescue Kit
Magic Key
Enable Magic Key:
# Temporary
sudo sysctl -w kernel.sysrq=1
# or
sudo echo 1 > /proc/sys/kernel/sysrq
# Permanent
sudo vim /etc/sysctl.conf
# Add:
kernel.sysrq=1
To use the magic SysRq key, press the key combo ALT
-SysRq
-<command key>
:
r
- Turns off keyboard raw mode and sets it to XLATEe
- Send a SIGTERM to all processes, except for init.i
- Send a SIGKILL to all processes, except for init.s
- Will attempt to sync all mounted filesystems.u
- Will attempt to remount all mounted filesystems read-only.b
- Will immediately reboot the system without syncing or unmounting your disks.o
- Will shut your system off (if configured and supported).
Note
Some keyboards may not have a key labeled ‘SysRq’. The ‘SysRq’ key is also known as the ‘Print Screen’ key.
Add GPG key to trusted keys (fix NO_PUBKEY)
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys <PUBKEY>
Restart touchpad driver
sudo modprobe -r psmouse
sudo modprobe psmouse
Disable jbd2 gvfsd
pkill gvfsd-metadata
rm -rf ~/.local/share/gvfs-metadata
Diagnostics
Diagnostic messages of the kernel
sudo dmesg --reltime --ctime
Journal logs
Filter all errors:
sudo journalctl --priority 2..3 -e
sudo journalctl -b 0 -u NetworkManager -e
sudo journalctl -b 0 -e
Crashing GNOME
Errors in journal:
sudo journalctl -b 0 -e /usr/bin/gnome-shell
# Following output:
sudo journalctl -f -o cat /usr/bin/gnome-shell
Reset Gnome configuration:
dconf reset -f /org/gnome/
dconf reset /org/gnome/desktop/interface/cursor-theme
Deb Packages
List installed packages with version
dpkg-query -f '${binary:Package}=${Version}\n' -W