Connect to Postgres running on the Windows host from WSL2 - windows

I am trying to connect to PostgreSQL DB installed on my windows machine from WSL2 however facing issues while connecting would appreciate if you can help me resolve this issue. In addition to that I have tried below options already.
Windows postgreSQL is running fine on port 5432
Have below entry in pg_hba.conf file
host all all 0.0.0.0/0 md5
Checked windows IP address for WSL2 from following command and then
Telnet , still no luck
sudo cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }'
172.19.240.1
telnet 172.19.240.1 5432
Defined inbound firewall rule for port 5432 from Windows Defender
Firewall with Advanced Security Still not working
Disabled firewall completely - same issue :(
OS : Windows 10 Home
Error Message
Trying 172.19.240.1...

Related

Windows Docker contnainer of Postgres unreachable on 192.168.56.1

I am running a Postgres Docker container by command docker run --name postgres -p 0.0.0.0:5432:5432 -e POSTGRES_PASSWORD=password -d postgres.
In my understanding, 0.0.0.0:5432 means all the IP addresses of the Windows host at port 5432. And, I can access the port by the command telnet 192.168.56.1 5432 with Windows CMD. With VirtualBox installed, 192.168.56.1 means the physical host.
However, I have an RHEL Linux virtual machine running on VirtualBox. On the VM, the command telnet 192.168.56.1 5432 does not work. But if I try another IP address of the Windows host, it works.
I need to use the virtual machine to test and access the Postgres database deployed on the physical host. I am wondering whether I missed anything here.
Any suggestions or hints will be highly appreciated.

How to Use sshuttle on Windows WSL2

We have a Jenkins server which is accessible only from within the VPC on the cloud. On Mac and Linux I use sshuttle to make a ssh connection to the bastion instance (to act a proxy) and open the Jenkins console in the browser. Everything works fine.
Now I'm on Windows and trying to do the same on WSL2. If I'm not mistaken previously, sshuttle didn't work on WSL1 (failed with some error message), but I managed to run it on WSL2 without any issue. The ssh connection is established and I can access my Jenkins (using curl).
Then I tried to access my Jenkins on Windows via WSL2:
1. I found the IP address of WSL2 and the port the ssh tunnle:
# lsof -i -n | grep ssh
sshuttle 1234 rad 5u IPv4 39270 0t0 TCP *:socks (LISTEN)
ssh 5678 rad 3u IPv4 40252 0t0 TCP 172.25.236.84:57578->bastion:ssh (ESTABLISHED)
2. I configured network proxy setting of Firefox (v77) to use my ssh tunnle:
Manual proxy configuration
SOCK host: 172.25.236.84
Port: 1080
SOCKS V5 (tested with V4 as well)
But loading the page fails with "The connection was reset" error on Firefox. I tested via Powershell that the SOCKS port is open and responding (using Test-NetConnection).
1. Any idea what the problem is? How to make it work?
2. If it's not gonna work, is there any other solution (e.g. Docker, etc)?
Thanks.
I'm not sure, but my guess is that sshuttle doesn't actually act as a SOCKS proxy and that's why the connection gets reset.
I managed to access my Jenkins on Windows machine using ssh SOCKS proxy: ssh -D 0.0.0.0:1080 rad#bastion and configured Firefox to use the SOCKS proxy.
Interestingly, for this you don't even need WSL. It seems Windows 10 has OpenSSH and you can use it. Just open CMD and type ssh -D 1080 rad#bastion and setup Firefox to use localhost as the proxy.
If there's any better solution or any comment/concern (apart from DNS over SOCKS) with this approach, please share.
Thanks.
As alternative on WSL(2) you can run a regular SSH tunnel.
Eg:
ssh -N -L 127.0.0.1:5432:some_domain_to_forward:5432 user#jumpbox_ip
and then just connect to 127.0.0.1:5432

Connecting PostgreSQL installed in docker inside Hyper-V Ubuntu from Windows 10 PgAdmin

I need help in connecting PostgreSQL which is installed in Docker inside HyperV ubuntu 18.4 from Windows 10 PgAdmin. So far I tried the following
Step 1: Install Postgres in Docker (Ubuntu running on Hyper-V)
sudo docker run -p 5432:5432 --name pg_test -e POSTGRES_PASSWORD=admin -d postgres
Step 2: Create a database
docker exec -it pg_test bash
psql -U postgres
create database mytestdb
Step 3: Get the ip address
sudo docker inspect pg_test | grep IPAddress
//returned with 172.17.0.2
Step 4: pg_hba.conf
host all all 0.0.0.0/0 md5
Step 5: When I try to connect from Windows PgAdmin 4, I get this below error -
Note: I have also tried using UBUNTU VM IP address, but no luck
Your's is a case where you are trying to connect to postgres from another subnet, i.e windows subnet to hyper visor subnet if you are not using bridged protocol.
So case 1:
If this is on NAT\HOST and not on bridge then you need to make sure you are able to ping the ubuntu server from windows server.
next is make sure that port is open from ubuntu's end. How do you check that, do a telnet on the port number from windows cmd prompt.
telnet 192.168.0.10 5432
if you are bridged and you can ping ping the server as well, checked that port is opened which is telnet works. You need to make sure that in the postgres.conf file
"listen address" is to "*". which is all.
Again from OS level in ubuntu run the command systemctl stop firewalld to stop firewall and then try to connect. IF this works then you need to open the port in the firewall using this command:
firewall-cmd --permanent --add-port 5432/tcp
I can see from you docker image that 5432 is already opened. This is more of port mapping and firewalld stuff.
You may want to check that pg_hba.conf is not restricted to local. It should not be the case for docker image but you never know.
See: https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html
Also, there is a typo: POSTGRES_PASSWOR=admin is missing D, it should be POSTGRES_PASSWORD=admin.
You don't need container IP. Since you have mapped container port to host machine (Ubuntu) anyone outsider just needs the Ubuntu machine IP, and on Ubuntu itself you can use localhost.

Access to localhost in virtualbox

I have a server on port 8085 (my pc - I use Ubuntu 16.04). There are also Virtualbox with installed Windows 7. I have an internet connection in Windows (it is possible to open google.com etc) but there is no connection to localhost. For localhost:8085 in Ubuntu everything works perfectly but for the same adress in virtual-machine doesn't work.
I tried to modify Settings->Network and put there bridged but there is the same problem.
How is it possible to access it?
Try 127.0.0.1 instead of localhost
or get ip address with terminal command ifconfig and use the address in the url

cannot connect to host 127.0.0.1 Virtualbox

So I'm using a Macbook Pro with virtualbox and trying to SSH to CentOS virtual machine.
I've started the SSH service on centOS and I've gone into the network settings of virtualbox and made sure that the adaptor was set to NAT and that port forwarding was set to port 22 for the guest and host.
When I run the following on my mac: ssh 127.0.0.1
It says: ssh: connect to host 127.0.0.1 port 22: Connection refused
Any ideas why?
Fixed the issue by enabling ssh on mac. I didn't realize it was off as default! If you go into the sharing options in system preferences you can tick a box which enables ssh. To connect you must use: $ ssh username#192.168.1.111 (or whatever the ip is of the machine).

Resources