SSH - erikpa1/MyExperience GitHub Wiki

Source tutorials:

SSH port forwarding

Basic connection commands

ssh -N -L 18789:127.0.0.1:18789 <username>@<device.ip>

#For custom port

ssh -p <non22port> -N -L 18789:127.0.0.1:18789 <username>@<device.ip>

MongoDB example:

ssh -N -L 27017:127.0.0.1:27017 <username>@<device.ip>

-N

-N tells SSH not to run any remote command or open a shell. You only get the connection, which is all a tunnel needs. Without it you'd also land in a shell on the VM. The terminal just sits there while the tunnel is open; Ctrl+C closes it.

-L

-L sets up local port forwarding, in the format localPort:targetHost:targetPort. So -L 27018:localhost:27017 means connections to port 27018 on your machine travel through the SSH connection and come out on the VM, going to localhost:27017. Here localhost is resolved on the VM, not on your PC, so it points to the Mongo instance running there.

Terminal file transfer

TODO

ZED IDE usage

In ZED you can open SSH session in the same way as you do in Visual Studio Code. For activation of command palete use Ctrl + Shift + P and enter the SSH configuration.

image

If you used already SSH connection you automatically get last folder, you asked to open.

image

Utils

Find ssh (mac, linux):

which ssh

SSH files

All files lies in:

cd ~/.ssh
ls

After LS files you can find:

  • known_hosts, this file contains list of fingerprints of every linux you have already connected.
  • **id_ed25519
  • **id_ed25519.pub -

Config files

To not manualy type SSH IP adress and user, you can use ssh config file:

cd ~/.ssh

Demo template looks like following:

Host <YOUR_FRIENDLYNAME>
  Hostname <IP_OR_URL>
  Port 22 # default
  User root

SSH key-gen

You just have to write:

ssh-keygen

After systems asks you to enter folder, default is "/home/[USER]/.ssh/id_rsa"

WARNING! - if you override existing key in that folder, you will permanently lost the access to that server. On cloud it's mostly not a big deal but in local server room, can be problem, you can lost all your data.

After entering SSH key path, you will be asked for passphrase, passphrase is another layer of security, it's password to your key. By hitting enter twice, you can skip adding and you make passwordless key.

Tutorial RSA pubkey

You can check public key with:

cat id_rsa.pub

Deploying SSH key for authentification - multi step

First you have create .ssh folder if you don't have a one.

mkdir .ssh
cd .ssh
nano authorized_keys

Than paste your public key to it. Keys are single line, they are visible as multi line in cat.

image

Deploying SSH key for authentification - single command

ssh-copy-i -i ~/.ssh/id_rsa.pub [USER]@[SERVER_IP]

-i stands for input file

Adding another type of SSH Key

ssh-keygen -t ed25519 -C "acme"

-t means type

-C means comment

ed25519 - another type of crypto key, this one should be more secure than default RSA

"acme" means nothing special it's just purpose of the certificate, for acronym check acronyms

When ed25519 is successfully copied on server with ssh-copy-id, you can choose to log in with that certificate.

ssh -i <PATH_TO_FILE_ED25519> <USER>@<IP_ADDRESS> 

Configuring SSH agent

In Linux with UI (or graphics card) this agent should be enabled by defalt. At first let's check if it's running:

ps aux|grep ssh-agent
GREP SSH agent

To get PID call:

eval "$(ssh-agent)"

which can output as:

Agent pid 26815

If agent is running you can add your ACME certificate to that agent which will cache your passphrase for you so you don't have to enter it every time you wan't to connect to the SSH session.

ssh-add ~/.ssh/<YOUR_CERT_ed25519>

After command enter passphrase.

SSH server configuration

At first check running ssh daemon (sshd) with:

which sshd

#or

systemctl status sshd

Debugging

Debug authentifications to SSH

With tail, you are able to login attempts.

tail -f /var/log/auth.log

This shows the systemd journal logs for the ssh service unit (the OpenSSH server, sshd). The -u flag filters logs to a specific unit, so you only see entries from that service. On some linux distributions this can be called ssh(d)!

journalctl -u ssh -f              # follow live, like tail -f
journalctl -fu ssh                # shorten of the one above
journalctl -u ssh -n 50           # last 50 lines
journalctl -u ssh --since today   # only today's entries
journalctl -u ssh --since "1 hour ago"
journalctl -u ssh -r              # newest first
journalctl -u ssh -b              # since last boot
journalctl -u ssh | grep Failed   # just failed logins

-f - means follow

Example:

image

Getting detailed informations about ssh connection.

ssh -v [USER]@[YOUR_IP]
⚠️ **GitHub.com Fallback** ⚠️