Get real IP address in local Rails development environment

Viewed 46432

I have Rails 2.3.8, Ruby 1.8.7, Mongrel Web Server and MySQL database.

I am in the development mode and I need to find the real IP address

When I use request.remote_ip I am getting the IP as 127.0.0.1

I know I am getting 127.0.0.1 because I am developing on the local machine.. but is there a way to get the real ip-address even if I am on the local machine?

I am using these mentioned below in my controller and all I get is 127.0.0.1 with all of them in the view.

request.remote_ip
request.env["HTTP_X_FORWARDED_FOR"]
request.remote_addr
request.env['REMOTE_ADDR']
9 Answers

I had to do this:

require 'socket'
Socket.ip_address_list.detect(&:ipv4_private?)&.ip_address

If you access the development machine with your real IP you should get your real IP, so don't use localhost, but your use your real IP. Not all routers are will allow LAN access to a machine to an external IP address that is actually on that LAN, but most will.

You could use request.ip to get your real machine IP, although request.remote_ip may be a better when you want IPs of other machines.

Related