Skip to content

Latest commit

 

History

History
executable file
·
81 lines (55 loc) · 2.22 KB

ssh.md

File metadata and controls

executable file
·
81 lines (55 loc) · 2.22 KB

Install

sudo apt-get install openssh-server

Connect

ssh -p PORT USER@SERVER_IP

# Execute one command and exit.
ssh -p PORT USER@SERVER_IP ls

SSH in Virtual Machine needs a port forwarding rule in network settings for the VM. Name ssh, host port 3022, guest port 22.

# SSH to local VM via port forwarding.
ssh -p 3022 [email protected]

Alias

/etc/ssh/ssh_config   # system-wide
~/.ssh/config         # per user (better)

# Add this in the config.
Host server_name
  Port 22
  User user
  HostName 123.456.789.255

# Connect with this now.
ssh server_name

SSH with RSA key

This is used to log in without writing the password each time. Also, it is much more secure.

This works by using a private id_rsa and public id_rsa.pub key pair. These keys can be generated by running ssh-keygen, and they are located in ~/.ssh i.e. /home/user/.ssh.

The id_rsa private key stays in the host machine in ~/.ssh. The id_rsa.pub public key goes on the server in a ~/.ssh/authorized_keys file. Simple as that, the next ssh is password-less.

To transfer a public key, use this from ~:

rsync -av -e 'ssh -p 3022' ./.ssh/id_rsa.pub [email protected]:~/.ssh/

Transfer files over SSH with rsync

Only trasnfer the files that have changes or are missing. Much faster and secure than FTP.

-a - Recursion and preserve everything.
-v - Transfer log.
-e - Specify remote shell.

# Transfer everything from the current directory to a new folder in the remote home directory.
rsync -av -e 'ssh' . [email protected]:~/folder/

# Transfer folder1 and its content to remote home directory
rsync -av -e 'ssh' ./folder1 [email protected]:~/

# Transfer everything from folder1 without the folder itself to remote folder2 in remote home directory.
rsync -av -e 'ssh' ./folder1/ [email protected]:~/folder2/

# Transfer multiple files and folders.
rsync -av -e 'ssh' ./file1 ./file2 ./folder1 [email protected]:~/folder2/

To Virtual Machine

rsync -av -e "ssh -p PORT_NUMBER" <SOURCE> <DESTINATION>:<PATH>

rsync -av -e 'ssh -p 3022' . [email protected]:~/make-this_folder