Skip to content

Instantly share code, notes, and snippets.

@algorythm
Last active May 25, 2021 14:59
Show Gist options
  • Save algorythm/d881240da8b419406a732449ea3219ea to your computer and use it in GitHub Desktop.
Save algorythm/d881240da8b419406a732449ea3219ea to your computer and use it in GitHub Desktop.
UNIX Tips and Tricks

Introduction

This Gist contains a few tips and tricks that I've gathered over time to optimize my experience when working on unix based systems. Some of the things mentioned in this document, will explained in further detail below. This file will contain things in a more compact manner.

General Networking

Ping

Ping though a specific NIC:

If you have multiple NICs, i.e.

eth0 10.0.0.2
eth1 172.16.0.6

You can ping through a particular NIC using -S:

ping google.com -S 10.0.0.2     # through eth0
ping 10.0.0.1 -S 10.0.0.2       # through eth0
ping 172.16.1.123 -S 172.16.0.6 # through eth1

Show time:

# on macOS
ping --apple-time 1.1.1.1

Ping every n seconds:

ping -i 2 1.1.1.1 # every 2 seconds

Ping 1.1.1.1 every 5 seconds through NIC on 10.0.0.2 with time:

ping --apple-time -i 5 -S 10.0.0.2 1.1.1.1

File Sharing

Using HTTP

On sender's end, cd into the directory you want to serve files from. Then

python3 -m http.server {port}

The file can now be downloaded on http://{ip}:{port}/relative/path/to/file, i.e.

curl -O http://127.0.0.1:1234/my-file.txt

Using NC

For a simpler usecase, we can use netcat

Receivers End:

nc -lp 1234 > out.file
# or on macOS:
nc -l 1234 > out.file

Senders End:

nc -w 3 {ip} {port} < in.file

A cool use-case is for sending a file to a server:

ssh [email protected]
nc -lp 1234 > my.file & # backgrounds the process, this will end once the file has been received
exit
nc -w 3 my.host 1234 < my.file # mostly useful on local networks, as the port (1234 in this case) has to be open
# the process on the server will end once the file has been uploaded

SSH

Key Management

Generate an SSH key

ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/id_ed25519 -C "[email protected]"

Get Fingerprint for Public Key

SHA256:

ssh-keygen -lf ~/.ssh/my-key.pub

MD5:

ssh-keygen -lE md5 -f ~/.ssh/my-key.pub

Port Proxy

Forward a port on a server to your local machine:

ssh -L {local port}:127.0.0.1:{remote port} [email protected]
# ex
ssh -L 9999:127.0.0.1:1433 # forwards port 1433 on server 127.0.0.1 to your local machine on port 9999

Kill a Stale Session

Sometimes the SSH connection gets stale, and makes your terminal hang. To get out of this (to end the session), type ~., and it should stop the process.

# Sending files back and forth
## Without Compression
**On the receiving end**:
```
nc -l -p 1234 > out.file
```
or on macOS
```
nc -l 1234 > out.file
```
**On the sending end**:
```
nc -w 3 [destination] 1234 < out.file
```
## With Compression
**Receiving End**:
```
nc -l 1234 | uncompress -c | tar xvfp -
```
**Sending End**:
```
tar cfp - /some/dir | compress -c | nc -w 3 [destination] 1234
```
Source: [nakkaya.com](https://nakkaya.com/2009/04/15/using-netcat-for-file-transfers/)
# Encrypted Files
**Add receiver**:
```
gpg --search-key [e-mail address]
```
**Encrypt**:
```bash
gpg --encrypt --sign --armor -r [e-mail address] <-vv for debug> [file.out]
# Generates file.out.asc
```
**Decrypt**:
```bash
gpg --decrypt file.out.asc
```

Tips and Tricks for UNIX

Here is a few tips and tricks I have learned through the years for UNIX (Linux/MacOS). Useful tips.

Bash Script Headers

Safe headers to make your bash scripts fail faster:

#!/usr/bin/env bash
set -euo pipefail # important
IFS=$'\n\t'       # less important

http://redsymbol.net/articles/unofficial-bash-strict-mode/

Directory of Script

CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

Check if variable is set

if [[ -z "${myVariable}" ]]; then
   echo "Variable isn't set"
else
   echo "Variable is set!"
fi

Upper Case / Lower Case

Bash:

$ var="hELlo"
$ echo ${var,,} # Lower-case
hello
$ echo ${var^^} # Upper-case
HELLO
$ echo ${var^}  # First character uppercase
HELlo

ZSH:

$ var="hELlo"
$ echo ${var:l} # Lower-case
hello
$ echo ${var:u} # Upper-case
HELLO

SSH Sessions

Ever needed to forward a local port from a server to your own computer? I.e. forward a database port for a database server behind a firewall to your local computer. It's easily done with:

ssh -L {local_port}:127.0.0.1:{remote_port} [email protected]
# i.e.
ssh -L 9999:127.0.0.1:1433

Above command will map port 1433 from the remote server to port 1234 on your local computer. But what if you already have an active SSH session to that server, and is waay to lazy (and let's be honest here: you are) to restart the session? No worries, you can use the tilde (~) commands. Type ~? to get a list of available commands. The one you are looking for is ~C, which let you to open a command line allowing you to "add SSH parameters" to your current session, such as the -L we used before:

ssh [email protected]
~C
-L 9999:127.0.0.1:1433

Bash script colors

Checkout https://misc.flogisoft.com/bash/tip_colors_and_formatting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment