General

General

CheatSheet General Commands

Starting a Simple Python HTTP Webserver (Windows / Linux)

To quickly serve files over HTTP, you can use Python’s built-in http.server module. Here’s how to start a simple HTTP server on port 9000:

python3 -m http.server 9000

Installing Nano Text Editor (Linux)

Debian/Ubuntu:

sudo apt-get install nano

Red Hat (Fedora, CentOS):

sudo dnf install nano

Update and Clean System (Linux)

Update your system’s package list, upgrade installed packages, and remove unnecessary packages to keep your system clean.

sudo apt update && sudo apt upgrade -y && sudo apt autoclean -y && sudo apt autoremove -y

Hashing Files with Certutil (Windows)

If you need to verify the integrity of a file on Windows, you can use certutil.exe to generate a SHA256 hash. Here’s how:

certutil.exe -hashfile .\file.txt SHA256

Replace file.txt with the path to your file.

SSH Connect Commands with a Private Key (Windows / Linux)

To connect to a remote server using SSH and a specific private key, use:

ssh -i path/to/your/private_key username@remote_host

Example:

ssh -i .\ssh-key.key [email protected]

Ensure that your public key is added to the remote user’s ~/.ssh/authorized_keys file to allow authentication.

SCP Commands (Windows / Linux)

Copying from Remote Linux to Local Windows

To copy a file from a remote server to your local machine, use:

scp -i path/to/your/private_key username@remote_host:/path/to/remote/file .

Example:

scp -i .\ssh-key.key [email protected]:/etc/linkding/data/backup.zip .

Copying from Local Windows to Remote Linux

To copy a file from your local machine to a remote server, use:

scp -i path/to/your/private_key local_file.ext username@remote_host:/path/on/remote/server/

Example:

scp -i .\ssh-key.key backup.zip [email protected]:/home/ubuntu/

Zip and Unzip Files (Linux)

Installing Zip/Unzip

If unzip is not installed on your Linux system, you can install it using:

sudo apt-get install zip unzip

Zipping a Folder Recursively

To zip a folder and all its contents recursively, use:

zip -r archive_name.zip folder_to_zip

Example:

zip -r backup.zip data/

Unzipping a File

To unzip a file to a specified directory, use:

unzip file.zip -d destination_folder

Example:

unzip backup.zip -d data/

Iptables (Linux)

Iptables is a command-line firewall utility that uses policy chains to allow or block traffic.

Listing Iptables Rules

To list the current iptables rules:

sudo iptables -L

Listing Iptables Rules with Numeric Ports

To display port numbers instead of service names, use the -n argument:

sudo iptables -L -n

Saving Iptables Rules

To save your current iptables rules to a file:

sudo iptables-save > iptables.v4.backup

Restoring Iptables Rules

To restore iptables rules from a saved file:

sudo iptables-restore < iptables.v4

You might also want to copy the backup file to a working file before restoring:

cp iptables.v4.backup iptables.v4
sudo iptables-restore < iptables.v4

Docker

Quick Installation Script (Linux)

To quickly install Docker on supported Linux distributions, you can use the following script. First, download the script:

curl -fsSL https://get.docker.com -o get-docker.sh

Next, make the script executable:

chmod +x get-docker.sh

Finally, execute the script:

./get-docker.sh

Listing Docker Containers

Before accessing a container, you need its name or ID. Use the following command to list running containers:

docker ps

Accessing a Running Container with Bash

To open a Bash shell inside a running Docker container:

docker exec -it <container_id_or_name> bash

Stopping All Running Docker Containers

To stop all currently running Docker containers:

docker kill $(sudo docker ps -q)

How It Works: The docker ps -q command lists only the IDs of all running containers. These IDs are then passed to docker kill, which stops all the specified containers.

Cleaning Up Docker Containers

To remove all unused containers, images, networks, and volumes:

docker system prune --all --volumes

--all removes all unused images, not just dangling ones.
--volumes also removes all unused volumes.
By default, Docker will prompt for confirmation. To skip the prompt, add --force.

Viewing Docker Container Logs

To view the logs of a specific Docker container:

docker logs wg-easy

Hextra Theme Update

Update All Hugo Modules

To update all Hugo modules in your project to their latest versions, run:

hugo mod get -u

Update Hextra Specifically

To update Hextra to the latest released version, execute:

hugo mod get -u github.com/imfing/hextra

Git (Windows / Linux)

Pushing Without Triggering CI/CD Actions

Use special keywords in your commit message to prevent automated pipelines (like GitHub Actions) from running for that specific push:

Prepare all modified files to be committed:

git add .

Create the commit, adding a specific keyword phrase to the commit message. Common keywords include [skip ci], [ci skip], [no ci], [skip actions], or [actions skip]:

git commit -m "Your commit message [skip ci]"

Upload your commit(s) to the main branch on the remote named origin:

git push origin main

PowerShell (Windows)

Check Laptop Battery Status

Retrieves detailed battery information using WMI (Windows Management Instrumentation):

gwmi -Class BatteryStatus -Namespace root\wmi

Check integrity of Windows

Scans and repairs system files:

sfc /scannow

Repairs Windows image files:

DISM.exe /Online /Cleanup-image /Restorehealth

Python Virtual Environment (Linux)

Creating and Activating a Virtual Environment

Create a new Python virtual environment in your current directory:

python3 -m venv aws-env

Activate the virtual environment:

source aws-env/bin/activate

To exit the virtual environment when finished:

deactivate