How to create an IP alias on Windows
Asked Answered
D

7

17

I need to create an alias for my network interface such that it can be accessed locally with either 127.0.0.1 or 33.33.33.33.

In *nix I would do this:

sudo ifconfig en1 inet 33.33.33.33/32 alias

It appears that the netsh tool may be able to do the same thing. If so, how? I've seen some examples that seem close, but I don't understand the options provided.

The motivation for this is to run two instances of JBossAS on the same machine without port offsets. Thus, they would need to use the same port numbers without conflicting.

Devote answered 20/1, 2012 at 16:46 Comment(0)
Q
18

You'd be correct. Also, you can add multiple addresses without touching the command line using the advanced interface properties screen.

netsh interface ip add address "Local Area Connection" 33.33.33.33 255.255.255.255

Windows Advanced TCP/IP Settings

Quest answered 20/1, 2012 at 16:50 Comment(2)
Is there a way under Windows 7 to add an alias to an interfacce card while still using DHCP? Using your two suggested solutions requires to deactivate DHCP.Volding
The Command netsh interface ip add address "Local Area Connection" 33.33.33.33 255.255.255.255 worked for me by changing the name to Loopback as follows - netsh interface ip add address "Loopback" 33.33.33.33 255.255.255.255, without changing any default windows confs. Just run the command with admin rights.Cosignatory
H
10

It may depend on which version of Windows you have, but here are some steps from my Windows 7 machine. You want to get to your "local adapter settings". There are probably 1000 ways to do this, but here is one.

  • Go to Control Panel -> Network and sharing center
  • Click "change adapter settings" on the left
  • Right click on your local network connection and go to properties
  • Select your TCP/IP v4 protocol and click properties
  • TCP must be set to static addressing, so set it to "Use the following address" and set up your default IP config for the network. Then click advanced.
  • Under IP Address in Advanced TCP/IP settings, click Add
  • Add extra IP addresses as needed

enter image description here

Another approach is to add the Microsoft Loopback adapter as a network device. This lets you set up a virtual network adapter on your machine. This can be useful for network testing from your own machine. See http://social.technet.microsoft.com/Forums/en-US/w7itpronetworking/thread/259c7ef2-3770-4212-8fca-c58936979851/ for more info.

Hydrophobic answered 20/1, 2012 at 16:56 Comment(3)
Can you do this with DHCP enabled?Vaporimeter
No. I believe this requires all static IP addresses, but it has been a few years since I've done this, so things may have changed.Hydrophobic
See my answer for the correct way to add an alias, for a DHCP or static address.Iasis
A
9

There is a comment about how to add alias while still using DHCP for the main one. I have similar problem.

The solution is:

  1. Add Microsoft Loopback Adapter as a network device (as suggested by one of the comment).
  2. Specify IP address for the new network interface.

(1) Add Microsoft Loopback Adapter as a network device (taken from here):

  1. Click the Start menu.
  2. Search for “cmd".
  3. Right-click on “cmd” and select “Run as Administrator”
  4. Enter “hdwwiz.exe”

From that point on it's the same approach as under Vista, i.e.:

  1. In the "Welcome to the Add Hardware Wizard", click Next.
  2. Select "Install the hardware that I manually select from a list (Advanced)" and click Next.
  3. Scroll down and select "Network adapters" and click Next.
  4. Select under Manufacturer "Microsoft" and then under Network Adapter "Microsoft Loopback Adapter" and click Next.

(2) To see the newly added network interface, and specify an IP address:

  1. Go to Control Panel -> Network and Sharing Center
  2. Click on "Change adapter setttings" on the left side
  3. Find the entry with Device Name "Microsoft Loopback Adapter"
  4. Right click on it, and choose Property
  5. Choose "Internet Protocol Version 4 (TCP/IPv4) and hit Properties button.
  6. Select "Use the following IP address:", and enter IP address, Subnet mask, and Default gateway as needed.
Apetalous answered 6/2, 2014 at 8:51 Comment(3)
Is it possible to install the loopback adapter from the command line?Slovenia
this is a life saver!Nikolia
This is not what the question was. Adding another adaptor is not adding an alias, although it may server a similar purpose. The correct solutions is to use the netsh command, as the original question stated. netsh interface ip add address "Local Area Connection" 192.168.2.2 255.255.255.0 (This has to be done as an administrator.) Use "ipconfig" to check what the network interface is called. In my case it is "Local Area Connection".Iasis
I
2

As pointed out above, when using a static IP address, the GUI provides a suitable way to achieve this. However...

You cannot add an alias in the GUI when using DHCP to get an address. The "netsh" command, however, is helpful to achieve this:

EDIT: The above statement used to be true, but at some point Microsoft quietly fixed the issue by adding a new setting. All credit to this answer.

Setting the property dhcpstaticipcoexistence to enabled solves the issue.

e.g. (Using a cmd or powershell as a user with local admin rights)

netsh interface ipv4 set interface interface="Interface Name" dhcpstaticipcoexistence=enabled

netsh interface ip add address "Interface Name" 192.168.2.2 255.255.255.0

This has to be done as an administrator.

Use "ipconfig" to check what the network interface is called. In my case it is "Local Area Connection".

Iasis answered 8/5, 2017 at 13:14 Comment(2)
does settings an alias IP like this survive a server reboot? Do you know?Teratoid
@andrews, no it doesn't. Check my linked answer that also provides a script to re-apply this after a reboot.Iasis
I
2

go to C:\Windows\System32\drivers\etc\hosts on windows and add a new entry as below

eg: 106.200.247.101 abc.com

Then you will be able to refer to using given hostname

Ineradicable answered 21/4, 2018 at 11:22 Comment(1)
This is no applicable here at all. The OP wanted to know how to add an alias to a network interface with an existing ip address. What you described here is how to add a static name resolution for host abc.com. This answer should be deleled.Iasis
S
0

for anyone coming, as i have, from google:
another way to accomplish this is to set a route and set the destination to understand the ip, an example: recently at our company our service was stopped for our T1, some old software that no one had the code for hard coded the external address of our server and we needed to make it go to an internal address of 192.168.2.100 so i set this route on the client machines: route -p add 208.44.37.176 MASK 255.255.255.248 192.168.2.100 METRIC 1 and on the server i set it's interface to listen on 208.44.37.182

so when the client tries to connect to 208.44.37.182 it would check the routing table, it would do a bitwise AND on the netmask for anything in the routing table to match to the routing address (the 208.44.176) and then route it to the gateway (192.168.2.100) failing this is routes to the default gateway.

so in this case it routes it through to 192.168.2.100 who sees it's addressed to him and replies in turn. if he wasn't listening on 208.44.37.182 then he would ignore it (or, if he is configured to do so, pass it along til someone takes it or drops it)

(for those curious about why that netmask:
182 is 10110110
248 is 11111000
176 is 10110000
for a bitwise AND if one of the two bits is 0, the result is 0, if they are both 1 it's 1. so you see taking 182 and bitwise ANDing it to 248 gives 176)

those are the things i learned when working on this, hope it helps someone else.

Samualsamuel answered 15/5, 2013 at 19:57 Comment(0)
D
-3

I found the answer.

If you obtain your IP address from DCHP, you don't need to configure IP address (making sense knowing that it is the service of a DCHP) so all you need to do is to determine the gateway.

Deckhouse answered 1/9, 2012 at 15:21 Comment(1)
Not an answer to the OP's question, which is "How do I add a second IP Address (an 'alias' address)?" Greg's and Michael's answers are correct from the Windows GUI standpoint. Using the netsh command from a CMD window is also possible.Rosabella

© 2022 - 2024 — McMap. All rights reserved.