Ubuntu18.04 detailed tutorial on permanently changing mac address

1, Preliminary preparation

1. As shown in the following figure, simply view the network card information using the ifconfig command to obtain the network card name and the corresponding mac address.

2. The root user executes the command: ll /etc | grep 'rc'; You can see the following files

wzl@wzl-ThinkPad-T14-Gen-1:/$ ll /etc | grep 'rc'
-rw-r--r--   1 root root     2319  6 June 18, 2020 bash.bashrc
-rw-r--r--   1 root root     1748  2 May 25, 2020 inputrc
-rw-r--r--   1 root root     9808  8 June 14, 2020 nanorc
drwxr-xr-x   2 root root     4096  2 June 25-18:03 rc0.d/
drwxr-xr-x   2 root root     4096  2 June 25-18:03 rc1.d/
drwxr-xr-x   2 root root     4096  8 September 13:24 rc2.d/
drwxr-xr-x   2 root root     4096  8 June 19-16:08 rc3.d/
drwxr-xr-x   2 root root     4096  8 September 13:24 rc4.d/
drwxr-xr-x   2 root root     4096  8 September 13:24 rc5.d/
drwxr-xr-x   2 root root     4096  2 June 25-18:03 rc6.d/
drwxr-xr-x   2 root root     4096  2 June 25-18:03 rcS.d/
-rw-r--r--   1 root root     4942  7 May 25, 2019 wgetrc

Of which rc0 ~ 6 0 ~ 6 in D represents the operation level:

0: In the system shutdown (shutdown) mode, the default operation level of the system cannot be set to 0, otherwise it cannot be started normally and will be shut down automatically as soon as it is started.
1: Single user mode, root Permission, which is used for system maintenance. Remote login is prohibited, just like Windows Login in safe mode under.
2: Multi user mode, no NFS Network support.
3: Complete multi-user text mode, including NFS´╝îAfter logging in, enter the console command line mode.
4: The system is not used. It is generally not used. In some special cases, it can be used to do some things. For example, when the laptop battery runs out, you can switch to this mode to make some settings.
5: Graphical mode, log in and enter graphic mode GUI Mode or GNOME,KDE Graphical interface, such as X Window System.
6: In the restart mode, the default operation level cannot be set to 6, otherwise it cannot be started normally, and it will be started all the time.

3. The following instructions are commonly used to temporarily change the mac address of the system.

 sudo ifconfig   Network card name  down
 sudo ifconfig   Network card name  hw ether  mac address
 sudo ifconfig   Network card name  up

But this method will fail after the computer restarts. In order to solve this problem, we hope that these three lines of instructions can be executed every time the system starts, so as to achieve the purpose of permanently changing the mac address (it will fail after reinstalling the system, and we need to follow the steps again). We can create a bash program, and then set the program to start. The specific operations are as follows:

  • In / etc / init Create a startup script under directory D (put the command in it)
  • Then increase its executable permissions
  • Finally, set rcxx D soft connection to startup script

2, Preparation of startup documents

The example of the startup file is as follows. After editing, save it (the name I set is rclocal) and save it in this directory (/ etc/init.d /):

sudo ifconfig enp0s31f6 down
sudo ifconfig enp0s31f6 hw ether  54:75:db:fc:3a:b8
sudo ifconfig enp0s31f6 up

3, Add permissions to startup files

Open the directory where the startup file is located, / etc / init D /, and then execute the following command. The file name should be modified according to its own definition:

chmod +755 rclocal

4, Set up software connection

Check the existing soft connections: ll / etc / RC3 d/

wzl@wzl-ThinkPad-T14-Gen-1:/$ ll  /etc/rc3.d/
Total consumption 16
drwxr-xr-x   2 root root  4096  8 June 19-16:08 ./
drwxr-xr-x 137 root root 12288  8 September 13:24 ../
lrwxrwxrwx   1 root root    27  2 June 25-18:00 K01speech-dispatcher -> ../init.d/speech-dispatcher*
lrwxrwxrwx   1 root root    15  2 June 25-18:00 S01acpid -> ../init.d/acpid*
lrwxrwxrwx   1 root root    17  2 June 25-18:00 S01anacron -> ../init.d/anacron*
lrwxrwxrwx   1 root root    16  2 June 25-18:00 S01apport -> ../init.d/apport*
lrwxrwxrwx   1 root root    22  2 June 25-18:00 S01avahi-daemon -> ../init.d/avahi-daemon*
lrwxrwxrwx   1 root root    24  2 June 25-19:40 S01binfmt-support -> ../init.d/binfmt-support*
lrwxrwxrwx   1 root root    19  2 June 25-18:00 S01bluetooth -> ../init.d/bluetooth*

It can be found that "K number" or "S number" are added in front of the startup script, where k represents a program to Kill, S represents a program to Start, and the number immediately followed represents the sequence of starting / stopping a program. The smaller the number, the first to Start (the specific value of the number can be set according to the situation).

Here, the commands we use to set the soft connection are (the file name needs to be changed according to the actual situation):

ln -s /etc/init.d/rclocal   /etc/rc3.d/S01rclocal

After this setting, start and restart, check whether the program can be started automatically, and check whether the mac address changed by the network card has taken effect.

Keywords: Linux

Added by sasa on Mon, 20 Dec 2021 08:21:12 +0200