Tag: GitBash

  • Access Ubuntu VM via SSH with VirtualBox Port Forwarding

    VirtualBox is a fantastic tool for creating virtual machines (VMs), allowing you to simulate various operating systems on your computer. In this post, I’ll walk you through how I successfully accessed my Ubuntu Server VM, hosted in VirtualBox, from my Windows laptop using Git Bash via SSH. The key is using port forwarding, a simple but powerful feature in VirtualBox. Here’s how I did it and why it works.

    The Problem

    When you install an Ubuntu VM in VirtualBox, by default it uses NAT (Network Address Translation) for networking. This setup gives the VM an internal IP address, like 10.0.2.15, which is not directly accessible from your host machine (in my case, Windows). You may need to connect to your VM from the host OS to run commands via SSH or manage the server efficiently. This is where port forwarding comes in handy.

    What is Port Forwarding?

    Port forwarding allows you to redirect traffic from a specific port on your host machine to a port inside your VM. This means that although the VM is hidden behind a NAT network, you can still access it through the host by specifying the right port.

    In this scenario, I wanted to access my Ubuntu VM via SSH. By default, SSH operates on port 22 inside the VM. Using port forwarding, I mapped this to port 2222 on my Windows machine.

    Steps to Set Up Port Forwarding in VirtualBox

    1. Open VirtualBox and select the Ubuntu VM you want to configure.
    2. Click Settings > Network > Adapter 1 (which is usually set to NAT by default).
    3. Under Advanced, click Port Forwarding.
    4. Add a new rule:
    • Protocol: TCP
    • Host IP: Leave this blank (defaults to localhost).
    • Host Port: Set this to an unused port on your host machine, e.g., 2222.
    • Guest IP: Leave this blank (defaults to the VM’s internal IP).
    • Guest Port: Set this to 22 (the default SSH port in the VM).

    5. Save the settings and start your VM.

      Accessing the VM via Git Bash

      Once port forwarding is set up, you can easily SSH into the VM from your Windows machine using Git Bash or any other terminal application. Here’s how I did it:

      1. Open Git Bash on your Windows machine.
      2. Run the following SSH command:
         ssh username@localhost -p 2222

      Replace username with your Ubuntu username.

      1. You may get a warning about the authenticity of the host, which is normal when connecting for the first time. Type yes to continue.
      2. You should now see a welcome message from your Ubuntu VM, indicating that you’ve successfully connected.

      Why This Setup is Great

      • Simplicity: You don’t need to deal with complex network setups or IP conflicts. Port forwarding makes it easy to access your VM without changing your network configuration.
      • Flexibility: This setup works consistently across reboots. Once the port forwarding rule is set, you can close VirtualBox or restart your machine, and the settings will remain intact.
      • Security: Since the connection is local (via localhost), it’s generally secure for development purposes. If you need external access, there are additional security configurations you would need to consider.

      Conclusion

      Port forwarding in VirtualBox is a simple but effective way to access a VM from your host machine. By redirecting SSH traffic from port 2222 on my Windows laptop to port 22 on my Ubuntu VM, I can now easily manage my server from Git Bash. If you’re using VirtualBox for development or learning, I highly recommend giving this a try!

      Feel free to share your experiences or ask questions in the comments below. Happy coding!