How to define localhost IP as 10.0.2.2?

On my Ubuntu PC, I need to use 10.0.2.2 instead of 127.0.0.1 to be able to let Android emulator to connect to web server running on localhost. So wondering how can I define the new IP?

I edited /etc/hosts and added

10.0.2.2 localhost
10.0.2.2 subdomain.localhost

Instead and restarted the hosts using /etc/init.d/hostname.sh. But ifconfig still does not show the new ip. How can I fix this?

1

1 Answer

You don't need to change /etc/hosts or machine hostname. These are just for name resolution. You need to bring up a loopback interface with this ip. So we will bring a copy of loopback (lo:1) with this ip:

$ sudo ifconfig lo:1 10.0.2.2 up

Then you can use ifconfig to see if its up. It will appear like:

lo:1 Link encap:Local Loopback inet addr:10.0.2.2 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1

You can use ssh to this ip.

To make this permanent you can add it to /etc/rc.local .

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like