Article directory
overview
This document mainly records the method of setting the boot command Ubuntu
for the system /etc/rc.local
.
system version
Ubuntu 20.04
Set boot command
1. View rc-local
service status
sudo systemctl status rc-local
The output is as follows:
root@localhost:~$ systemctl status rc-local
● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
Drop-In: /usr/lib/systemd/system/rc-local.service.d
└─debian.conf
Active: inactive (dead)
Docs: man:systemd-rc-local-generator(8)
2. Set rc-local
the service to start at boot
sudo systemctl enable rc-local
Note: Ubuntu 20.04 cannot directly enable
rc-local
the service, an error will be reported, andrc-local
the service file needs to be created manually. The command error is as follows:
root@localhost:~$ sudo systemctl enable rc-local
[sudo] password for simple:
The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
Possible reasons for having this kind of units are:
• A unit may be statically enabled by being symlinked from another unit's
.wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
instance name specified.
3. Manually create system self-starting services
sudo vim /etc/systemd/system/rc-local.service
Enter the following:
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
4. Create rc.local
the file
sudo vim /etc/rc.local
Enter the following:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
# _IP=$(hostname -I) || true
# if [ "$_IP" ]; then
# printf "My IP address is %s\n" "$_IP"
# fi
exit 0
5. Set rc.local
file permissions
sudo chmod +x /etc/rc.local
6. Add boot command
Take deleting a redundant static route as an example. For example, there is a dual network card environment. One of the network cards needs to be used for intranet data transmission, and the other network card is used for accessing Internet data transmission. However, after the configuration is completed, it is found that The internal network data transmission is normal, but the Internet data transmission is abnormal. After investigation, it is found that the default gateway of the internal network card is set to the internal network gateway by mistake when configuring the internal network card, resulting in abnormal Internet data transmission. Therefore, this item needs to be deleted. Redundant static routes. The command to delete is sudo route del -net 0.0.0.0 dev enp0s8
, and now I want to automatically execute this command when booting, so I need to add this command to rc.local
the file.
sudo vim /etc/rc.local
exit 0
Add the following before
sudo route del -net 0.0.0.0 dev enp0s8
7. Enable rc-local
service
sudo systemctl start rc-local
sudo systemctl enable rc-local
8. View rc-local
service status
sudo systemctl status rc-local
9. Restart the system
sudo reboot
After the system restarts, you can check the routing table and find that the redundant static route has been deleted.