繁体   English   中英

如何在Windows 10中设置静态IP地址?

[英]How to set a static IP address in windows 10?

在使用简单的脚本搜索代码以设置静态IP地址后,我在StackOverflow上找不到完整且易于实现的答案。 这使我想到了以下问题:

将Windows 10 IP地址设置为静态IP地址,然后再次设置为动态IP地址,将是“易于实施”的代码?

^注意:Easy旨在确保代码及其完整实现尽可能简单,而不是使用户感到挑战。

请注意,这是以下网址的实现: http : //www.midnightdba.com/Jen/2014/03/configure-static-or-dynamic-ip-and-dns-with-powershell/ 所有的积分都归MidnightDBA所有。 我希望它能使某人受益!

手动将IP地址设置为静态

  1. Start>control panel>Network and Internet>Network and Sharing Center>Change adapter settings>rmb on the ethernet/wifi/connection that is in use>properties>Select: Internet Protocol Version 4(TCP/IPv4)>Properties>

  2. 这将导致屏幕类似于附件图像。 您可以在那里手动填写数字。 这些数字在您自己的情况下可能会有所不同,您需要做注释3中建议的工作,以便自己确定这些数字。 在Windows 10中手动设置IP地址的示例

设置静态IP(半自动):

这意味着您可以通过打开一个文件(双击您创建的脚本)将IP地址设置为静态,并通过运行您创建的另一个脚本将IP地址设置为动态IP地址。 指示步骤如下:

  1. start>type Powershell>rmb>Open powershell as administrator
  2. (如果您第一次不能立即运行脚本,请仅执行此步骤。)键入: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser并按Enter,以设置安全策略,以便您可以运行Powershell脚本。
  3. 在内容为c:/example_folder static_ip.ps1中创建一个名称为static_ip.ps1.ps1文件,例如:

     $wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'"; $wmi.EnableStatic("your_static_ip_adress", "your_subnetmask"); $wmi.SetGateways("your_routers_ip_adress", 1); $wmi.SetDNSServerSearchOrder("your_dns"); 

    或仅需双击static_ip.ps1脚本即可设置静态IP :(注意示例值已填写)

     # 18-07-20 Todo: add wifi network detection that automatically triggers setting a static IP and back dynamic IP. # First ensure the script is automatically ran as administrator, else it appearently does not have the privileges to change the local IP adress: $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) $testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) if ($testadmin -eq $false) { Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition)) exit $LASTEXITCODE } # Next set it static: $wmi.EnableStatic("192.21.89.5", "255.255.254.0"); $wmi.SetGateways("192.21.89.1", 1); $wmi.SetDNSServerSearchOrder("192.21.89.1"); # Now close the window this has just created. # This leaves other Powershell windows open if they were already open before you ran this script. # Also, It yields an error with a $ sign at the start of the line. # Source: https://stackoverflow.com/questions/14874619/powershell-exit-doesnt-really-exit Stop-Process -Id $PID 
  4. 然后在powershell中输入:

     cd c:/example_folder .\\static_ip.ps1 

    注意,如果static_ip.ps1文件的路径包含空格,请将change directory命令change directory为:

     cd "c:/example_folder" 

要使IP重新动态(半自动):

  1. 创建一个名为dynamic_ip.ps1的文本文件,该文件位于目录c:/examplefolder ,其内容为:

     $wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'"; $wmi.EnableDHCP(); $wmi.SetDNSServerSearchOrder(); 

    或只需双击dynamic_ip.ps1脚本即可对其进行更改:

     #18-07-20 Todo: add wifi network detection that automatically triggers setting a static IP and back dynamic IP. # First ensure the script is automatically ran as administrator, else it appearently does not have the privileges to change the local IP adress: $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) $testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) if ($testadmin -eq $false) { Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition)) exit $LASTEXITCODE } # Next set it dynamic: $wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'"; $wmi.EnableDHCP(); $wmi.SetDNSServerSearchOrder(); # Now close the window this has just created. # This leaves other Powershell windows open if they were already open before you ran this script. # Also, It yields an error with a $ sign at the start of the line. # Source: https://stackoverflow.com/questions/14874619/powershell-exit-doesnt-really-exit Stop-Process -Id $PID 
  2. 在Powershell中:

     cd c:/example_folder .\\dynamic_ip.ps1 

成功在Powershell中首次尝试后,您可以通过使用powershell打开脚本来打开/运行脚本,从而简单地设置静态IP地址(在资源管理器中,双击文件,或right mouse button (rmb)>open with powershell )。 但这要起作用,脚本路径不能包含任何空格!

补充笔记:

  1. 如果您再次离开家庭网络,请不要忘记重新设置IP地址的动态性,否则当您尝试在其他wifi /以太网网络中访问Internet时可能会遇到问题!

  2. your_static_ip_adress :您可以通过以下方式读取动态IP地址和路由器ip地址: start>type cmd>open command prompt>type: ipconfig ,或键入: ipconfig -all 。*此外,上述说明中所述的规则通常适用。

    your_routers_ip_adress请参见“ your_static_ip_adress”,通常以.1结尾

    your_subnetmask请参阅“ your_static_ip_adress”

    your_dns ,可以是路由器的ip地址,例如googles DNS 8.8.8.8

  3. 确定静态IP地址的规则:来源: https : //www.howtogeek.com/184310/ask-htg-should-i-be-setting-static-ip-addresses-on-my-router/

    3.1不要分配以.0或.255结尾的地址,因为这些地址通常是为网络协议保留的。

    3.2不要为IP池的最开始分配地址,例如10.0.0.1,因为开始地址始终是为路由器保留的。 即使出于安全考虑更改了路由器的IP地址,我们仍然建议不要分配计算机。

    3.3不要在可用的专用IP地址池之外分配地址。 这意味着,如果路由器的池为10.0.0.0到10.255.255.255,则您分配的每个IP(请牢记前两个规则)都应在该范围内。

  4. 这是手动填充此帖子第一张图的数据(以设置静态IP)的(半)自动等效方法。

  5. (Wifi连接问题进行故障排除)如果:

    • 您有2个不同的wifi网络( AB ),您都可以在同一位置连接
    • 其中只有B拥有正确的"your_routers_ip_adress"/local gateway-adress
    • 并且您在连接到错误的wifi( A )时意外地将local IP设置为(错误的) static IP
    • 然后先断开错误的wifi( A ),然后再将local IP地址设置为动态,
    • 并且(因此)会遇到wifi问题:(使scanning network requirements )。

      然后:

    • local IP地址设置为dynamic

    • 重新连接到错误的wifi网络( A )。
    • 将其设置回static ,然后再次设置为dynamic
    • 断开wifi( A )的连接。
    • 现在,您应该能够再次正确连接到两个wifi网络。

      要么:

    • local IP地址设置为static

    • 重新连接到错误的wifi网络( A )。
    • 将其设置回static ,然后再次设置为dynamic
    • 断开wifi( A )的连接。
    • 现在,您应该能够再次正确连接到两个wifi网络。

GUI和PowerShell提供了很好的信息。

当您通过PowerShell手动分配IP时,DNS服务器IP很重要。 另外,它可以通过命令提示符完成,这在远程管理PC时很有用。 以下帖子具有相关信息。 也许您可以考虑在帖子中添加这些步骤。

https://tinylaptop.net/how-to-configure-setup-static-ip-on-windows-10-laptop/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM