/james/notes/snippets

Spoof a MAC address on Linux

Change MAC address for the current session

ip link set dev enp42s0 down
ip set link dev enp42s0 address 00:11:22:33:44:55
ip link set dev enp42s0 up

Change MAC address on every boot

# Add a new udev rule
vi /etc/udev/rules.d/81-mac-spoof.rules
ACTION=="add", SUBSYSTEM=="net", ATTR{address}=="XX:XX:XX:XX:XX:XX", RUN+="/usr/bin/ip link set dev $name address YY:YY:YY:YY:YY:YY"

Where XX:XX:XX:XX:XX:XX is the original MAC address, and YY:YY:YY:YY:YY:YY is the new MAC address

Change MAC address for the current session on legacy systems

ifconfig eth0 down
ifconfig eth0 hw ether 00:11:22:33:44:55
ifconfig eth0 up

Install Docker on Ubuntu

# Add docker repository
sudo apt-get install ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install docker
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# Start on boot
sudo systemctl enable docker.service
sudo systemctl enable containerd.service
# Allow non-root user to access docker (Not a particularly great idea...)
sudo groupadd docker
sudo usermod -aG docker $USER
# Reboot and test with the hello-world container
docker run hello-world

Install VirtualBox on Ubuntu/Debian

wget -O- https://www.virtualbox.org/download/oracle_vbox_2016.asc | sudo gpg --dearmor --yes --output /usr/share/keyrings/oracle-virtualbox-2016.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/oracle-virtualbox-2016.gpg] https://download.virtualbox.org/virtualbox/debian $(lsb_release -cs) contrib" | sudo tee /etc/apt/sources.list.d/virtualbox.list
sudo apt-get update
sudo apt-get install virtualbox-6.1
# Host user must be part of vboxusers group to share USB devices
sudo usermod -a -G vboxusers $USER

Useful Linux Commands

Add a user to a group

sudo usermod -a -G <group> <user>
# E.g. Add user "james" to Virtual Box shared folder group
sudo usermod -a -G vboxsf james
# E.g. Add user "james" to serial group 
sudo usermod -a -G dialout james
# E.g. Add current user to docker group
sudo usermod -a -G docker $USER

Make a random binary file

# Make 2048 byte random file
dd if=/dev/urandom of=file.bin bs=2048 count=1

Split and combine files

# Split a single file into multiple 1GB files
# Output files will be named split_file.bin.aa, split_file.bin.ab, etc
split -b 1G input_file.bin split_file.bin.

# Combine multiple files into one
cat split_file.bin.* > combined_file.bin

View logs of systemd services

# View logs
journalctl -u service.name
# View live updating logs
journalctl -f -u service.name

TAR commands

# Compress contents of directory of files 
cd dir_name
# How to remember: Compress Zee Filez, Verbose me now
tar czfv ../archive.tar.gz .
# Extract the files into the current directory
# How to remember: eXtract Zee File, Verbose me now
tar xzfv archive.tar.gz ./

Allow port with iptables

Allow incomming connections to TCP port 8080 on interface eth0.

iptables -A INPUT -i eth0 -p tcp --dport 8080 -j ACCEPT
# Make persistent
netfilter-persistent save

GDB

Run program with command line arguments.

gdb program_name
run <command_line_arguments>

NFS

# Share a directory
vi /etc/exports
# Add read / write access from 10.0.4.X
/path/to/dir 10.0.4.0/24(rw,sync,no_root_squash)
# Restart NFS
sudo systemctl restart nfs

SSH

Enable access to server via SSH key.

# Create SSH key (RSA)
ssh-keygen -t rsa -b 4096
# Create SSH key (ED25519)
ssh-keygen -t ed25519

# Copy SSH key to remote server
ssh-copy-id -i ~/.ssh/key_file_name user@host

Disable SSH host key checking. Useful when connecting to embedded systems with initramfs, as the host key changes each time.

sudo vi ~/.ssh/config

Host 10.0.4.1
    StrictHostKeyChecking no
    UserKnownHostsFile=/dev/null

# Host file must be owned by root, and have no group / other write permissions
sudo chmod go-w ~/.ssh/config

Can use Host * to disable on all hosts, but not typically a good idea.

Fix "Failed To Load Selinux Policy, Freezing" error

Add selinux=0 to the end of the linux16 grub boot command to allow you to boot the machine.

Once booted run:

sudo yum reinstall selinux-policy-targeted

Find Commands

# Recursively search for <string> in any file
grep -ri "<string>" .
# Recursively search for file or directory with <string> in file name
find . -name "*<string>*"

Python Webserver

python -m SimpleHTTPServer 8080
python3 -m http.server 8080

Useful Aliases

Add aliases to: ~/.bashrc

# Save last command
alias savecmd='echo $(history -p !!) >> ~/saved_commands.txt'
# Search history (If Ctrl-R fails)
alias gh='history | grep'

Screen Shortcuts

Useful pip commands

Install from a .whl file

python -m pip install file.whl

Install from a git repository

# Repo is python package
pip install git+https://url_of_repo.git
# Repo contains python package in subdirectory
pip install git+https://url_of_repo.git#subdirectory=path/to/python/setup/file

Download package and dependencies

The following commands download a python package and all it's dependencies into the directory the command is run from.

# Downloads package for python version installed on the local machine
pip download <package_name>

# Download matplotlib for Python 2.7 on Windows
pip download matplotlib --only-binary=:all: --python-version 2.7 --platform win_amd64

# Download matplotlib for Python 3.9 on Linux
pip download matplotlib --only-binary=:all: --python-version 3.9 --platform manylinux1_x86_64
# Info about manylinux here: https://github.com/pypa/manylinux