The Tech Cheat Sheet

For whenever I forget random silly things like "how to exit vi" (I'm an Emacs guy).

ISO's

HTTP

RTFM

curl

Dump headers too

curl -D/dev/stderr http://httpbin.org/ip

Ignore SSL errors (watch out!)

curl -k https://httpbin.org/ip

Continue interrupted download

curl -C- -o file http://httpbin.org/ip

Basic auth

curl -u user:pw http://httpbin.org/ip

Sharing a bunch of files

Simple HTTP with Python

python -m SimpleHTTPServer 5380

FTP

Save time - use HTTP to simply serve some files, use scp (aka sftp) to give authenticated read-write access.

SFTP aka scp

Amend /etc/ssh/sshd_config:

Subsystem sftp internal-sftp
Match Group sftpusers
    X11Forwarding no
    AllowTcpForwarding no
    ChrootDirectory /jail
    ForceCommand internal-sftp

Add the sftpusers group, add a default sftp user, create a jail with data:

groupadd sftpusers
useradd -g sftpusers -d /jail/data -s /sbin/nologin sftp
mkdir -p /jail/data && chown root:root /jail && chmod 755 /jail

DNS

Closest OpenNIC public access servers

If you like Google:

nameserver 8.8.8.8
nameserver 8.8.4.4

dig

bind

Gentle restart:

named-checkconf
rndc reload

vim

No Emacs cheat sheet. It's already wired in my brain, like breathing.

git

Find the commit that broke something

git-bisect(1) to the rescue.

(Optional, but simplifies things a lot) Write a short script to test for the broken thing. Save as ./test-broken.

Go back to repo root and start bisecting:

git bisect start
git bisect bad

Go back to the last known working version, verify as good, mark as good:

git checkout 1234abcd
./test-broken
git bisect good

Option 1: if you wrote the script:

git bisect run ./test-broken

Option 2: become human for-loop. Check if it works, and run either git bisect good, git bisect bad, or git bisect skip until there is only one suspect left.

When done:

git bisect reset

Rewrite history

git-rebase(1) to the rescue.

First off, find the commit that broke something.

Start an interactive rebase, add ~1 after the commit hash to go one commit earlier.

git rebase --interactive badb33f5~1

Your editor will pop up. You'll see something like this:

pick f00dc4f3 let's have coffee
pick 1043d1d0 I love you
pick badb33f5 break stuff

Go to the line with badb33f5 and change the leading word pick to edit (or e). Save file and close it.

Fix the broken code. Did you write ./test-broken? Run it.

Amend the broken commit:

git commit --amend -m "don't break stuff"

Continue rebasing, fixing any merge conflicts on the way:

git rebase --continue

Rewrite published history

git push --force

Configure branch to track a remote

In .git/config:

[branch "master"]
    remote = origin
    merge = refs/heads/master

Being a douche

Can be used with --amend on existing commits.

Impersonate someone else

git commit --author "Kamil's Evil Twin <kamil@trollcat.example.com>"

Forge commit timestamp

git commit --date=2015-04-01T00:00

UNIX

Makefile

"Phony" rules that never produce files:

.PHONY: all lint run deploy clean
.PHONY: *

Shellout:

BIN = ${shell find bin -type f -perm /u=x} ${shell find . -name "*.py"}

List of files with extension .a that must become files with extenion .b:

B_FILES = ${A_FILES:.a=.b}

More complex variable substitution is possible using % pattern matching:

DEST_FILES = ${SRC_FILES:%=${DEST}/%}

Make .b files from .a files and some other stuff:

%.b: %.a otherfile.c
    atob --in $< --out $@ --extra-c otherfile.c

Suppress echo:

run:
    @ ./dev_app.py

Ignore exit status:

clean:
    - rm .coverage

Current git branch as a slug:

BRANCH_SLUG ?= $(shell git rev-parse --abbrev-ref HEAD | sed -E 's/[_/]+/-/g')

Print any makefile variable (use e.g. make print-BIN):

print-%: ; @echo $*=$($*)

Remove all default suffixes (implicit rules):

.SUFFIXES:

sed

Change a line:

sed -E 's/pattern/substitution/g'

Delete a matching line:

sed -E '/pattern/d'

Backups:

sed -i~

RAID

Disks are cheap! TLDR: if you care about the data, use RAID1; if you also care about performance, use RAID10; if you only need scratch space or are tight on budget, RAID5 is adequate.

0

1

10

5

6

Email

Important headers

X-Priority: 1 (Highest)
X-MSMail-Priority: High
Importance: High

With mail / sendmail

$ sendmail -t
From: me
To: test@example.com
Subject: Subject

Message

.
$ mail -s "Subject" test@example.com
Message

.

Reading mbox files, for humans

mutt -f ./mbox

Discovering things

Did it ever occur to you that you've been restarting a machine so rarely that you forgot which bootloader it had installed?

dd if=/dev/sda count=512 | strings | grep -oiE 'grub|lilo'

Stupid hacks

furl

For some very odd and troubling reason, stuck without curl, wget, or any other HTTP client?

furl() {
    host=$1
    url=$2
    printf "GET $url HTTP/1.1\r\nHost: $host\r\nConnection: close\r\n\r\n" |\
            nc $host 80
}
furl example.com /index.html

Layer 2 networking


See this as plaintext. Get the permalink. Check out related. Go home.