Use netsh commands in Windows to modify IP address gateways and DNS

Source: Internet
Author: User
Use netsh commands in Windows to modify IP address gateways and DNS

The following are two usage instructions on netsh. Copy them to the text document and change the suffix. txt to. CMD and double-click it to execute:

The first is to use the netsh command to modify the IP address, subnet mask, default gateway, and DNS of the computer.

@ echo off
netsh interface IP Set address" Local Connection " static 192.168.0.1 00000000255.0 192.168.0.254 1
netsh interface IP Set DNS" Local Connection " static
202.194.40.1
netsh interface IP add DNS" Local Connection " 202.194.40.2 2

Note: The content displayed in the blue font must be modified. in the second, third, and fourth rows, " Local Connection " generally, no modification is required, this depends on your computer right-click "Network neighbors" and select "properties". The connection name in the "Network Connection" window appears. Generally, there is only one "Local Connection ".

In the second row192.168.0.1 255.255.255.0 192.168.0.254The three addresses are IP addresses, subnet masks, and default gateways. replace them with the addresses you want to modify.

In the third row 202.194.40.1For the DNS address, replace it with the DNS address you want to modify.

202.194.40.2 in the fourth line is the secondary DNS address, that is, the second one. Replace it with the second DNS address you want to modify. If not, delete the fourth line directly.

The second is to use the netsh command to modify the IP address and subnet mask of the computer. The default gateway and DNS are dynamically obtained.

@ Echo off
Netsh interface IP Set address"Local Connection"DHCP
Netsh interface IP Set DNS"Local Connection"DHCP

Note: This is relatively simple. You only need to modify the blue font based on your computer"Local Connection". Generally, this parameter does not need to be modified.

 

NextArticleIt is a detailed usage of the netsh command. After reading it, you will understand its other usage.

It can be used in Windows 2000NetshRun the following command to write a script: Change the computer's static Internet Protocol (IP) address to a Dynamic Host Configuration Protocol (DHCP) address or vice versa.NetshThe command does not need to restart the computer to complete this task. For a portable computer that is moved from one environment to another, when it must use a static IP address in one location, and the IP address allocated by DHCP must be used in another location, this function is particularly useful.
This method is applicable to Windows 2000, Windows XP, and Windows Server 2003.

To switch the static address of the specified adapter to a DHCP address, type the following command:

Netsh interface IP Set address "Local Connection" DHCP

Note:: Enter this command to change the interface "Local Connection" to DHCP.

To display all the adapters on the computer and their current IP addresses to determine the correct adapter name, type the following command:

Netsh interface IP show config

To change to a static address, type the following command:

Netsh interface IP Set address "Local Connection" static IP mask gateway hops For example: Netsh interface IP Set address "Local Connection" static192.168.0.10 255.255.255.0 192.168.0.1 1 Change DNS to 192.168.0.1: Netsh interface IP Set DNS "Local Connection" static 192.168.0.1 Delete Gateway: Netsh interface IP Delete address "Local Connection" Gateway = all Delete DNS:
Netsh interface IP Delete DNS "Local Connection" All

If the system prompts "invalid interface specified" when executing the configuration, enable the system's "Remote Registry Service" service.

Control Panel --> Administrative Tools --> services --> enable remote registry service
Modify IP address, subnet mask, gateway, DNS . The "network connection name" is the connection name displayed in the Network Properties on your machine. Change the following name to your own connection name.
Netsh interface IP Set address "network connection name" static 192.168.0.88 255.255.255.0 192.168.0.1 1
Netsh interface IP Set DNS "network connection name" static 202.216.224.66
Netsh interface IP add DNS "network connection name" 202.216.224.67 If you want to change to dynamic IP Address Allocation and automatic DNS access, use the following command.
Netsh interface IP Set address "network connection name" DHCP
Netsh interface IP Set DNS "network connection name" DHCP Because I often switch between different networks in my location, such as lan, system Intranet, and Internet (Internet alone, I usually have three ADSL
Network switching ). I used to set multiple IP addresses of different CIDR blocks on the local machine, and then switch the route so that different CIDR blocks can be accessed at the same time through different gateways.
The purpose of multiple networks. However, I often find some problems, so I decided to use the most primitive method to solve the problem, that is, to use only the IP address of the CIDR block when using a certain CIDR block.
You need to constantly change the IP address. Of course, this change in the "Network Connection" attribute of Windows is very troublesome, but fortunately, the "netsh" command of Windows provides
You can use this function to change the IP address under the line to make it a bat batch file, which is much more convenient. The following describes the specific methods and commands: First, enter the command prompt line: "Win + R"> "cmd", and press Enter.
Enter netsh and press Enter. The following message is displayed: netsh>
Enter the interface and press enter to display the netsh interface>
Enter the IP address and press Enter. The netsh interface IP address is displayed.> Go to the ipsettings. We need to use the following command:
Set address-add an IP address to the specified interface. Let's take a look at the windows help for this order:
--------------------------------------------------------------------------
Usage: Set Address [name =] <string>
[[Source =] DHCP |
[Source =] Static [ADDR =] IP Address [mask =] IP subnet mask]
[[Gateway =] <IP address> | none [gwmetric =] integer] Parameters: tag value
name-Interface Name.
source-one of the following values:
DHCP: for a specified interface, configure an IP address using DHCP
.
static: Set to use the local static configuration to set the IP address
. gateway-one of the following values:
: specify the default IP address
gateway.
none: No Default Gateway is set.
gwmetric-the number of hops of the default gateway. If the gateway is set to 'None',
this field is not set.
the following options are set only when 'source' is 'static ': ADDR-IP address of the specified interface.
Mask-specifies the subnet mask of the IP address. Note: It is used to change the IP Address Configuration Mode from DHCP to static or from static.
Change the mode to DHCP. Use static IP addresses to add IP addresses or
Default Gateway.
Example: Set address name = "Local Area Connection" Source = DHCP
Set address local static 10.0.0.9 255.0.0.0 10.0.0.1 1
---------------------------------------------------------------------------- After understanding the usage, we can set it:
After netsh interface IP>, enter
Set address "Local Connection" static 192.168.4.23 255.255.255.192 192.168.4.1 1
Press enter. "OK" will be displayed later. The IPaddress is set.
Here, "Local Connection" refers to the name of the connection seen in Windows "Network Properties". "static" indicates that the static IP address is to be specified instead of being assigned by DHCP. The next three are IP addresses, subnet Mask, Gateway. The last "1" refers to the number of hops of the default gateway, generally "1 ". Now, the IP address setting in the command line is complete.
At the same time, you can also change the DNS settings under the command line, using the set DNS command, you can enter set dns /? View the usage method. Finally, we willSave as a bat file(Setip192168423.bat ):

@ Echo off
Rem ETH // ETH is the name of the NIC, which can be queried in network connection, for example, "Local Connection"
Set Eth = "Local Connection"
Rem IP // ip address you want to change
Set IP = 192.168.4.23
Rem GW // GW is the gateway address
Set GW = 192.168.4.1
Rem netmasks // netmasks is the subnet mask
Set netmasks = strongswan 192

ECHO is changing the local IP address to: % IP %
Rem
If % GW % = none netsh interface IP Set address % ETH % static % IP % netmasks % GW %> NUL
If not % GW % = none netsh interface IP Set address % ETH % static % IP % netmasks % GW % 1> NUL
Echo .........................
Echo checks the current local IP Address:
Ipconfig
Echo .........................
Echo successfully changed the local IP address to % IP %!
Pause
Close

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.