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

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.

主站蜘蛛池模板: 舒城县| 崇明县| 都江堰市| 东源县| 叶城县| 清苑县| 达拉特旗| 余江县| 元阳县| 老河口市| 岳普湖县| 白银市| 宣城市| 巴东县| 罗定市| 湟中县| 晋宁县| 应城市| 元江| 石家庄市| 宽城| 长阳| 上思县| 杭锦后旗| 桃园县| 开远市| 龙游县| 安康市| 五河县| 盐池县| 新野县| 蓝田县| 连江县| 毕节市| 桦南县| 涟源市| 游戏| 霍邱县| 黎川县| 郧西县| 广安市|