Home

Terminal

Command History

Display a list of the last used terminal commands

history

Search for keyword in history

history | grep <keyword>

Execute specific command

!<number> // e.g. !50

Set timestamp for each command (for the current session)

export HISTTIMEFORMAT="%F %T "

Use a specific command (opens the default editor)

fc <number> // command runs after exit, to skip delete the command line and exit without saving

Kill process

On Linux And Mac

1. lsof -i:< port number > # Get the PID of the running port
2a. sudo kill -15 < PID >
2b. sudo kill -9 < PID > # Kill port if flag -15 doesn't work

Shortcut

sudo kill -9 $(sudo lsof -t -i:<port number>)

List open ports

sudo lsof -i -P -n | grep LISTEN

or

sudo netstat -tulpn | grep LISTEN

Check a specific port

sudo lsof -i:<port>

On Windows (Run Terminal With Admin Rights)

1. netstat -ano | findstr :< port number > # Get PID of the running port
2. taskkill /pid < PID > /F

With kill-port Package

1. npm i kill-port
2. npx kill-port < port >

Creating SSH Keys

On Linux

Checking for existing SSH keys

`ls -al ~/.ssh`  # show files inside the .ssh folder

# check for the following two files

id_ed25519 # private key (keep this secret!)
id_ed25519.pub # public key (can be shared)

# or if unsure, display all private keys (even with custom name)
find ~/.ssh -type f -name "id_*" ! -name "*.pub"

to create .ssh folder:

mkdir ~/.ssh
chmod 700 ~/.ssh

Generating a new SSH key and adding it to the ssh-agent

1. ssh-keygen -t ed25519 -C "your_email@example.com" # -C and the email is optional it could just be a string, e.g. "GitHub Key"
2. # A filename is asked, with just <enter> the default name is used

# Next steps are only needed if a passphrase has been added (though some tools, e.g. GNOME Keyring or macOS Keychain could benefit from it)
# They assure the passphrase needs to be entered only once and is remembered within the session

3. eval "$(ssh-agent -s)" # starts the ssh agent
4. ssh-add ~/.ssh/id_ed25519 # adds the SSH private key to the running SSH agent

Adding a new SSH key to your GitHub account

Copy the SSH public key to your clipboard.

cat ~/.ssh/id_ed25519.pub

Then select and copy the contents of the id_ed25519.pub file displayed in the terminal to your clipboard

Add SSH key to GitHub

1. In the upper-right corner of any page, click your profile photo, then click Settings.
2. In the "Access" section of the sidebar, click  SSH and GPG keys.
3. Click New SSH key or Add SSH key.
4. In the "Title" field, add a descriptive label for the new key. For example, if you're using a personal laptop, you might call this key "Personal laptop".
5. Select the type of key, either authentication or signing. For more information about commit signing, see "About commit signature verification."
6. Paste your key into the "Key" field.
7. Click Add SSH key.

Testing your SSH connection

ssh -T git@github.com

If successful it should print:

> Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.

On Windows

Check for existing key

ssh-add -l

Create key

1. ssh-keygen -t rsa -b 4096 -C "your-github-mail@example.com"
2. Enter to accept default file location
3. Enter to accept empty secure passphrase

Location of the file containing the key (id_rsa.pub)

C:\Users\YourUsername\.ssh\id_rsa.pub

Navigate to the file and display the content of public key

cat .ssh/id_rsa.pub

Add key to Github

1. Copy content of public key
2. On Github: Settings > SSH and GPG keys > New SSH key
3. Enter title
4. Paste key into the key field and submit

Testing is similar to Linux

Testing Passphrase for key

ssh-keygen -y -f ~/.ssh/<key-name>

If passphrase was correct, the key will be shown otherwise an error message

Delete old key from authorized_keys

ssh-keygen -R <ip>

SSH with a non-standard key

-vvv shows which key ssh tries to use

ssh -i ~/.ssh/<key-name> root@<server-ip> -vvv

Create an SSH config file

nano ~/.ssh/config

Host < custom-name >
    HostName < server-ip >
    User < user name for ssh login, e.g. deploy >
    IdentityFile ~/.ssh/< your_custom_key_name >
    IdentitiesOnly yes

#Usage
ssh < custom-name >

cURL (Client URL)

Syntax

cURL [options] [URL]

GET Request

curl http://localhost:3000/posts

POST Request

-X POST # Set the HTTP method
-H "Content-Type: application/json" # Set the header. Default is application/x-www-form-urlencoded
-d '{"user": "John", "id":1}' # Set the body data

# Complete request:
curl -X POST -H "Content-Type: application/json" -d '{"User": "John", "id": 1}' http://localhost:5000/posts

Content-Type Options

application/x-www-form-urlencoded (default): -d "field1=value1&field2=value2"

application/json: -d '{"User": "John", "id": 1}'

# By using the -F flag, Content-Type is automatically set to multipart/form-data
multipart/form-data: -F "field1=value1" -F "file=@example.txt"

# By using the --data-binary flag, no Content-Type is set, is has to be done manually if needed
curl -X POST --data-binary "@file.json" https://example.com/api

Generate random bytes (for token, etc.)

Random Base64, 32 Byte string

openssl rand -base64 32