Skip to content

Linux

Run sudo command via SSH on remote host

REMOTE_USER=user
REMOTE_HOST_IP_ADDR=192.168.10.82
REMOTE_HOST_SUDO_PASSWORD=user
REMOTE_COMMAND="sudo cat /etc/hosts"

echo ${REMOTE_HOST_SUDO_PASSWORD} | sshpass -p ${REMOTE_HOST_SUDO_PASSWORD} ssh -tt ${REMOTE_USER}@${REMOTE_HOST_IP_ADDR} "${REMOTE_COMMAND}"

Modify motd in linux

nano /etc/update-motd.d/99-auto-luks-tpm2-unseal

# refresh
sudo run-parts /etc/update-motd.d/

Broadcast to all user

wall I am dsync89!

Add ssh keys to server to VS code

IPADDRESS=192.168.10.236
PORT=22
SSH_USERNAME=user
OUT_FILE="${HOME}/.ssh/${IPADDRESS}/id_rsa"
mkdir -p "$(dirname "${OUT_FILE}")"
echo
ssh-keygen -t rsa -b 4096 -f ${OUT_FILE}
echo "Saving id_rsa to ${OUT_FILE}"

echo "copying key to ${IPADDRESS}"
ssh-copy-id -i "${HOME}/.ssh/${IPADDRESS}/id_rsa.pub" -p ${PORT} ${SSH_USERNAME}@${IPADDRESS}

Live follow kernel message

sudo dmesg -wT

Convert public key PEM to DER

# 1. create PEM public key
openssl rsa -pubout -in /etc/keys/privkey_evm.pem -out /etc/keys/pubkey_evm.pem

# 2. convert PEM pub key to DER
openssl rsa -pubin -inform PEM -in /etc/keys/pubkey_evm.pem -outform DER -out x509_evm.der

Mount a new rootfs

  # First, find and mount the new filesystem.

  mkdir /newroot
  mount /dev/whatever /newroot

  # Unmount everything else you've attached to rootfs.  (Moving the filesystems
  # into newroot is something useful to do with them.)

  mount --move /sys /newroot/sys
  mount --move /proc /newroot/proc
  mount --move /dev /newroot/dev

  # Now switch to the new filesystem, and run /sbin/init out of it.  Don't
  # forget the "exec" here, because you want the new init program to inherit
  # PID 1.

  exec switch_root /newroot /sbin/init

Run command in parallel

Such as find. @see https://stackoverflow.com/questions/28357997/running-programs-in-parallel-using-xargs for more info

find / \( -fstype rootfs -o -fstype ext4 \) -type f -uid 0 -print0 | xargs -0 -t -I % -P $(nproc) evmctl ima_hash %

Here's an example running commands in parallel in conjuction with find:

find -name "*.wav" -print0 | xargs -0 -t -I % -P $(nproc) flac %

-print0 terminates filenames with a null byte rather than a newline so we can use -0 in xargs to prevent filenames with spaces being treated as two seperate arguments.

-t means verbose, makes xargs print every command it's executing, can be useful, remove if not needed.

-I % means replace occurrences of % in the command with arguments read from standard input.

-P $(nproc) means run a maximum of nproc instances of our command in parallel (nproc prints the number of available processing units).

flac % is our command, the -I % from earlier means this will become flac foo.wav

Simple service unit

[Unit]
Description=Rclone Mount
After=network.target

[Service]
# Change the user and group variables here.
User=root
# Group=deluge23

Type=simple

# Change the path to Radarr or mono here if it is in a different location for you.
ExecStart=/home/deluge23/mountall.sh
TimeoutStopSec=20
KillMode=process
Restart=on-failure

# These lines optionally isolate (sandbox) Radarr from the rest of the system.
# Make sure to add any paths it might use to the list below (space-separated).
#ReadWritePaths=/opt/Radarr /path/to/movies/folder
#ProtectSystem=strict
#PrivateDevices=true
#ProtectHome=true

[Install]
WantedBy=multi-user.target

List hardware topo

apt install hwloc

Output as png

lstopo --of png > lstopo.png

Netplan config search stanza

The search stanza in netplan YAML will append the domain name in the host.

# This is the network config written by 'subiquity'
network:
  ethernets:
    enp6s18:
      addresses:
      - 192.168.10.221/24
      gateway4: 192.168.10.1
      nameservers:
        addresses:
        - 192.168.10.1
        search:
        - dsync89.com
  version: 2

For example, host wiki will becomes host wiki.dsync89.com

# host gitlab
wiki.dsync89.com has address 192.168.10.100

You can even SSH to the host directly without having to append the domain name!

ssh wiki will become ssh wiki.dsync89.com

You can use systemd-resolve --status and look at what is the default DNS server. The Current DNS Server should show the following:

...

Link 2 (enp6s18)
      Current Scopes: DNS
DefaultRoute setting: yes
       LLMNR setting: yes
MulticastDNS setting: no
  DNSOverTLS setting: no
      DNSSEC setting: no
    DNSSEC supported: no
  Current DNS Server: 192.168.10.1
         DNS Servers: 192.168.10.1
          DNS Domain: dsync89.com

Compress a lot of files using multi-core.

Move dir1 into tar, then compress it with the context of 12 sizes of dictionary per thread, show progress and write result to dir1.tar.xz.

tar Oc dir1 | pxz -D 12 -cv - > dir1.tar.xz

Extract

pixz -d <dir1.tar.xz> -o <output_dir>

Hardware Info

Summarize all info.

inxi -b

Show full Linux info.

inxi -F

Show top cpu.

inxi -t c

Show top memory.

inxi -t m

Rclone sync dir from local to remote machine

Actual copy

rsync -a --progress /opt/media/ remote_user@remote_host_or_ip:/opt/media/

Dry run

rsync -anv --progress /opt/media/ remote_user@remote_host_or_ip:/opt/media/

Create new partition on new raw disk

Following command will auto align, unit s is important. Leftover will be created as new free space.

parted /dev/sda
parted rm 1 # remove existing partition
(parted) mklabel gpt
(parted) unit s
(parted) mkpart primary ext4 0% 100%

Then format as ext4

mkfs.ext4 /dev/sda1

Mount NFS (Synology)

sudo apt install nfs-common
sudo mount 192.168.1.214:/volume1/Downloads /media/NAS/Downloads

Automount at boot Open /etc/fstab and add the following

192.168.1.214:/volume1/Downloads /media/NAS/Downloads nfs rsize=8192,wsize=8192,timeo=14,intr

Resize LVS to use all space

On Ubuntu installed using LVM

lvresize -l +100%FREE ubuntu-vg/ubuntu-lv --resizefs

Automount file system

Find the UUID

blkid

Make a mount point

mkdir /mnt/sdcard

Edit /etc/fstab

UUID=eb67c479-962f-4bcc-b3fe-cefaf908f01e  /mnt/sdcard  ext4  defaults  0  2

Format of the line

UUID=<uuid-of-your-drive>  <mount-point>  <file-system-type>  <mount-option>  <dump>  <pass>

INFO

default: Give users read and write access to the file system

pass: used by the fsck program to determine the order in which filesystem checks are done at reboot time. As you can see in this file, the value of the pass field for the root file system is 1. Swap partitions do not need to be checked and the value for them is zero. All other file systems should have a value of 2. So I set the pass value to 2 for my drive.

Refresh

mount -a

rsync copy without preserving the input file folder hierarchy

echo "Copying ${IN_CERT_PATH}/*.pem to ${OUT_CERT_PATH}..."
find ${IN_CERT_PATH} -name '*.pem' -exec rsync -R --no-relative {} ${OUT_CERT_PATH} \; > /dev/null # rsync copy without preserving input file folder hierarchy

if boot failed at initramfs, try to run the following root.

@ref: https://landley.net/writing/rootfs-programming.html

exec switch_root /root /sbin/init

Check if pictures is valid

Install tools

sudo apt install -y imagemagick file
shell
#!/bin/bash

# Check if the ImageMagick's identify command is available
if ! command -v identify &> /dev/null; then
    echo "Error: identify command (part of ImageMagick) is not installed."
    exit 1
fi

# Log file for invalid images
LOG_FILE="invalid_images.log"

# Clear the log file if it exists
> "$LOG_FILE"

# Function to check if a file is a valid image
check_image() {
    local file="$1"
    if file "$file" | grep -qE 'image|bitmap'; then
        if identify "$file" &> /dev/null; then
            echo "Valid image: $file"
        else
            echo "Invalid image: $file"
            echo "$file" >> "$LOG_FILE"
        fi
    fi
}

# Main function to traverse directories recursively
check_images_in_directory() {
    local dir="$1"
    while IFS= read -r -d '' file; do
        check_image "$file"
    done < <(find "$dir" -type f -print0)
}

# Ensure a directory is provided
if [ -z "$1" ]; then
    echo "Usage: $0 <directory>"
    exit 1
fi

# Start checking images in the provided directory
check_images_in_directory "$1"

echo "Invalid images have been logged to $LOG_FILE"