sudo apt-get install openssh-server
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]
# It will ask for the root password in the remote machine
ssh -t [email protected] "sudo command"
/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
Access servers with a public/private key instead of a password. This is a much more secure method.
Transfer the public key id_rsa.pub
to the remote server, in the ~/.ssh/authorized_keys
file, with this command:
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
If the keys don't exist on local machine, generate them:
ssh-keygen
# keys are stored in /home/user/.ssh
ls ~/.ssh
The private key id_rsa
stays in the local machine.