Tag: network interfaces

How to configure the network interface to use a dynamic IP address (DHCP) in PowerShell

Note: all settings in this article must be done with administrator rights.

Dynamic Host Configuration Protocol (DHCP) allows the network adapter to obtain the correct network settings without manually configuring network interfaces.

To manage a network interface, you need to know its index. The list of interfaces and their indices can be obtained with the following command:

Get-NetIPAddress | Format-Table

Before activating DHCP on the network interface, you need to delete the IP address settings (if any).

Remove the static IP address:

Remove-NetIPAddress -InterfaceIndex INTERFACE_INDEX

Remove the default gateway:

Remove-NetRoute -InterfaceIndex INTERFACE_INDEX

For example:

Remove-NetIPAddress -InterfaceIndex 18
Remove-NetRoute -InterfaceIndex 18

Remove an IP address using a pipeline:

Get-NetIPAddress -IPAddress 192.168.0.1 | Remove-NetIPAddress

This command removes all of the IP addresses with the address 192.168.0.1.

To enable DHCP on the network interface, use a command of the form:

Set-NetIPInterface -InterfaceIndex INTERFACE_INDEX -Dhcp Enabled

For example:

Set-NetIPInterface -InterfaceIndex 18 -Dhcp Enabled

If you want to delete records about DNS servers so that DNS settings are also obtained automatically, then run the following command:

Set-DnsClientServerAddress -InterfaceIndex INTERFACE_INDEX -ResetServerAddresses

Please note that you can use DHCP and DNS server settings at the same time (specify the desired DNS servers for the network interface, which automatically receive an IP address and other network settings). To set the DNS server settings on the network interface with the specified index, use a command of the form:

Set-DnsClientServerAddress -InterfaceIndex INTERFACE_INDEX -ServerAddresses ("8.8.8.8","8.8.4.4")

How to set IP address, netmask, default gateway and DNS for a network interface in PowerShell

Setting a static IP, netmask and gateway for a network adapter in PowerShell is a little confusing but possible. This post will show you how to do this using the New-NetIPAddress and Set-NetIPAddress cmdlets.

The old way of configuring network interfaces is using the netsh command. First, we look at the name of the network interfaces of any of the following commands (they are equivalent):

netsh interface show interface
netsh int show int

Then we set the parameters for the selected network interface (in the following commands this is the interface named “Wi-Fi”):

netsh interface ip set address "Wi-Fi" static <IP> <SUBNET> <GATEWATE>
netsh interface ip set dns "Wi-Fi" static <DNS SERVER>

But the netsh command is deprecated and Microsoft warns that they can remove netsh and we should use Powershell.

To manage a network adapter, we need to know its index, you can get it with the command:

Get-NetIPAddress

The Get-NetIPAddress cmdlet retrieves the IP address configuration such as IPv4 addresses, IPv6 addresses, and IP interfaces to which the addresses are associated.

You can use the following command to present information more conveniently:

Get-NetIPAddress | Format-Table

Additionally, you can sort by interface index number:

Get-NetIPAddress | Sort-Object -Property InterfaceIndex | Format-Table

Then, using the New-NetIPAddress cmdlet, you can set a static IP address:

New-NetIPAddress -InterfaceIndex NTERFACE_INDEX -IPAddress IP_ADDRESS -PrefixLength MASK -DefaultGateway GATEWAY

For example, the command to configure the network adapter with index 18 static IP address 192.168.1.71 with subnet mask /24 (255.255.255.0) and default gateway 192.168.1.1:

New-NetIPAddress -InterfaceIndex 18 -IPAddress 192.168.1.71 -PrefixLength 24 -DefaultGateway 192.168.1.1

This command will work fine if you want to switch from a dynamic IP address to a static one. But if you want to change the parameters of the network interface on which a static IP address is already set, then you will receive the following error:

New-NetIPAddress: Instance DefaultGateway already exists

To work around this, you can use Set-NetIPAddress, but this cmdlet does not accept the -DefaultGateway parameter, so a new address is assigned, but with the old gateway.

For example, the following command:

Set-NetIPAddress -InterfaceIndex 18 -IPAddress 192.168.1.71 -PrefixLength 24 -DefaultGateway 192.168.1.1

fails with the error:

Set-NetIPAddress: A parameter cannot be found that matches parameter name 'DefaultGateway'.

How do I remove the gateway to start with New-NetIPAddress, or replace the gateway when using Set-NetIPAddress?

It is necessary to use the following algorithm of actions.

Remove the static IP address:

Remove-NetIPAddress -InterfaceIndex INTERFACE_INDEX

Removing the default gateway:

Remove-NetRoute -InterfaceIndex INTERFACE_INDEX

Add a new IP address and gateway:

New-NetIPAddress -InterfaceIndex INTERFACE_INDEX -AddressFamily IPv4 10.1.2.4 -PrefixLength 24 -Type Unicast -DefaultGateway 10.1.2.255

To set the DNS server settings on the network interface with the specified index, use a command of the form:

Set-DnsClientServerAddress -InterfaceIndex INTERFACE_INDEX -ServerAddresses ("8.8.8.8","8.8.4.4")

How to prevent NetworkManager from managing a specific interface? (SOLVED)

NetworkManager is a Linux service that manages various network interfaces, including physical, such as Ethernet and wireless, and virtual, such as VPN and other tunnels. Network Manager can be configured to manage some or all of the system interfaces.

NetworkManager has a graphical interface - this is the applet that opens when you click on the network icon located next to the clock, as well as the network and network settings windows, which you can access from the applet. NetworkManager comes preinstalled on many Linux distributions by default.

While NetworkManager is an excellent service for managing the day-to-day needs of a user's computer, its effects are usually not optimal for a testing environment. NetworkManager can independently, without a user request, change the MAC addresses of network interfaces, as well as change their state - for example, when a USB Wi-Fi adapter is connected, it is NetworkManager that sets it in the up state, it can bring the wireless interface out of monitor mode and switch it to its normal state.

If you want to avoid this for certain network interfaces, then besides the obvious solution to stop the NetworkManager service or remove NetworkManager altogether, there are other options. By the way, if you are just not satisfied with the fact that the MAC addresses are changed to arbitrary, then this can be configured or disabled in the NetworkManager itself.

What is an unmanaged interface in NetworkManager

The unmanaged state in NetworkManager for a network interface means that NetworkManager does not interact with this network interface in any way: it does not enable it, does not change its operating mode, does not change the MAC address, does not use it to scan networks, and does not even show it in the list of network interfaces - that is, NetworkManager pretends that this interface simply does not exist in the system.

How to determine if NetworkManager is managing a specific network interface

The unmanaged state only has an effect on the NetworkManager itself. You, as usual, you can see all network interfaces with the command

ip a

or only wireless interfaces by command:

iw dev

At the same time, it is not indicated in any way whether this interface is controlled by NetworkManager.

As already mentioned, if your device is not in the list of network interfaces shown by NetworkManager, then it may be in an unmanaged state.

But to be sure of this, you can use the nmcli command, which is a command line tool for managing NetworkManager. To display a list of network interfaces and their status, run the command:

nmcli dev status

For unmanaged devices, it will show “unmanaged”.

Suppose I want NetworkManager not to change in any way the settings of the wireless network interface named wlp0s20f0u1.

How to temporarily move a network interface to unmanaged in NetworkManager

The network interface can be disconnected from NetworkManager management temporarily or permanently (so that this status remains after reboot). In fact, you can change the status at any time.

To temporarily make the interface unmanaged, run a command like:

nmcli dev set INTERFACE managed no

For instance:

nmcli dev set wlp0s20f0u1 managed no

We check:

nmcli dev status

Pay attention to the line:

wlp0s20f0u1   wifi      unmanaged  --             

This setting is reset not only after restarting the computer, but also after disconnecting and connecting the network interface (plug and unplug).

How to put network interface to unmanaged in NetworkManager so that this persists after reboot

To prevent NetworkManager from touching the network interface immediately after starting the service and to keep this setting after a reboot, you need to use the keyfile method.

To do this, open the file /etc/NetworkManager/NetworkManager.conf:

sudo gedit /etc/NetworkManager/NetworkManager.conf

And add lines like this there:

[keyfile]
unmanaged-devices=mac:00:11:22:33:44:55;mac:66:77:88:99:00:aa

You can list any number of MAC addresses.

You can view the MAC addresses of network interfaces with the command

ip a

But remember that NetworkManager could already assign an arbitrary MAC address, to see the real one, run a command like this:

nmcli dev set INTERFACE managed no

List the MAC address of each interface that Network Manager should ignore, separated by semicolons. Make sure the MAC addresses listed here are written in lowercase.

Newer versions of NetworkManager may also use the more general alternative KEYFILE method, which does not include the actual MAC addresses and instead uses the interface names. This syntax replaces the above [keyfile] section with the following:

[keyfile]
unmanaged-devices=interface-name:eth*,except:interface-name:eth0;interface-name:wlan*

For example, I want NetworkManager not to manage the wlp0s20f0u1, wlp0s20f0u2 and wlp0s20f0u3 network interfaces, then my lines are as follows:

[keyfile]
unmanaged-devices=interface-name:wlp0s20f0u1;interface-name:wlp0s20f0u2;interface-name:wlp0s20f0u3

Save your changes and restart the service:

sudo systemctl restart NetworkManager

Let's check:

nmcli dev status

How to return a network interface under NetworkManager control

Now the listed network interfaces will always be “unmanaged”. But they can be set back under the control of NetworkManager. To do this, simply roll back the changes made in the NetworkManager.conf file and restart the service.

If you took the interface out of the NetworkManager control with the nmcli command, then you can return it to its previous state with the command:

nmcli dev set INTERFACE managed yes

How to turn off NetworkManager

If you want to completely stop NetworkManager so that it stops managing all network interfaces at once, then run the command:

sudo systemctl stop NetworkManager

If you want to remove NetworkManager from startup, then run:

sudo systemctl disable NetworkManager

To start NetworkManager again and add it to startup run:

sudo systemctl start NetworkManager
sudo systemctl enable NetworkManager

Why does VirtualBox lose connection when changing MAC address (SOLVED)

Virtual machine, after changing MAC address of eth0, the connection does not work. When changing only the last three octets, the situation is the same. Restarting the modem does not give results, there is no filtering by MAC addresses. Why is this happening and how can I fix it?

VirtualBox does not support changing the MAC address inside the guest virtual machine.

The “official” method of operation is to change the address in the virtual machine settings when the guest computer is completely turned off.

Guest MAC address information is generated by the physical hardware, in this case the VirtualBox network adapter.

VirtualBox uses the hardware on the host and provides the MAC address for the guest as if it were in metal. But a reasonable question arises, because on a physical computer, the MAC address is also hard-coded into the network card and it is impossible to change it on it, but Linux supports changing the MAC address to arbitrary ones and the network is working fine, why is there such a difference?

And by the way, changing the MAC address in the virtual machine breaks the Internet connection for the wired interface only. For Wi-Fi wireless network interfaces (connected via USB), changing the MAC address does not lead to any problems, they work fine, there is a network connection.

There are two possible reasons for this behavior:

1. Whatever MAC address is hard-coded into the network card, the final decision whether or not to accept a packet is made by the operating system kernel. This is in the case of a physical computer. In the case of a virtual machine, it is up to VirtualBox, which may not be aware of what exactly is happening in the virtual machine and that it has changed its MAC address. Therefore VirtualBox does not know about the new MAC address and does not forward Ethernet frames to it.

2. The differences between wired and wireless networks are obvious: in wireless networks, radio waves propagate freely and every device within reach has access to absolutely all transmitted frames. As for the wired network, the sender sends Ethernet frames to a specific port (in the sense of the device's network port). That is, it is possible that the data is simply not sent by the sender to the new MAC address, since it does not know anything about it.

However, some users wrote that they managed to get the wired interface in the virtual machine to work after changing the MAC address - for this they put it in promiscuous mode. This method did not work for me (the network is still unavailable), but you can try it yourself (replace the eth0 interface name with your own):

sudo macchanger -s eth0 # check the current MAC address
sudo macchanger -r eth0 # change the MAC address to random
sudo ip link set dev eth0 promisc on # put the network interface in promiscuous mode
ping 8.8.8.8 # check if the network is working
# sudo macchanger -p eth0 # return the old MAC address if the network is down

As for the wireless interfaces in the virtual machine, everything works there without the need to switch the network interface to promiscuous mode.

Commands for changing the MAC-address of the Wi-Fi card (replace the wlan0 interface name with your own):

sudo ip link set dev wlan0 down # this is required
sudo macchanger -s wlan0 # check the current MAC address
sudo macchanger -r wlan0 # change the MAC address to random
sudo ip link set dev wlan0 up # this is required
# sudo macchanger -p wlan0 # if needed, return the old MAC address
Loading...
X