You can achieve this by forwarding ports to the virtual machine.
Make sure your virtual machine has openssh-server
installed and running!
Open VirtualBox -> Click on your VM -> "Settings" -> "Network" -> "Advanced" -> "Port Forwarding"
Click the icon in the top right which has a plus (+) symbol to add a port forwarding rule and enter the following data:
Name: ssh (or whatever you want)
Protocol: TCP
Host IP: 127.0.0.1
Host port: 2222
Guest port: 22
Note that the host port is not the usual ssh
port of 22.
Now you can ssh into your virtual machine!
ssh user@localhost -p 2222