netsh interface (1/5)
netsh interface is the built-in Windows tool for managing adapters, static IPs, DNS, routes and the ARP cache from the command line. Part 1 covers the top-level context, show/set interface, the tunnel contexts (6to4, clat, fl48/fl68, httpstunnel) and the ipv4 add/delete/set address commands.
32.1 What it does and where it runs
- Built into Windows; runs in both CMD and PowerShell. Linux uses ip, macOS uses networksetup.
- A normal user can run show and help; set, add and delete need Run as administrator.
- Short form int works (netsh int ipv4 ...), and ? after any command prints its help.
- dump prints the whole configuration as a script you can save as a backup.
- Step 1
1.ipv4 set address static 192.168.52.10 255.255.255.0 192.168.52.1
- Step 2
2.ipv4 add dnsservers "Ethernet" 8.8.8.8
- Step 3
3.PC resolves a website name
- Step 4
4.DNS query goes to the gateway 192.168.52.1
- Step 5
5.Gateway forwards it to the DNS server 8.8.8.8, reply comes back
32.2 Top-level context
netsh interface ?$ netsh interface ?6to4 - Changes to the `netsh interface 6to4' context.clat - Changes to the `netsh interface clat' context.dump - Displays a configuration script.fl48 - Changes to the `netsh interface fl48' context.httpstunnel - Changes to the `netsh interface httpstunnel' context.ipv4 - Changes to the `netsh interface ipv4' context.set - Sets configuration information.show - Displays information.The following sub-contexts are available: 6to4 clat fl48 fl68 httpstunnel ipv4 ipv6 isatap portproxy tcp teredo udp32.3 show interface and set interface
- show interface lists admin state, connect state, type and name of every adapter; use that exact name in every later command.
- set interface enables, disables or renames an adapter (name + at least one option).
- admin=disabled takes the adapter down at once: never do it on the adapter your RDP session uses.
- connect= is only for non-LAN interfaces (VPN); newname= renames LAN adapters only.
netsh interface show interfacenetsh interface show interface name=Wi-Fi$ netsh interface show interfaceAdmin State State Type Interface Name-------------------------------------------------------------------------Enabled Disconnected Dedicated EthernetEnabled Connected Dedicated Ethernet 2Enabled Connected Dedicated Wi-Fi$ netsh interface show interface name=Wi-FiWi-Fi Type: Dedicated Administrative state: Enabled Connect state: Connectedset interface name="Ethernet" ... name="Ethernet" which adapter admin=enabled adapter on admin=disabled adapter off connect=connected non-LAN only (VPN) connect=disconnected non-LAN only newname="LAN 1" rename (LAN only)note: name + at least 1 option32.4 IPv6 transition contexts: 6to4, clat, fl48/fl68, httpstunnel
| Context | What it is | State on the lab PC |
|---|---|---|
| 6to4 | Wraps IPv6 inside IPv4; deprecated in current Windows | state, relay, routing all default |
| clat | Customer-side translator of 464XLAT for IPv6-only networks with old IPv4 apps | permit disabled, pref64 from DNS/RA enabled, autoenable enabled |
| fl48 / fl68 | Lists virtual interfaces (help, dump, show virtualinterfaces only) | every interface mode none |
| httpstunnel | IP-HTTPS: IPv6 inside HTTPS, used by DirectAccess | no interface configured |
netsh interface 6to4 show statenetsh interface 6to4 show relaynetsh interface 6to4 show routingnetsh interface clat show globalnetsh interface fl48 show virtualinterfacesnetsh interface httpstunnel dump$ netsh interface 6to4 show state6to4 Service State : defaultUndo on Service Stop : default$ netsh interface 6to4 show relayRelay Name : defaultUse Relay : defaultResolution Interval : default$ netsh interface 6to4 show routingRouting State : defaultUse SiteLocals : default$ netsh interface clat show global------------------------------------permit : disabledpref64fromdns : enabledpref64fromra : enabledautoenable : enabledstore : active$ netsh interface fl48 show virtualinterfacesIdx VirtIfId VirtIfIdx CompId Mode Ogn--- ---------- ---------- ---------- ---- --- 11 0 11 1 none default 18 0 18 1 none default 7 0 7 1 none default$ netsh interface httpstunnel dump# IPHTTPS Configurationpushd interface httpstunnelresetpopd# End of IPHTTPS configuration6to4 set interface routing=enabled|disabled6to4 set relay name, state, interval6to4 set routing routing, sitelocals6to4 set state state, undoonstop6to4 show interface|relay|routingclat set global permit, pref64*, autoclat set instance 1 interface on/offclat show global store=active|persistentclat show instance config of 1 interfacehttps add interface client|server + urlhttps set interface url, state, authmodehttps delete/reset remove interfaceshttps show interfaces, statistics32.5 ipv4 context, set address and add address
- ipv4 ? shows add, delete, dump, install, reset, set, show, uninstall. reset wipes all user IPv4 settings and needs a restart: last resort.
- set address static: fixed order IP, mask, gateway (gwmetric optional). It removes all old static IPs and gateways.
- set address source=dhcp sends the adapter back to DHCP.
- add address adds an extra IP next to the existing one, or a backup gateway with gwmetric=2. It disables DHCP on that adapter.
- skipassource=true: the IP is never used as the source of outgoing packets.
netsh interface ipv4 show config ^ "Loopback Pseudo-Interface 1" netsh interface ipv4 set address ^ name="Ethernet" static ^ 192.168.52.10 255.255.255.0 192.168.52.1 netsh interface ipv4 set address ^ name="Ethernet" source=dhcp netsh interface ipv4 add address ^ "Ethernet" 192.168.52.11/24 netsh interface ipv4 add address ^ "Ethernet" gateway=192.168.52.2 gwmetric=2 netsh interface ipv4 add address ^ "Ethernet" 192.168.52.12/24 ^ skipassource=true$ netsh interface ipv4 show config "Loopback Pseudo-Interface 1"Configuration for interface "Loopback Pseudo-Interface 1" DHCP enabled: No IP Address: 127.0.0.1 Subnet Prefix: 127.0.0.0/8 (mask 255.0.0.0) InterfaceMetric: 75$ netsh interface ipv4 set address ?Usage: set address [name=]<string> [[source=]dhcp|static] [[address=]<IPv4 address>[/<integer>] [[mask=]<IPv4 mask>] [[gateway=]<IPv4 address>|none [gwmetric=]<integer>] [[type=]unicast|anycast] [[subinterface=]<string>] [[store=]active|persistent]Remarks: Used to enable or disable DHCP for IP address configuration. Also removes any previous static IP addresses and default gateways,32.6 add dnsservers and add winsservers
add dnsservers "Ethernet" 8.8.8.8 name= adapter name or index address= DNS server IP index=2 position in list validate=yes check new server (default) validate=no skip the check add winsservers "Ethernet" 192.168.52.5 name= adapter name or index address= WINS server IP index=2 position in list note: replaces the list from DHCP32.7 add route
- Step 1
1.PC 192.168.52.10 sends traffic for 172.16.5.10 (branch server)
- Step 2
2.Without a route it goes to the default gateway
- Step 3
3.Gateway has no route for 172.16.0.0 -> packet dropped
- Step 4
4.Fix: ipv4 add route 172.16.0.0/16 "Ethernet" 192.168.52.254
- Step 5
5.Packet now goes PC -> branch router 192.168.52.254 -> branch server
- Order: prefix, interface, nexthop. nexthop is only needed when the network is not on-link.
- metric chooses between two routes for the same prefix.
- store=active disappears after reboot; persistent is the default. publish is for router advertisements, keep it no on a PC.
netsh interface ipv4 add route ^ 172.16.0.0/16 "Ethernet" 192.168.52.254 netsh interface ipv4 add route ^ 172.16.0.0/16 "Ethernet" ^ 192.168.52.254 metric=50 store=active32.8 add excludedportrange
- Reserves a block of ports so Windows never hands them out as dynamic ports.
- Needs protocol, startport and numberofports; useful when another service keeps stealing your app's port.
- In the list, * marks administrator-reserved blocks; the rest were reserved by the system.
netsh interface ipv4 show ^ excludedportrange protocol=tcp REM syntax (admin CMD):netsh interface ipv4 add excludedportrange ^ protocol=tcp startport=50000 ^ numberofports=20 store=persistent$ netsh interface ipv4 show excludedportrange protocol=tcpProtocol tcp Port Exclusion RangesStart Port End Port---------- -------- 50000 50059 * 51866 51965 56700 56799 59828 59927 60028 60127* - Administered port exclusions.32.9 Neighbors (ARP) and the delete commands
- Step 1
1.PC talks to the gateway; ARP reply arrives
- Step 2
2.Gateway MAC becomes a dynamic entry in the cache
- Step 3
3.add neighbors: static MAC for printer 192.168.52.20, never expires
- Step 4
4.Gateway MAC changes -> delete arpcache flushes the cache
- Step 5
5.Next packet makes ARP build a fresh entry
| Command | What it removes |
|---|---|
| delete address "Ethernet" 192.168.52.11 | A static IP; gateway=<ip> or gateway=all removes gateways |
| delete arpcache ["Ethernet" <ip>] | ARP cache of all adapters or one entry |
| delete destinationcache | Windows path cache (per-destination info like MTU) |
| delete dnsservers "Ethernet" all | The whole static DNS list |
| delete excludedportrange tcp 50000 20 | Port block; startport + numberofports must match exactly |
| delete neighbors | Same job as delete arpcache |
| delete route 172.16.0.0/16 "Ethernet" | A route (prefix + interface, nexthop optional) |
| delete winsservers "Ethernet" all | WINS servers |
32.10 Common mistakes
- Thinking store=active and persistent are the same: active lasts only until reboot, persistent (default) survives it.
- Testing on a remote machine without store=active: one mistake and only a reboot can save you if you used active.
- Disabling the adapter your RDP session uses.
- Forgetting that add address and add dnsservers replace DHCP settings.
- Deleting an excluded port range with a different numberofports than you added.
32.11 Interview tips
- Static IP from CLI: get the name from netsh interface show interface, then ipv4 set address static IP mask gateway, ipv4 add dnsservers, verify with ipv4 show config (PowerShell: New-NetIPAddress).
- Back to DHCP: ipv4 set address name=Ethernet source=dhcp (DNS: set dnsservers source=dhcp).
- set address replaces, add address appends.
- Mention store=active as a safe way to test changes remotely.