如何在 Windows 上创建 IP 别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8944860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:07:54  来源:igfitidea点击:

How to create an IP alias on Windows

windowsipnetsh

提问by codefinger

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.

我需要为我的网络接口创建一个别名,以便可以使用 127.0.0.1 或 33.33.33.33 在本地访问它。

In *nix I would do this:

在 *nix 中,我会这样做:

sudo ifconfig en1 inet 33.33.33.33/32 alias

It appears that the netshtool 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.

netsh工具似乎也能做同样的事情。如果是这样,如何?我看过一些看起来很接近的例子,但我不明白提供的选项。

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.

这样做的动机是在没有端口偏移的情况下在同一台机器上运行两个 JBossAS 实例。因此,它们需要使用相同的端口号而不会发生冲突。

回答by Greg Buehler

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

Windows 高级 TCP/IP 设置

回答by Michael Levy

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.

这可能取决于您使用的 Windows 版本,但这里有一些来自我的 Windows 7 机器的步骤。您想进入“本地适配器设置”。可能有 1000 种方法可以做到这一点,但这里只有一种。

  • 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
  • 进入控制面板 -> 网络和共享中心
  • 点击左侧的“更改适配器设置”
  • 右键单击您的本地网络连接并转到属性
  • 选择您的 TCP/IP v4 协议并单击属性
  • TCP 必须设置为静态寻址,因此将其设置为“使用以下地址”并为网络设置默认 IP 配置。然后点击高级。
  • 在高级 TCP/IP 设置中的 IP 地址下,单击添加
  • 根据需要添加额外的 IP 地址

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.

另一种方法是将 Microsoft 环回适配器添加为网络设备。这使您可以在计算机上设置虚拟网络适配器。这对于从您自己的机器进行网络测试很有用。有关更多信息,请参阅http://social.technet.microsoft.com/Forums/en-US/w7itpronetworking/thread/259c7ef2-3770-4212-8fca-c58936979851/

回答by januarvs

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

有关于如何在仍使用 DHCP 作为主要别名的同时添加别名的评论。我有类似的问题。

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. 将 Microsoft Loopback Adapter 添加为网络设备(如其中一条评论所建议)。
  2. 指定新网络接口的 IP 地址。

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

(1) 将 Microsoft Loopback Adapter 添加为网络设备(取自此处):

  1. Click the Start menu.
  2. Search for “cmd".
  3. Right-click on “cmd” and select “Run as Administrator”
  4. Enter “hdwwiz.exe”
  1. 单击开始菜单。
  2. 搜索“cmd”。
  3. 右键单击“cmd”并选择“以管理员身份运行”
  4. 输入“hdwwiz.exe”

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

从那时起,它与 Vista 下的方法相同,即:

  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.
  1. 在“欢迎使用添加硬件向导”中,单击下一步。
  2. 选择“安装我从列表中手动选择的硬件(高级)”,然后单击“下一步”。
  3. 向下滚动并选择“网络适配器”,然后单击“下一步”。
  4. 在制造商“Microsoft”下选择,然后在网络适配器下选择“Microsoft Loopback Adapter”,然后单击下一步。

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

(2) 查看新添加的网络接口,并指定IP地址:

  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.
  1. 进入控制面板 -> 网络和共享中心
  2. 点击左侧的“更改适配器设置”
  3. 查找设备名称为“Microsoft Loopback Adapter”的条目
  4. 右键单击它,然后选择属性
  5. 选择“Internet 协议版本 4 (TCP/IPv4)”并点击“属性”按钮。
  6. 选择“使用以下IP地址:”,根据需要输入IP地址、子网掩码和默认网关。

回答by Lifeboy

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

使用 DHCP 获取地址时,您无法在 GUI 中添加别名。然而,“netsh”命令有助于实现这一点:

netsh interface ip add address "Local Area Connection" 192.168.2.2 255.255.255.0

netsh 接口 ip 添加地址“本地连接” 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".

使用“ipconfig”检查网络接口的名称。就我而言,它是“本地连接”。

回答by austin

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

对于像我一样来自谷歌的任何人:
另一种实现此目的的方法是设置路线并设置目的地以了解 ip,例如:最近在我们公司,我们的 T1 服务停止了,一些旧软件没有一个有硬编码我们服务器的外部地址的代码,我们需要让它转到内部地址 192.168.2.100 所以我在客户端机器上设置了这个路由: route -p add 208.44.37.176 MASK 255.255.255.248 192.168 .2.100 METRIC 1 和在服务器上我设置它的接口来监听 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.

因此,当客户端尝试连接到 208.44.37.182 时,它会检查路由表,它会在网络掩码上对路由表中的任何内容进行按位 AND 运算以匹配路由地址(208.44.176),然后将其路由到失败的网关 (192.168.2.100) 是到默认网关的路由。

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)

所以在这种情况下,它会将它路由到 192.168.2.100,后者看到它是发给他的,然后依次回复。如果他没有在 208.44.37.182 上收听,那么他会忽略它(或者,如果他被配置为这样做,则将其传递直到有人接受或丢弃它)

(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)

(对于那些好奇为什么该网络掩码的人:
182 是 10110110
248 是 11111000
176 是 10110000
按位,如果两位之一为 0,则结​​果为 0,如果它们都是 1,则为 1。所以你看到取 182 和按位与 248 相加得到 176)

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

这些是我在做这件事时学到的东西,希望它可以帮助别人。

回答by Pankaj Jaiswal

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

在 Windows 上转到 C:\Windows\System32\drivers\etc\hosts 并添加一个新条目,如下所示

eg: 106.200.247.101 abc.com

例如:106.200.247.101 abc.com

Then you will be able to refer to using given hostname

然后您将能够使用给定的主机名进行引用

回答by Jeka Santos

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.

如果您从 DCHP 获取 IP 地址,则不需要配置 IP 地址(知道它是 DCHP 的服务是有意义的),因此您需要做的就是确定网关。