Skip to content

Latest commit

 

History

31 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Commands

Bash

# forward port without iptables
socat TCP4-LISTEN:8080,fork TCP4:1.1.1.1:8080
# create a RAM disk
mount -t tmpfs -o size=512m tmpfs /mnt/tmpfs

Docker

# save image image-name
sudo docker save image-name | gzip > /tmp/image-name.img.gz
# load image image-name
gunzip -c /tmp/image-name.img.gz | sudo docker load 

Git

# find which branches contain a commit
git branch --contains <commit>
# revert last commit and keep modified files
git reset --soft HEAD^

HTTP

# perform 1000 HTTP requests using 5 concurrent threads
ab -n 1000 -c 5 http://localhost:8080/api/version

# this requires Apache HTTP server benchmarking tool which is available though
# package httpd-tools in RHEL/CentOS and apache2-utils in Debian/Ubuntu

Java

# get thread dump for PID 12345
jstack -l 12345 > /tmp/thread.log
# get heap summary for PID 12345
jmap -heap 12345 > /tmp/heap.log
# get heap histogram for PID 12345
jmap -histo 12345 > /tmp/histogram.log
# get heap dump for PID 12345
jmap -dump:format=b,file=/tmp/heap.bin 12345
# force garbage collection for PID 12345
jcmd 12345 GC.run
# enable JMX remote monitoring
java
  -Dcom.sun.management.jmxremote
  -Dcom.sun.management.jmxremote.port=9010
  -Dcom.sun.management.jmxremote.authenticate=false
  -Dcom.sun.management.jmxremote.ssl=false
  -Dcom.sun.management.jmxremote.local.only=false
  -jar app.jar

Mail

# test smtp server
swaks -s server -f from -t to -au user -ap pass

Nmap

# scan open ports
nmap 192.168.1.1
# scan open ports with service info
nmap -sV 192.168.1.254
# scan subnet for devices
nmap -sn 192.168.1.1-255
nmap -sn 192.168.1.1/24
# scan subnet for ports
nmap -p22,80,443 192.168.1.1/24
# scan subnet for ports with service info
nmap -sV -p22,80,443 192.168.1.1/24
# reverse lookup subnet
nmap --dns-servers 8.8.4.4,8.8.8.8 -sL 209.132.183.105/24

NVMe

# get info
nvme list
nvme id-ctrl /dev/nvme0n1 -H
# secure erase
# (first check if "Crypto Erase Supported as part of Secure Erase" is available)
nvme format /dev/nvme0n1 --ses 2

OpenSSL

# print certificate info
openssl x509 -text -in <certificate> -noout

# check if private key matches certificate
openssl rsa -modulus -in <private-key> -noout | openssl md5
openssl x509 -modulus -in <certificate> -noout | openssl md5

# check if private key matches certificate request
openssl rsa -modulus -in <private-key> -noout | openssl md5
openssl req -modulus -in <certificate-request> -noout | openssl md5

PostgreSQL

-- running queries
SELECT pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity 
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%' 
ORDER BY query_start desc;
-- table statistics
SELECT relname, seq_scan, idx_scan, n_tup_ins, n_tup_upd, n_tup_hot_upd, n_tup_del, n_tup_newpage_upd, autovacuum_count, last_autovacuum
FROM pg_stat_user_tables
ORDER BY n_tup_ins desc;
-- table sizes
SELECT
  table_name,
  pg_size_pretty(pg_table_size(table_name)) AS table_size,
  pg_size_pretty(pg_indexes_size(table_name)) AS indexes_size,
  pg_size_pretty(pg_total_relation_size(table_name)) AS total_size
FROM (SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name
      FROM information_schema.tables
      WHERE table_schema != 'pg_catalog'
        AND table_schema != 'pg_toast'
        AND table_schema != 'information_schema') AS all_tables
ORDER BY pg_total_relation_size(table_name) DESC;
-- index sizes
SELECT
  i.schemaname AS schema,
  i.relname AS table,
  indexrelname AS index,
  pg_size_pretty(pg_relation_size(relid)) AS table_size,
  pg_size_pretty(pg_indexes_size(relid)) AS indexes_size,
  pg_size_pretty(pg_total_relation_size(relid)) AS total_size,
  pg_size_pretty(pg_relation_size(indexrelid)) AS index_size
FROM pg_stat_all_indexes i
WHERE schemaname != 'pg_catalog'
  AND schemaname != 'pg_toast'
  AND schemaname != 'information_schema'
ORDER BY pg_relation_size(indexrelid) DESC;
-- terminate connections
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE datname = '<database name>';
-- copy database
CREATE DATABASE "new" WITH TEMPLATE "old";
-- partition tree
SELECT *
FROM pg_partition_tree('schema.table'::regclass)
WHERE parentrelid IS NOT NULL
ORDER BY relid::varchar;
-- find tables that may need new indexes
SELECT
  relname                                               AS TableName,
  to_char(seq_scan, '999,999,999,999')                  AS TotalSeqScan,
  to_char(idx_scan, '999,999,999,999')                  AS TotalIndexScan,
  to_char(n_live_tup, '999,999,999,999')                AS TableRows,
  pg_size_pretty(pg_relation_size(relname :: regclass)) AS TableSize
FROM pg_stat_all_tables
WHERE schemaname = 'public'
      AND 50 * seq_scan > idx_scan -- more than 2%
      AND n_live_tup > 10000
      AND pg_relation_size(relname :: regclass) > 5000000
ORDER BY relname ASC;

Sed

# remove first line of file
sed -i '1d' file.txt

# remove second line of file
sed -i '2d' file.txt

# remove first and second line of file
sed -i '1d;2d' file.txt

# remove last line of file
sed -i '$d' file.txt

SSH

# copy file from local to remote without scp
cat local.txt | ssh user@example.com "cat > /tmp/remote.txt"
# copy file from remote to local without scp
ssh user@example.com "cat /tmp/remote.txt" > local.txt
# SSH's X11 Forwarding (for instance xterm)
ssh -X user@example.com "xterm"

# the remote machine must have:
# 1. sshd X11 Forwarding enabled ("X11Forwarding yes" at /etc/ssh/sshd_config)
# 2. xorg-x11-xauth package installed (yum install xorg-x11-xauth)
# SSH socks5 proxy
ssh -D 9050 user@domain.com

curl --socks5 127.0.0.1:9050 ipinfo.io
proxychains curl ipinfo.io
proxychains firefox
ssh-agent | tee ~/.ssh/agent.env  # once
source ~/.ssh/agent.env           # multiple terminals

Tmux

# create named session
tmux new -s session-name
# dettach session (inside tmux)
Ctrl+b d
# list session
tmux ls
# attach session
tmux attach -t session-name
tmux attach -t 0
# windows
Ctrl+b c Create a new window (with shell)
Ctrl+b w Choose window from a list
Ctrl+b 0 Switch to window 0 (by number )
Ctrl+b , Rename the current window
# panes
Ctrl+b % Split current pane horizontally into two panes
Ctrl+b " Split current pane vertically into two panes
Ctrl+b o Go to the next pane
Ctrl+b ; Toggle between the current and previous pane
Ctrl+b x Close the current pane

Tor

# create Tor connection to explicit country exit node
echo -e "ExitNodes {us}\nStrictNodes 1" | tor -f -
# use curl
curl --socks5 127.0.0.1:9050 ipinfo.io
# use torsocks
torsocks curl ipinfo.io
# use proxychains
proxychains curl ipinfo.io
# use chromium
chromium --user-data-dir=/tmp/x --proxy-server="socks5://127.0.0.1:9050"

YouTube

# download playlist to mp3
youtube-dl -x --audio-format="mp3" --audio-quality="160k" --add-metadata --continue "https://www.youtube.com/playlist?list=PL7x1NEEwqJNvSGCXac6zGxERF9CDcsOK9"

About

List of useful commands.

Topics

Resources

Stars

4 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors