Introduction

SSH (Secure Shell) is a cryptographic network protocol widely used in the industry to securely access servers over an unsecured network. It enables remote login to computer systems, execution of commands on remote machines, file transfer, and tunneling, ensuring secure communication and data integrity.

Keys

SSH uses public-key cryptography to authenticate the remote computer and allow it to authenticate the user, if necessary. The SSH key pair consists of a private key and a public key. The private key is kept secret and should never be shared, while the public key can be shared with anyone.

Generate a new SSH key pair

RSA (default)
ssh-keygen
ed25519
ssh-keygen -t ed25519 -C "[email protected]"

View public key

cat ~/.ssh/id_rsa.pub

Configuration

It is possible to configure the SSH client to use the keys and other settings by editing the ~/.ssh/config file.

~/.ssh/config
Host *
IgnoreUnknown UseKeychain
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa

Connect to a remote server

To connect to a remote server using SSH, you need to know the username and the address of the server. The default port for SSH is 22, but it can be changed by the server administrator.

ssh username@address
Example
ssh [email protected]

Copy files via SCP

Secure Copy Protocol (SCP) is a command-line utility that allows you to securely copy files and directories between two locations. It uses the SSH protocol to encrypt the data during the transfer, ensuring data integrity and confidentiality.

Find the target file or dir

ssh username@address
cd path/to/file

Find current address

copy the desired path

pwd

Copy remotely to local

scp username@address:/path/to/file /path/to/destination/folder
Copy directory recursivelly
scp -r username@address:/path/to/dir /path/to/destination/folder

Error Troubleshooting

WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

Error when you dont add the ssh into the known hosts

Solution
ssh-keygen -R <host>

For example,

ssh-keygen -R 192.168.1.6