Skip to content

Instantly share code, notes, and snippets.

View hongkongkiwi's full-sized avatar

Andy hongkongkiwi

View GitHub Profile
hongkongkiwi /
Last active June 9, 2024 16:51
Increase swap on BRTFS. The normal method causes a swapon failed: Invalid argument error, so below commands are required.
sudo truncate -s 0 /swap.img
sudo chattr +C /swap.img
sudo fallocate -l 64G /swap.img
sudo chmod 0600 /swap.img
sudo mkswap /swap.img
sudo swapon /swap.img
sudo swapon --show
hongkongkiwi / update-mergerfs
Created March 7, 2020 06:26
Updates mergerfs to the latest version
#!/usr/bin/env bash
# Read a single char from /dev/tty, prompting with "$*"
# Note: pressing enter will return a null string. Perhaps a version terminated with X and then remove it in caller?
# See for dealing with multi-byte, etc.
function get_keypress {
local REPLY IFS=
>/dev/tty printf '%s' "$*"
[[ $ZSH_VERSION ]] && read -rk1 # Use -u0 to read from STDIN
# See regarding '\n' -> ''
hongkongkiwi / generate-ssh-key
Last active December 18, 2023 07:45
Simple one liner to generate an SSH key without a password in the default place with the comment as hostname then print out the public key for copy and paste.
HOSTNAME=`hostname` ssh-keygen -t rsa -C "$HOSTNAME" -f "$HOME/.ssh/id_rsa" -P "" && cat ~/.ssh/
hongkongkiwi / generate-dropbear-key
Last active December 17, 2023 16:38
Generate SSH Key in Dropbear with some options (by default it uses default id_dropbear as the name and ed25519 as the type)
#!/bin/sh +ux
# We set the sh +ux flags so that we error on undefined variables and error on bad commands
help() {
echo >&2 "$0 [-f] [-p] [-q] [<priv_key_file>] [<key_type>] [<key_comment>]"
echo >&2
echo >&2 "-q / --quiet to silent all output (except -p if passed)"
echo >&2 "-p / --pubkey to output public key after generation"
echo >&2 "-f / --force to force replacing existing key"
echo >&2
hongkongkiwi / ubuntu_fastest_package_mirror
Created June 20, 2022 04:49
One liner to find the fastest ubuntu mirror
curl -s | xargs -n1 -I {} sh -c 'echo `curl -r 0-102400 -s -w %{speed_download} -o /dev/null {}/ls-lR.gz` {}' | sort -g -r | head -1 | awk '{ print $2 }'
hongkongkiwi /
Last active November 16, 2023 23:38
Initialises Terraform HTTP backend to Gitlab. Here's a nice script which will ask for details in an interactive way.
# More information can be found at
terraform {
backend "http" {
hongkongkiwi / validate_authorized_keys
Last active October 3, 2023 04:25
Shell script to validate all authorized_keys in one or more files or all files in a directory
#!/bin/sh -ue
REMOVE_COMMENTS_SED='/^[[:blank:]]*(#|$)/d; s/#.*//'
if ! command -v "$SSH_KEYGEN_BIN" >/dev/null 2>&1; then
echo >&2 "I require $SSH_KEYGEN_BIN but it's not installed. Aborting."; exit 255
# Assume all keys are valid until told otherwise
hongkongkiwi /
Created November 23, 2017 03:57
Small script to setup ssh keys and configs on Unraid to persist after boot. Supports multiple users.
# Add this line into /boot/config/go where username is the user you want to setup, probably root
#/boot/config/ssh/ "username"
if [[ "$1" == "" ]]; then
echo "Invalid User!"
exit 1
hongkongkiwi /
Last active May 24, 2023 06:43
Simple shell script to count all files (or directories) in current directory, optionally can pass in a pattern for find
#!/bin/sh -u
# Count all files
# ./count
# Count only mp3 files
# ./count "*.mp3"
# Count only directories
# ./count "" "d"
# Count only directories ending with blah
# ./count "*blah" "d"
hongkongkiwi /
Last active April 28, 2023 03:41
Script to generate a Root CA, Intermediate CA and then to sign the Intermediate with the Root.
USER=`id -u -n`
GROUP=`id -g -n`