如何在 Linux 中更改 MAC 地址
MAC地址
在向你展示如何在 Linux 中更改 MAC 地址之前，让我们首先讨论为什么要更改它。


可能有几个原因。也许你不希望在公共网络上公开你的实际 [MAC 地址](https://en.wikipedia.org/wiki/MAC_address)（也称为物理地址）？还有可能是网络管理员可能已在路由器或防火墙中阻止了特定的 MAC 地址。


一个实用的“好处”是某些公共网络（例如机场 WiFi）允许在有限的时间内免费上网。如果你还想继续使用，那么伪造 Mac 地址可能会欺骗网络，让它认为是一台新设备。这也是一个有名的原因。


![](/data/attachment/album/202003/18/120702qdjyb7hvyj7bsrj7.jpg)


我将展示更改 MAC 地址（也称为欺骗/伪造 MAC 地址）的步骤。


### 在 Linux 中更改 MAC 地址


让我们一步步来：


#### 查找你的 MAC 地址和网络接口


让我们找出一些[关于 Linux 中网卡的细节](https://itsfoss.com/find-network-adapter-ubuntu-linux/)。使用此命令获取网络接口详细信息：



```
ip link show
```

在输出中，你将看到一些详细信息以及 MAC 地址：



```
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
    link/ether 94:c6:f8:a7:d7:30 brd ff:ff:ff:ff:ff:ff
3: enp0s31f6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DORMANT group default qlen 1000
    link/ether 38:42:f8:8b:a7:68 brd ff:ff:ff:ff:ff:ff
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default
    link/ether 42:02:07:8f:a7:38 brd ff:ff:ff:ff:ff:ff
```

如你所见，在这里，我的网络接口称为 `enp0s31f6`，MAC 地址为 `38:42:f8:8b:a7:68`。


你可能需要在安全的地方记录下来，以便稍后还原到该原始 MAC 地址。


现在你可以继续更改 MAC 地址。


注意！


如果在当前使用的网络接口上执行此操作，那么可能会中断你的网络连接。因此，请在其他网卡上尝试使用此方法，或者准备重启网络。


#### 方法 1：使用 Macchanger 更改 MAC 地址


![](/data/attachment/album/202003/18/120521ebk9jzk6jks5tf5y.jpg)


[Macchanger](https://github.com/alobbs/macchanger) 是查看、修改和操作网卡 MAC 地址的简单程序。它几乎在所有 GNU/Linux 操作系统中都可用，你可以使用发行版的包安装程序进行安装。


在 Arch Linux 或 Manjaro 上：



```
sudo pacman -S macchanger
```

在 Fedora、CentOS 和 RHEL 上：



```
sudo dnf install macchanger
```

在 Debian、Ubuntu、Linux Mint、Kali Linux 上：



```
sudo apt install macchanger
```

**重要！**系统会要求你选择是否应将 `macchanger` 设置为在每次启动或关闭网络设备时自动运行。每当你接到网线或重启 WiFi 时，它都会提供一个新的 MAC 地址。


![Not a good idea to run it automatically](/data/attachment/album/202003/18/120522wxnm1kn1mybdw5n1.jpg)


我建议不要自动运行它，除非你确实需要每次更改 MAC 地址。因此，选择“No”（按 `Tab` 键），然后按回车键继续。


**如何使用 Macchanger 更改 MAC 地址**


你还记得网络接口名称吗？你在前面的步骤中获得了它。


现在，要将随机 MAC 地址分配给该网卡，请使用：



```
sudo macchanger -r enp0s31f6
```

更改 MAC 后，使用以下命令进行验证：



```
ip addr
```

现在你将看到已经伪造 MAC。


要将 MAC 地址更改为特定值，请使用以下命令指定自定义 MAC 地址：



```
macchanger --mac=XX:XX:XX:XX:XX:XX
```

其中 XX:XX:XX:XX:XX:XX 是你要更改的新 MAC。


最后，要将 MAC 地址恢复为其原始硬件值，请运行以下命令：



```
macchanger -p enp0s31f6
```

但是，你不必如此。重启系统后，更改将自动丢失，并且实际的 MAC 地址将再次恢复。


你可以随时查看手册页以获取更多详细信息。


#### 方法 2：使用 iproute2 更改 Mac 地址（中级知识）


我建议你使用 macchanger，但如果你不想使用它，那么可以使用另一种方法在 Linux 中更改 MAC 地址。


首先，使用以下命令关闭网卡：



```
sudo ip link set dev enp0s31f6 down
```

接下来，使用以下命令设置新的 MAC：



```
sudo ip link set dev enp0s31f6 address XX:XX:XX:XX:XX:XX
```

最后，使用以下命令重新打开网络：



```
sudo ip link set dev enp0s31f6 up
```

现在，验证新的 MAC 地址：



```
ip link show enp0s31f6
```

就是这些了。你已经成功地在 Linux 中修改了 MAC 地址。敬请期待 FOSS 更多有关 Linux 教程和技巧的文章。




---


via: <https://itsfoss.com/change-mac-address-linux/>


作者：[Dimitrios Savvopoulos](https://itsfoss.com/author/itsfoss/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

Before I show you how to change the MAC address in Linux, let’s first discuss why you would change it in the first place.

You may have several reasons. Maybe you don’t want your actual [MAC address](https://en.wikipedia.org/wiki/MAC_address?ref=itsfoss.com) (also called physical address) to be exposed on a public network? Other cases can be that the network administrator might have blocked a particular MAC address in the router or firewall.

One practical ‘benefit’ is that some public network (like Airport WiFi) allows free internet for a limited time. If you want to use the internet beyond that, spoofing your Mac address may trick the network into believing that it’s a new device. It’s a famous meme as well.

![Airport Wifi Meme](https://itsfoss.com/content/images/wordpress/2020/03/airport_wifi_meme.jpg)

I am going to show the steps for changing MAC address (also called spoofing/faking MAC address). Let’s go through each step:

## Step 1: Find your MAC address and network interface

Let’s find out some [details about the network card in Linux](https://itsfoss.com/find-network-adapter-ubuntu-linux/). Use this command to get the network interface details:

`ip link show`


In the output, you’ll [see your IP address](https://itsfoss.com/check-ip-address-ubuntu/), MAC address and other details:

```
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
link/ether 94:c6:f8:a7:d7:30 brd ff:ff:ff:ff:ff:ff
3: enp0s31f6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DORMANT group default qlen 1000
link/ether 38:42:f8:8b:a7:68 brd ff:ff:ff:ff:ff:ff
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default
link/ether 42:02:07:8f:a7:38 brd ff:ff:ff:ff:ff:ff
```


Output of `ip link show`


As you can see, in this case, my network interface is called ** enp0s31f6** and its MAC address is

**.**

**38:42:f8:8b:a7:68**

You may want to note it down in a secure place to revert to this original MAC address later on.

Now you may proceed to change the MAC address.

**! If you do this on a network interface that is currently in use, probably your network connection will be terminated. So either try this method on an additional card or**

**Attention***.*

*be prepared to restart your network*## Step 2: Changing MAC address in Linux

There's a popular tool dedicated to changing MAC addresses (`macchanger`

), or you could use the `ip`

command as well. We'll go through both of them.

If you say `ifconfig`

, remember that you're officially living in the past, since it's now one of the [deprecated Linux commands](https://itsfoss.com/deprecated-linux-commands?ref=itsfoss.com). Adieu, * net-tools*!

### Method 1: Change MAC address using Macchanger

[Macchanger](https://github.com/alobbs/macchanger?ref=itsfoss.com) is a simple utility to view, modify, and manipulate MAC addresses for your Network interface cards. It is available in almost all GNU/Linux operating systems and you can install it using the package installer of your distribution.

On Arch Linux or Manjaro:

`sudo pacman -S macchanger`


On Fedora, CentOS, RHEL:

`sudo dnf install macchanger`


On Debian, Ubuntu, Linux Mint, and Kali Linux:

`sudo apt install macchanger`


**You’ll be asked to specify whether macchanger should be set up to run automatically every time a network device is brought up or down. This gives a new MAC address whenever you attach an Ethernet cable or re-enable WiFi.**

**Important!**![Configuring Macchanger](https://itsfoss.com/content/images/wordpress/2020/03/configuring_mcchanger.jpg)

**)**

**Not a good idea to run it automatically**I recommend not running it automatically unless you really need to change your MAC address every time. So, choose No (by pressing the tab key) and hit Enter key to continue.

#### How to Use Macchanger to change MAC address

Do you remember your network interface name? You got it in Step 1 earlier.

Now, to assign any random MAC address to this network card, use:

`sudo macchanger -r enp0s31f6`


After changing the MAC id, verify it using the command:

`ip addr`


You will now see that MAC has been spoofed.

To change the MAC address to a specific value, specify any custom MAC address using the command:

`macchanger --mac=XX:XX:XX:XX:XX:XX `


where `XX:XX:XX:XX:XX:XX`

is the new MAC id that you want to change.

Finally, to revert the MAC address to its original hardware value, run the following command (where `-p`

tag refers to the permanent/original MAC address of the hardware):

`macchanger -p enp0s31f6`


However, you don’t have to do this. Once you reboot the system, the changes will be automatically lost, and the actual MAC address will be restored again.

You can always check the [man page of macchanger](https://manpages.ubuntu.com/manpages/jammy/man1/macchanger.1.html) for more details.

### Method 2: Changing Mac address using iproute2 [intermediate knowledge]

I would recommend using Macchanger but if you don’t want to use it, there is another way to change the [MAC address in Linux](https://itsfoss.com/find-mac-address-linux/).

First, turn off the network card using the command (this was the warning, you're manually restarting the network here):

`sudo ip link set dev enp0s31f6 down`


Next, set the new MAC for the network card using the command:

`sudo ip link set dev enp0s31f6 address XX:XX:XX:XX:XX:XX`


Finally, turn the network back on with this command:

`sudo ip link set dev enp0s31f6 up`


Now, verify the new MAC address:

`ip link show enp0s31f6`


That’s it. You have successfully changed the MAC address in true Linux style. Interested in Linux networking? Check out these commands.

[21 Basic Linux Networking Commands You Should KnowA list of basic Linux networking commands that will help you troubleshoot network issues, monitor packets, connect devices, and much more.](https://itsfoss.com/basic-linux-networking-commands/)![](https://itsfoss.com/content/images/wordpress/2016/06/essential-linux-networking-commmands.png)


![](https://itsfoss.com/content/images/wordpress/2016/06/essential-linux-networking-commmands.png)

Stay tuned with It’s FOSS for more Linux tutorials and tips.