I have vagrant vm centos 7 running ssh on XXXX port (not default 22)
How can I connect to XXXX port using "vagrant ssh" command
I tried this but did not work.
config.vm.network :forwarded_port, guest: 22, host: 2222, id: "ssh" , disabled: true
config.vm.network :forwarded_port, guest: xxxx, host: 2223, auto_correct: true
config.ssh.port = 2223
It might depend on the provider you're using, but setting config.ssh.guest_port=XXXX along with a port forwarding does the trick for me when using Virtualbox as a provider.
With that you shouldn't even have to specify config.ssh.port, as Vagrant will detect the port forwarding settings automatically.
See also vagrantfile documentation
Wandering what configuration #su_li used exactly, I tried different possibilities and the following code came to work as expected :
config.vm.network "forwarded_port", guest: 54321, host: 2222, id: "ssh"
config.ssh.guest_port = 54321
The guest OS (the VM) runs sshd on port 54321.
The host OS will send ssh request to port 2222.
With this configuration, all requests to port 2222 on the host side will be forwarded to port 54321 on the guest side.
And all responses from port 54321 from the guest side will be forwarded to port 2222 on the host side.
Note that the id: "ssh" part is necessary if you want to override the default ssh port forwarding configuration.
Related
VirtualBox Port Forwarding on Windows 7 not Working
Im trying to ssh onto my VirtualBox from my Windows 7 host via port forwarding, but VirtualBox wont open the port for listening. I can connect to it by turning on the VirtualBox GUI and navigating via that terminal, but I cannot connect via a standard ssh client from my host. I want to be able to ssh on port 2222 on my host to the guest.
Here's my setup:
Host: Window 7 SP1
Guest: Ubunto Ubuntu 12.04
VirutalBox: 4.3.26
Host Processor: Intel Core i7 920
The guest machine is configured as a NAT and port forwarding is enabled for 127.0.0.1 for host port 2222 to guest port 22.
The output from ifconfig on the guest:
eth0 inet addr:10.0.2.15 Bcast:10.0.2.255 Mask: 255.255.255.0
The output from ps -ef | grep sshd on the guest:
root 625 1 0 12:27 ? 00:00:00 /usr/sbin/sshd -D
The output from netstat -ant | grep 22 on the guest:
tcp 0 0 0.0.0.0:22 0.0.0.0:*
tcp6 0 0 :::22 :::*
But on the host, netstat -ant | grep 2222 doesnt show anything.
In the VBox.log however I have this:
00:00:03.413790 NAT: set redirect TCP host 127.0.0.1:2222 => guest 10.0.2.15:22
00:00:03.424301 supR3HardenedErrorV: supR3HardenedScreenImage/LdrLoadDll: rc=VERR_LDRVI_UNSUPPORTED_ARCH fImage=1 fProtect=0x0 fAccess=0x0 \Device\HarddiskVolume2\Windows\mfnspstd64.dll: WinVerifyTrust failed with hrc=Unknown Status 0x800B0101 on '\Device\HarddiskVolume2\Windows\mfnspstd64.dll'
00:00:03.424422 supR3HardenedErrorV: supR3HardenedMonitor_LdrLoadDll: rejecting 'C:\Windows\mfnspstd64.dll' (C:\Windows\mfnspstd64.dll): rcNt=0xc0000190
00:00:03.424476 NAT: failed to redirect TCP 127.0.0.1:2222 => 10.0.2.15:22
The last line looks like the suspect but there's no clue as to why it fails to redirect. I've tried all of the following from various other posts and forums but cant get it to listen on any port on the host:
Turned off the firewall
Changed the port
Enabled VT-X on BIOS
Disabled Hyper-V
Tried numerous different builds of VirtualBox
Any help would be much appreciated. Works fine on my Mac Book with OS-X.
Did you set forwarding in machine settings ?
To forward ports in VirtualBox, first open a virtual machine’s settings window by selecting the Settings option in the menu.
Select the Network pane in the virtual machine’s configuration window, expand the Advanced section, and click the Port Forwarding button. Note that this button is only active if you’re using a NAT network type – you only need to forward ports if you’re using a NAT.
Use VirtualBox’s Port Forwarding Rules window to forward ports. You don’t have to specify any IP addresses – those two fields are optional.
Also here: http://www.howtogeek.com/122641/how-to-forward-ports-to-a-virtual-machine-and-use-it-as-a-server/
I found the solution in a hypernode-vagrant issue: VirtualBox fails to establish the port forwarding for SSH on Vagrant's standard port 2222, but higher ports work. In that issue, ports >= 4000 worked, whereas ports <= 3500 would fail. On my machine running Windows 10, I found 2380 to be the first port for which the TCP redirect can be established.
The port on the host used for forwarding can be changed by adding the following lines to your Vagrantfile (where you may have to replace 4000 by a higher number):
config.vm.network :forwarded_port, guest: 22, host: 2222, disabled: true
config.vm.network :forwarded_port, guest: 22, host: 4000, id: "ssh"
I have no idea what the root cause for this behavior could look like, but the workaround has been working reliably so far.
I used this article to ssh into my Raspberry pi3 VM.
Using this command ssh -p 2222 pi#localhost.
Originally, I had kept trying to use ssh pi#10.0.2.x -p 2222, but it didn't work and kept returning a "Connection timed out." My port number is 2222, but yours could be different depending what you set in your VirtualBox.
I am using a Windows 10 into a Debian Raspberry Pi VM (VirtualBox).
I've recently switched from MAMP to Vagrant (using VirtualBox). With MAMP any computer connected to my WiFi network would have access to a project by simply entering the host IP into it's web browser. With Vagrant however, I can only connect to the VM from the computer that's running it.
Changing config.vm.network "private_network" to config.vm.network "public_network" isn't working for me. In fact, even though the VM will launch and allow me to SSH, I can no longer access it from my browser.
Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "scotch/box"
config.vm.network "public_network"
config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
config.vm.synced_folder ".", "/var/www", :mount_options => ["dmode=777", "fmode=666"]
end
I've tried both setting and not setting the IP address, and use en1: Wi-Fi (AirPort) for the bridged network interface.
What am I doing wrong? Is this possible with Vagrant?
I've been able to fix this issue by assigning a static IP to my Vagrant VM that is in the same local range as my host machine.
For example:
My Host machine is: 192.168.1.123
This will work:
config.vm.network "public_network", ip: "192.168.1.201"
But this will NOT:
config.vm.network "public_network", ip: "192.168.0.201"
Remember to do a vagrant destroy as sometimes certain changes are not made until the box is recreated from scratch.
I'm running a private network Vagrant environment with a specified ip address and hostname. I can currently access the http version on port 80, but I'd also like to be able to access it on port 8080.
I added something like this:
config.vm.network :forwarded_port, guest: 80, host: 8080, auto_correct: true
But, when I visit my site on port 8080, I get an unavailable page error. Port 80 still works. how can I properly access my VM on a different port if the forwarded port doesn't work for private network?
Remember Vagrant Port forwarding ONLY works for the default NAT networking (using VirtualBox NAT), not private or public.
So if you want to access the service via private IP on port 8080, you either listen it on port 8080 or use iptables to forward packets.
e.g.
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
Ok, I am working with a rails application and it is my Vagrantfile and I access to my web service very well.
Vagrant.configure("2") do |config|
config.vm.hostname = "web"
config.vm.network :private_network, ip: "192.168.33.10"
config.vm.network :forwarded_port, host: 3000, guest: 3000
end
I did it following the instructions of Vagrant site.
Given the following Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "centos_64"
config.vm.host_name = 'web'
config.vm.network "private_network", ip: "192.168.50.4"
end
Why can't I ssh onto the guest from the host?
$ ssh vagrant#web -p 22
ssh: connect to host web port 22: Connection timed out
But using vagrant ssh works:
$ vagrant ssh
Last login: Tue Mar 4 21:29:24 2014 from 10.0.2.2
[vagrant#web ~]$
As expected, I can ping the IP Address from the guest. But I can't ping from the host.
I'm confused as to why it's happening since my setup does not look different from this configuration.
First, vagrant ssh uses the forwarded port and not the private network address. You can get the configuration with vagrant ssh-config.
Is the name "web" really resolving to the specified IP? Can you ping/connect using the IP instead of the name? If not, verify that you don't have other VMs or external networks with the same address. Also some VPN products mess up the routing.
I managed to change the ssh address with
config.ssh.host = '192.168.0.13'
config.ssh.port = '22'`
as mentioned in https://superuser.com/questions/920536/how-to-change-the-ssh-host-in-vagrantfile/921728#921728
Just because the guest host has a defined hostname does not mean that the host machine will resolve its ip.
You should be able to ssh into the machine by doing:
ssh vagrant#192.168.50.4 <==== vagrant is the default password,
but you can avoid typing it alway by doing:
ssh-copy-id vagrant#192.168.50.4
here is my configuration and it's working for me
config.vm.box = "ubuntu/xenial64"
config.vm.network "private_network", ip: "192.168.88.88"
config.ssh.host = "192.168.88.88"
I set up vagrant to run a vm on a host os. What I would like to do is be able to ssh from other machines directly into the vagrant vm (ie, I shouldn't ssh into the host and then vagrant ssh, etc. into the vagrant vm).
Currently, I can ssh not using vagrant ssh from the host os using ssh vagrant#127.0.0.1 -p 2222. However, if I run the same command (replacing 127.0.0.1 with the host's ip address), I get "ssh connect to host XXXXX port 2222: Connection refused."
I tried adding my own port forwarding rule to vagrant:
config.vm.network :forwarded_port, guest: 22, host: 2222
But that doesn't allow ssh connection from either the host machine or any other machine in the network. Additionally, I spent a while with config.ssh in the vagrant docs. I think that most of those parameters though specify what port the vagrant vm is running ssh on.
I really don't think this should be that difficult. Does anyone know what I might be doing wrong, or what I should do differently to ssh into a vagrant vm from a remote server?
If you don't want to change network to public you can override default port forwarding for ssh by this:
config.vm.network :forwarded_port, guest: 22, host: 2222, host_ip: "0.0.0.0", id: "ssh", auto_correct: true
This will forward guest 22 port to 2222 on your host machine and will be available from any ip, so you can access it outside your local machine.
Since v1.2.3 Vagrant port forwarding by default binds with 127.0.0.1 so only local connections are allowed.
You got "Connection refused" because the port forwarding was NOT binding to your network interfaces (e.g. eth0, wlan0). The port 2222 on your host is NOT even open to hosts in the same network (loopback interfaces not accessible to other hosts).
If you want to SSH directly to the Vagrant VM from a remote host (in the same LAN), the best and easiest way is to use Public Network (VirtualBox's Bridged networking mode).
Add the following to your Vagrantfile and do a vagrant reload.
It should bridge through one of the public network interfaces, you should be able to get the IP address after VM is up, vagrant ssh into it and run ifconfig -a or ip addr to get the IP address to ssh to from remote hosts.
Sample Vagrantfile
<!-- language: lang-rb -->
config.vm.network :public_network # 2nd interface bridged mode
or more advanced, you can set default network interface for public network
<!-- language: lang-rb -->
config.vm.network "public_network", :bridge => 'en1: Wi-Fi (AirPort)'
See more => Public Network
You can also add another rule to Vagrantfile like the following:
config.vm.network :forwarded_port, guest: 1234, host: 22
Connect to Vagrant with the default port (2222) and edit /etc/ssh/sshd_config, then add below Port 22 the port previously configured as 'guest', resulting:
...
Port 22 #Uncomment this line if it's commented
Port 1234
....
Finally, restart the ssh daemon or do vagrant reload (if you edited Vagrantfile while the VM was running you have to reload it) and now you can connect to Vagrant using 'host' port (22 in my case) from outside the host computer.
You can't remove the default port, because Vagrant would hang when starting up.
Use vagrant share --ssh
Vagrant now has a service for registering a Vagrant VM
for remote SSH access automatically.
See here: https://www.vagrantup.com/docs/share/ssh.html
You call vagrant share --ssh.
This generates an SSH key (encrypted and password-protected),
uploads it to a Hashicorp server,
and returns a silly global box name (e.g. "rambunctious-deer-3496").
Then everybody who
has a Hashicorp Atlas account
knows the box name,
knows the password for the key, and
has Vagrant installed(!)
can perform remote SSH to the box via
vagrant connect --ssh BOXNAME.
Vagrant takes care of all the admin stuff behind the scenes (here are some details).
Works as advertised.
I guess this will even work if the Vagrant host (not merely the VM) is behind a NAT.
Limitations:
vagrant share sessions expire (currently after 8 hours)
expect some latency, because all traffic is (presumably)
routed through the Altas server
I have seen my remote connections close (for no obvious reason)
after I had not used them for maybe 15 minutes.