Article Title: very practical Linux system boot speed up I have a trick !. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Linux is a very good operating system, but a long start time is one of the shortcomings of this system. Can I use some settings to shorten the Linux Startup time? Of course.
Linux is a very good operating system, but a long start time is one of the shortcomings of this system. Can I use some settings to shorten the Linux Startup time? Of course. This article will show you how to shorten the startup time without sacrificing the availability of the Linux system. To achieve this goal, you need to understand the various services of the system and their subordination, so that they can be started at the same time as allowed, thus greatly saving the system startup time.
Preparations
If you want to use the method described in this article to set the Linux system, you must first be familiar with the setting language of the Linux system. Setting the system startup process is a dangerous action, which may cause the system to fail to start. Once this happens, you 'd better restart the system and select single user mode to restore and restart the system. Of course, the most important thing is to back up important data before setting.
Next, we recommend that you test the modification before modifying the local system. If you only have one computer, we recommend that you use the user mode Linux system (UML) for testing. UML is a kernel patch that translates the Linux kernel into binary so that users can run the Linux system as they do with common software. In the event of an accident, only UML is lost, while the original system is safe and sound.
Linux Startup sequence and Runlevel
Linux system startup involves multiple steps. Here we will introduce the system startup steps after the kernel is loaded. You can run the/sbin/runlevel command to view the current runlevel of the system. Runlevel is a number used in Linux to distinguish between advanced startup settings. These numbers are very common, and most of them have clear meanings.
How does init initialize the system?
Init can use an ASC Ⅱ setup file to learn how to change the runlevel of the system. In general, this setting file can guide init to execute the/etc/rc. d/rc command to obtain runlevel information.
System Service
The various system services required by the user are started by the rc command program. The potential services of the system are complicated. Most Linux systems generally only provide sshd (SecureShell Service), syslog (System Log tool), and lpd (Print Service) functions.
You can find all services in the/etc/rc. d/directory. If you open this directory, you will find that many services are just a shell program, which only directs other programs to complete the work. Some may ask, how does the rc program know which programs should be run at each runlevel? The answer is that under the/etc/rc. d/directory, there are some directories that correspond to their own runlevel. Their names are rc *. d/. For example, runlevel 5 is/etc/rc. d/rc5.d /. Each rc *. d/directory contains two symbolic links that can be connected to a specified service program in the/etc/init. d/directory.
Transformation
If you decide to start the Linux system and enter the graphic mode (runlevel 5), when init directs the rc command to run the program and informs it of the runlevel level, the rc Instruction Program will be directed to/etc/rc. d/rc5.d. The rc command program execution link can be divided into two parts. First, it will execute all the links whose names start with "K" and pass the "stop" parameter to them, and stop all services that these links point. After these services are stopped, the rc command program will execute all the links with names starting with "S" and pass the "start" parameter to them to start the services that these links point.
In addition, the rc command program can determine the execution sequence of the link based on the two decimal numbers of The Link name. Execute the Link first with a small number.
The following is an example to help you better understand the situation. When the user starts the system to enter runlevel 5, the first link to be executed is K05saslauthd, because it is prefixed with "K, and the last two decimal numbers must be smaller than other links starting with "K. The first start link to be executed is S05kudzu. The last execution is S99local. Below is the link to the program in runlevel 5.
# cd /etc/rc.d/rc5.d # ls -al total 8 drwxr-xr-x 2 root root 4096 Jul 15 09:29 . drwxr-xr-x 10 root root 4096 Jun 21 08:52 .. lrwxrwxrwx 1 root root 19 Jan 1 2000 K05saslauthd -> ../init.d/saslauthd lrwxrwxrwx 1 root root 20 Feb 1 2003 K15postgresql -> ../init.d/postgresql lrwxrwxrwx 1 root root 13 Jan 1 2000 K20nfs -> ../init.d/nfs lrwxrwxrwx 1 root root 14 Jan 1 2000 K24irda -> ../init.d/irda lrwxrwxrwx 1 root root 17 Jan 1 2000 K35winbind -> ../init.d/winbind lrwxrwxrwx 1 root root 15 Jan 1 2000 K50snmpd -> ../init.d/snmpd …… lrwxrwxrwx 1 root root 18 Feb 8 11:15 K92iptables -> ../init.d/iptables lrwxrwxrwx 1 root root 19 Feb 1 2003 K95firstboot -> ../init.d/firstboot lrwxrwxrwx 1 root root 15 Jan 1 2000 S05kudzu -> ../init.d/kudzu lrwxrwxrwx 1 root root 14 Jun 21 08:55 S09isdn -> ../init.d/isdn lrwxrwxrwx 1 root root 17 Jan 1 2000 S10network -> ../init.d/network lrwxrwxrwx 1 root root 16 Jan 1 2000 S12syslog -> ../init.d/syslog lrwxrwxrwx 1 root root 17 Jan 1 2000 S13portmap -> ../init.d/portmap lrwxrwxrwx 1 root root 17 Jan 1 2000 S14nfslock -> ../init.d/nfslock lrwxrwxrwx 1 root root 18 Jan 1 2000 S17keytable -> ../init.d/keytable lrwxrwxrwx 1 root root 16 Jan 1 2000 S20random -> ../init.d/random lrwxrwxrwx 1 root root 16 Jun 21 08:52 S24pcmcia -> ../init.d/pcmcia lrwxrwxrwx 1 root root 15 Jan 1 2000 S25netfs -> ../init.d/netfs lrwxrwxrwx 1 root root 14 Jan 1 2000 S26apmd -> ../init.d/apmd lrwxrwxrwx 1 root root 16 Jan 1 2000 S28autofs -> ../init.d/autofs lrwxrwxrwx 1 root root 14 Jan 1 2000 S55sshd -> ../init.d/sshd lrwxrwxrwx 1 root root 20 Jan 1 2000 S56rawdevices -> ../init.d/rawdevices lrwxrwxrwx 1 root root 16 Jan 1 2000 S56xinetd -> ../init.d/xinetd lrwxrwxrwx 1 root root 14 Feb 1 2003 S58ntpd -> ../init.d/ntpd lrwxrwxrwx 1 root root 13 Jun 21 10:42 S60afs -> ../init.d/afs …… lrwxrwxrwx 1 root root 18 Jan 1 2000 S80sendmail -> ../init.d/sendmail lrwxrwxrwx 1 root root 13 Jan 1 2000 S85gpm -> ../init.d/gpm lrwxrwxrwx 1 root root 15 Mar 22 08:24 S85httpd -> ../init.d/httpd …… lrwxrwxrwx 1 root root 14 Jul 15 09:29 S98wine -> ../init.d/wine lrwxrwxrwx 1 root root 13 Feb 8 17:26 S99db2 -> ../init.d/db2 lrwxrwxrwx 1 root root 11 Jun 21 08:52 S99local -> ../rc.local
|
If you want to temporarily terminate a runlevel service, you only need to delete the corresponding link. However, manual setting of links is a heavy and error-prone task, but it is a useful helper called chkconfig.
Identify unterminated services
To understand which services are not terminated, run the/sbin/chkconfig-list command. The following shows the command execution result. You can clearly see that each row contains eight items. The chkconfig command can also be used to enable or terminate a service.
afs 0:off 1:off 2:off 3:on 4:off 5:on 6:off anacron 0:off 1:off 2:on 3:on 4:on 5:on 6:off apmd 0:off 1:off 2:on 3:on 4:on 5:on 6:off atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off autofs 0:off 1:off 2:off 3:on 4:on 5:on 6:off crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off ………
|
The first column on the left shows the service name, and the second column shows the runlevel and current status. For example, ntpd (no interaction background program in network time) is set to be enabled only at runlevel 3 and runlevel 5, while sshd is enabled at runlevel 2, 3, 4, and 5. The user may find that the services not started at runlevel 0 and runlevel 6 are because runlevel 0 and runlevel 6 indicate that the system is terminated and of course there is no service.
Subordination
As mentioned above, each runlevel service must be run one by one during system startup. It may be easy for users to think that if multiple services can run simultaneously, it will inevitably save a lot of system startup time. Unfortunately, this method does not work because there is a subordination between various services. Traditional Linux systems do not allow users to specify the subordination between services.
A simple example is provided to illustrate how to clarify the subordination between services. From the perspective that ntpd needs a network base, we can conclude that ntpd depends on network services. The relationship can be expressed using the following statement:
You can determine that the netfs service also depends on the network service, and the autofs service also depends on the network service. Now you can list a simple subordination table:
ntpd : network netfs : network autofs : network
|
It means that once the network service is enabled, ntpd, netfs, and autofs services can be run simultaneously. Assuming that it takes 10 seconds to start each service, it takes 40 seconds to start the above four services in the traditional method, it takes only 20 seconds to use the method mentioned above. In fact, the start time of each service is different, but the truth is the same.
For example, the lpd service (S60lpd) needs to run after the network service, but for a home system with an inkjet printer interface, you do not have to establish a network connection before installing the printer. In this case, the more reasonable situation is that the printer service is prior to the network service. In addition, the crond Service (S90crond) should also run after the network service. However, unless you have a cron file that can use remote computer files, the crond Service does not have to run before the network service.
It can be seen that some services depend on other services to run, some services are completely independent, and whether they run is irrelevant to other services.