官术网_书友最值得收藏!

Working with runlevels

We can use various runlevels for various tasks, as we've seen earlier in this chapter. Let's use the CentOS 6.5 distribution. To see the runlevel at the shell in real time, we can use the runlevel command:

[philip@localhost Desktop]$ runlevel
N 5
[philip@localhost Desktop]$

From the preceding output, the N means the previous runlevel. In our case, we have not changed the runlevel. The 5 indicates we are currently in runlevel 5. We can also run another command to display the runlevel. We can use the who command with the -r option, as can be seen here:

[philip@localhost Desktop]$ who -r
run-level 5 2018-06-20 08:09
[philip@localhost Desktop]$

From the preceding output, we can see a further description, namely run-level 5 using the who –r command.

Now, we can change the runlevel in our CentOS 6.5 distribution by leveraging the init or telinit commands. Let's see how we would change from runlevel 5 to runlevel 1:

[philip@localhost Desktop]$ who -r
run-level 5 2018-06-20 08:09
[philip@localhost Desktop]$ init 1

We will get an error when we press Enter; the reason being, we need root permission to change runlevel 5 to runlevel 1 in the CentOS 6.5 distribution:

[philip@localhost Desktop]$ init 1
init: Need to be root
[philip@localhost Desktop]$

Now, let's authenticate as the root user and retry the init 1 command:

[philip@localhost Desktop]$ su -
Password:
[root@localhost ~]# init 1

Now, we will be placed into runlevel 1, which removes the GUI and takes us directly into the shell. This runlevel 1 is typically known as a single user, which we would use for recovery:

From the preceding output, we have run the runlevel and who -r commands and verified that we are indeed in runlevel 1.

Now, let's bring back up the system into GUI state, namely runlevel 5:

Now, when we run the runlevel command in the GUI, we will see the previous runlevel 1 replacing the N with an S in the runlevel command:

[philip@localhost Desktop]$ runlevel
S 5
[philip@localhost Desktop]$

Similarly, we can run the who command with the -r option to see more information:

[philip@localhost Desktop]$ who -r
run-level 5 2018-06-20 08:20 last=S
[philip@localhost Desktop]$

Now, let's say we want to turn on a daemon in some runlevel. We will use dnsmasq for demonstration. First, let's verify that the dnsmasq service is presently off:

[philip@localhost Desktop]$ chkconfig | grep dnsmasq
dnsmasq 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[philip@localhost Desktop]$

Great! Let's now turn on the dnsmasq daemon only for runlevels 3-5:

[philip@localhost Desktop]$ chkconfig --levels 345 dnsmasq on
You do not have enough privileges to perform this operation.
[philip@localhost Desktop]$

From the preceding output, we get an error because we need root privilege to turn on/off the daemon in the respective runlevels. Let's retry as the root user:

[philip@localhost Desktop]$ su -
Password:
[root@localhost ~]# chkconfig --levels 345 dnsmasq on
[root@localhost ~]#

Great! Now let's rerun the chkconfig command and only look for the dnsmasq daemon:

[root@localhost ~]# chkconfig | grep dnsmasq
dnsmasq 0:off 1:off 2:off 3:on 4:on 5:on 6:off
[root@localhost ~]#

From the preceding output, we can see that the dnsmasq daemon is now set to on in runlevels 3-5.

主站蜘蛛池模板: 连州市| 巩义市| 高安市| 黑水县| 伊川县| 屏东市| 泸溪县| 宁武县| 尼勒克县| 崇左市| 栖霞市| 绥阳县| 汉沽区| 德格县| 横峰县| 黔江区| 临夏市| 济南市| 红原县| 石柱| 丹巴县| 志丹县| 东光县| 定襄县| 慈溪市| 奉新县| 明光市| 敦化市| 永靖县| 兰西县| 德安县| 同心县| 咸阳市| 泽普县| 措勤县| 比如县| 青川县| 惠来县| 牙克石市| 镇原县| 秭归县|