Skip to content

Instantly share code, notes, and snippets.

Last active November 18, 2021 13:01
Show Gist options
  • Save mikaelkall/c7405de9065848025cb7 to your computer and use it in GitHub Desktop.
Save mikaelkall/c7405de9065848025cb7 to your computer and use it in GitHub Desktop.
Documents good sysadmin solutions and oneliners to problems.

Scp files between two servers without the need to first copy the file locally.

scp -3 'username@server1:/home/user/filename.tar.gz' username@server2:/home/user/

Create point to point tunnel between two servers over ssh with a tun interface to avoid the need to open a firewall.

cli># ssh -w5:5 root@hserver
srv># ifconfig tun5 netmask   
cli># ifconfig tun5 netmask   

Oneliner to list block percentage on a filesystem

dumpe2fs -h /dev/sda1 2> /dev/null | awk -F ':' '{ if($1 == "Reserved block count") { rescnt=$2 } } { if($1 == "Block count") { blkcnt=$2 } } END { print "Reserved blocks: "(rescnt/blkcnt)*100"%" }'

Set block percentage

tune2fs -m1 /dev/sda1

Write line number and function name when debug bash script.

export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
$ bash -x ./script

Run command as a service

$ socat TCP-LISTEN:6666,fork,reuseaddr exec:/command

Forward port to a different port.

socat TCP-LISTEN:9999,reuseaddr,fork,su=nobody

socat TCP-LISTEN:80,fork TCP:<address>:80

TLS termination and MITM proxy

 openssl req -new -x509 -keyout test.key -out test.crt -node
 cat test.key test.crt > cert.pem
 socat -v openssl-listen:443,reuseaddr,cert=./cert.pem,verify=0,fork tcp4:xx.xx.xx.xx:80

Log everything from terminal to file

exec >> /var/log/logfile 2>&1

Clone driver over ssh and create virtualbox image.

 ssh root@host "dd if=/dev/sda | gzip -1 -" | dd of=image.gz status=progress    
 gunzip image.gz
 VBoxManage convertfromraw image.bin image.vdi --format VDI

Fix gpg when switch yubikey

gpgconf --kill gpg-agent
gpg --card-status

Jq magic

"Items": {
   "password": {
       "S": "password"   
   "username": {
   "S": "username"

Example JQ parsing

 <command_output_json> | jq -r '.Items[] | "\(.username[]):\(.password[])"'


sudo sshfs -o allow_other,default_permissions username@server:/home/username/debug /mnt/debug

Relink libc on binary

patchelf --set-interpreter ${pkgs.glibc}/lib/ ~/bin/hetty

Setup sound

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment