How to SSH into Guest Linux Running in Virtual Box from Host Windows
Public key authentication allows you to access a server via ssh without password. Here are two methods to copy the public ssh key to the server.
- Generate the SSH key on windows with ssh-keygen and copy it.
ssh-keygen
cat .\.ssh\id_rsa.pub
2. Copy the public key to the Linux VM machine by any one of the ways mentioned below:
2.1 (Manually) Now log into the Linux VM image and check if touch /home/user_name/.ssh/authorized_keys
file exists. If not then create the file with proper permissions and paste the copied public key. Keep in mind that you have to create these new directories and files in the end user’s home directory, not your own (root/sysadmin).
# create the directory and file
mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys
# permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
# open the file to edit and paste the public key
vim ~/.ssh/authorized_keys
2.2 (Using command)
OpenSSH provides a handy tool call called ssh-copy-id
for copying ssh public keys to remote systems. It even creates required directories and files.
ssh-copy-id -i ~/.ssh/id_rsa.pub YOUR_USER_NAME@IP_ADDRESS_OF_THE_SERVER
3. Open the settings of the VM Machine. Switch the Network Adapter’s Attached to option to NAT.
4. Click on Port Forwarding, a rules window will open.
5. Now to add SSH port forwarding rule, click on + icon on top right. Then add 22 to both host and port column. If you want to customize the port for host you can do it also.