Linux基础篇(系统管理)(含服务相关命令)

接上篇:Linux基础篇(远程登录)

 

服务管理

计算机中一个正在被执行的程序或进程,被叫做”进程“(process)(以d结尾的被称为守护进程);

启动之后一直存在,常驻内存的进程,被称为”服务“(service);

 

基本语法:(CentOS 6版本)

-- serviceName:服务名
-- start:开启
-- stop:停止
-- restart:重启
-- status:状态
service serviceName start|stop|restart|status

查看服务:(CentOS 6版本)

-- .d:守护进程
-- serviceName:服务名
/etc/init.d/serviceName
-- 查看network服务(网络服务)
[root@hadoop100 ~]# /etc/init.d/network
Usage: /etc/init.d/network {start|stop|status|restart|force-reload}
-- 查看 /etc/init.d/ 下的服务文件
[root@hadoop100 ~]# ll /etc/init.d/
总用量 40
-rw-r--r--. 1 root root 18281 5月  22 2020 functions
-rwxr-xr-x. 1 root root  4569 5月  22 2020 netconsole
-rwxr-xr-x. 1 root root  7928 5月  22 2020 network
-rw-r--r--. 1 root root  1160 10月  2 2020 README

在 CentOS 7版本中,保留了 CentOS 7 network 服务(兼容),并使用了新服务 NetworkManager 来管理 网络;

基本语法:(CentOS 7版本)

-- start:启动
-- stop:停止
-- restart:重启
-- status:状态
-- serviceName:服务名
systemctl start|stop|restart|status serviceName

查看服务:(CentOS 7版本)

-- 查看 /usr/lib/systemd/ 文件夹下的所有服务
[root@hadoop100 ~]# ls /usr/lib/systemd/
catalog                 systemd-activate          systemd-machined           systemd-sysv-install
import-pubring.gpg      systemd-backlight         systemd-machine-id-commit  systemd-timedated
ntp-units.d             systemd-binfmt            systemd-modules-load       systemd-udevd
rhel-autorelabel        systemd-bootchart         systemd-pull               systemd-update-done
rhel-configure          systemd-cgroups-agent     systemd-quotacheck         systemd-update-utmp
rhel-dmesg              systemd-coredump          systemd-random-seed        systemd-user-sessions
rhel-dmraid-activation  systemd-cryptsetup        systemd-readahead          systemd-vconsole-setup
rhel-domainname         systemd-fsck              systemd-remount-fs         system-generators
rhel-import-state       systemd-hibernate-resume  systemd-reply-password     system-preset
rhel-loadmodules        systemd-hostnamed         systemd-rfkill             system-shutdown
rhel-readonly           systemd-importd           systemd-shutdown           system-sleep
scripts                 systemd-initctl           systemd-shutdownd          user
system                  systemd-journald          systemd-sleep              user-generators
systemd                 systemd-localed           systemd-socket-proxyd      user-preset
systemd-ac-power        systemd-logind            systemd-sysctl

CentOS 7版本启动了两个网络服务

-- 查看NetworkManager状态
[root@hadoop100 ~]# systemctl status NetworkManager
● NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
   Active: active (running) since 四 2022-07-28 11:39:50 CST; 2 weeks 0 days ago
     Docs: man:NetworkManager(8)
 Main PID: 761 (NetworkManager)
    Tasks: 3
   CGroup: /system.slice/NetworkManager.service
           └─761 /usr/sbin/NetworkManager --no-daemon

8月 12 09:44:20 hadoop100 NetworkManager[761]: <info>  [1660268660.1493] device (ens33): state change: config -...ged')
8月 12 09:44:20 hadoop100 NetworkManager[761]: <info>  [1660268660.1596] device (ens33): state change: ip-confi...ged')
8月 12 09:44:20 hadoop100 NetworkManager[761]: <info>  [1660268660.1607] device (ens33): state change: ip-check...ged')
8月 12 09:44:20 hadoop100 NetworkManager[761]: <info>  [1660268660.1609] device (ens33): state change: secondar...ged')
8月 12 09:44:20 hadoop100 NetworkManager[761]: <info>  [1660268660.1616] manager: NetworkManager state is now C...LOCAL
8月 12 09:44:20 hadoop100 NetworkManager[761]: <info>  [1660268660.1671] manager: NetworkManager state is now C..._SITE
8月 12 09:44:20 hadoop100 NetworkManager[761]: <info>  [1660268660.1674] policy: set 'ens33' (ens33) as default...d DNS
8月 12 09:44:20 hadoop100 NetworkManager[761]: <info>  [1660268660.1712] device (ens33): Activation: successful...ated.
8月 12 09:44:20 hadoop100 NetworkManager[761]: <info>  [1660268660.1721] manager: NetworkManager state is now C...LOBAL
8月 12 10:25:07 hadoop100 NetworkManager[761]: <info>  [1660271107.0709] agent-manager: req[0x5654bdd11c80, :1....tered
Hint: Some lines were ellipsized, use -l to show in full.
-- 查看network状态
[root@hadoop100 ~]# systemctl status network
● network.service - LSB: Bring up/down networking
   Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)
   Active: active (exited) since 五 2022-08-12 09:44:20 CST; 50min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 7932 ExecStop=/etc/rc.d/init.d/network stop (code=exited, status=0/SUCCESS)
  Process: 8095 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS)
    Tasks: 0

8月 12 09:44:19 hadoop100 systemd[1]: Starting LSB: Bring up/down networking...
8月 12 09:44:19 hadoop100 network[8095]: 正在打开环回接口: [  确定  ]
8月 12 09:44:20 hadoop100 network[8095]: 正在打开接口 ens33: 连接已成功激活(D-Bus 活动路径:/org/freedesktop/on/11)
8月 12 09:44:20 hadoop100 network[8095]: [  确定  ]
8月 12 09:44:20 hadoop100 systemd[1]: Started LSB: Bring up/down networking.
Hint: Some lines were ellipsized, use -l to show in full.

此时我们关闭 network,并重新启动 NetworkManager

-- 停止 centos 6版本遗留的 网络服务 network
[root@hadoop100 ~]# systemctl stop network
-- 停止之后发现无法访问网络
[root@hadoop100 ~]# ping www.baidu.com
ping: www.baidu.com: 未知的名称或服务
-- 此时重启 centos 7版本推荐的 网络服务 NetworkManager即可
[root@hadoop100 ~]# systemctl restart NetworkManager
[root@hadoop100 ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=128 time=26.3 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=128 time=25.0 ms

注意事项:若是使用CMD窗口、软件工具等连接的虚拟机,关闭网络服务 network 后自动断开连接;

 

设置服务自启动

输入命令 setup 即可看到一个交互式设置工具(图形化界面)

 

使用命令配置开机自启动(CentOS 6操作命令)

查看自启动服务列表(chkconfig --list)

[root@hadoop100 ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:关    3:关    4:关    5:关    6:关

更改服务自启动语法

-- serviceName:服务名称
-- off:关闭
-- on:开启
-- 注意:当前语法未指定系统级别,因此当使用 on 开启的时候,开启的是所有运行级别,但某些运行级别不存在服务的不受影响;
chkconfig serviceName off|on

-- runLevel:指定的运行级别
-- serviceName:服务名称
-- off:关闭
-- on:开启
chkconfig --level runLevel serviceName off|on
[root@hadoop100 ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:关    3:关    4:关    5:关    6:关
[root@hadoop100 ~]# chkconfig network on
[root@hadoop100 ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:开    3:开    4:开    5:开    6:关
[root@hadoop100 ~]# chkconfig network off
[root@hadoop100 ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:关    3:关    4:关    5:关    6:关
[root@hadoop100 ~]# chkconfig --level 5 network on
[root@hadoop100 ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole      0:关    1:关    2:关    3:关    4:关    5:关    6:关
network         0:关    1:关    2:关    3:关    4:关    5:开    6:关
View Code

 

使用命令配置开机自启动(CentOS 7操作命令)

-- 查看所有服务列表
systemctl list-unit-files

-- enable:开启
-- disable:关闭
-- serviceName:服务名称
systemctl enable|disable serviceName
[root@hadoop100 ~]# systemctl status NetworkManager
● NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2022-08-12 11:42:44 CST; 2h 16min ago
     Docs: man:NetworkManager(8)
 Main PID: 724 (NetworkManager)
   CGroup: /system.slice/NetworkManager.service
           └─724 /usr/sbin/NetworkManager --no-daemon

8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4141] device (ens33): state change: prepare ...ged')
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4805] device (ens33): state change: config -...ged')
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4842] device (ens33): state change: ip-confi...ged')
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4849] device (ens33): state change: ip-check...ged')
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4850] device (ens33): state change: secondar...ged')
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4855] manager: NetworkManager state is now C...LOCAL
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4975] manager: NetworkManager state is now C..._SITE
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4976] policy: set 'ens33' (ens33) as default...d DNS
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.5059] device (ens33): Activation: successful...ated.
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.5063] manager: NetworkManager state is now C...LOBAL
Hint: Some lines were ellipsized, use -l to show in full.
[root@hadoop100 ~]# systemctl disable NetworkManager
Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
Removed symlink /etc/systemd/system/network-online.target.wants/NetworkManager-wait-online.service.
[root@hadoop100 ~]# systemctl status NetworkManager
● NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; disabled; vendor preset: enabled)
   Active: active (running) since 五 2022-08-12 11:42:44 CST; 2h 18min ago
     Docs: man:NetworkManager(8)
 Main PID: 724 (NetworkManager)
   CGroup: /system.slice/NetworkManager.service
           └─724 /usr/sbin/NetworkManager --no-daemon

8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4141] device (ens33): state change: prepare ...ged')
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4805] device (ens33): state change: config -...ged')
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4842] device (ens33): state change: ip-confi...ged')
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4849] device (ens33): state change: ip-check...ged')
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4850] device (ens33): state change: secondar...ged')
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4855] manager: NetworkManager state is now C...LOCAL
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4975] manager: NetworkManager state is now C..._SITE
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.4976] policy: set 'ens33' (ens33) as default...d DNS
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.5059] device (ens33): Activation: successful...ated.
8月 12 13:32:38 hadoop100 NetworkManager[724]: <info>  [1660282358.5063] manager: NetworkManager state is now C...LOBAL
Hint: Some lines were ellipsized, use -l to show in full.
View Code
-- enable:开启
-- disabale:关闭
-- static:静态,依赖于其他服务
[root@hadoop100 ~]# systemctl list-unit-files
UNIT FILE                                     STATE
proc-sys-fs-binfmt_misc.automount             static
dev-hugepages.mount                           static
dev-mqueue.mount                              static
proc-fs-nfsd.mount                            static
proc-sys-fs-binfmt_misc.mount                 static
run-vmblock\x2dfuse.mount                     disabled
sys-fs-fuse-connections.mount                 static
sys-kernel-config.mount                       static
sys-kernel-debug.mount                        static
tmp.mount                                     disabled
var-lib-nfs-rpc_pipefs.mount                  static
brandbot.path                                 disabled
cups.path                                     enabled
systemd-ask-password-console.path             static
systemd-ask-password-plymouth.path            static
systemd-ask-password-wall.path                static
session-1.scope                               static
session-3.scope                               static
session-9.scope                               static
abrt-ccpp.service                             enabled
abrt-oops.service                             enabled
abrt-pstoreoops.service                       disabled
abrt-vmcore.service                           enabled
abrt-xorg.service                             enabled
abrtd.service                                 enabled
accounts-daemon.service                       enabled
alsa-restore.service                          static
alsa-state.service                            static
anaconda-direct.service                       static
anaconda-nm-config.service                    static
anaconda-noshell.service                      static
anaconda-pre.service                          static
anaconda-shell@.service                       static
anaconda-sshd.service                         static
anaconda-tmux@.service                        static
anaconda.service                              static
arp-ethers.service                            disabled
atd.service                                   enabled
auditd.service                                enabled
auth-rpcgss-module.service                    static
autofs.service                                disabled
autovt@.service                               enabled
avahi-daemon.service                          enabled
blk-availability.service                      enabled
bluetooth.service                             enabled
bolt.service                                  static
brandbot.service                              static
...
...
...
View Code

init与systemd的区别:

init

以前的Linux启动都是用init进程。启动服务;

缺点:

  • 启动时间长。init进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
  • 启动脚本复杂。init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。

systemd

在较新的linux系统上,都使用systemd 取代了init,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。systemd为系统启动和管理提供了完整的解决方案。它提供了一组命令。字母d是守护进程(daemon)的缩写。查看systemd 的版本:

systemctl --version

CentOS 7防火墙自启动操作

-- 查看当前防火墙状态,firewalld:是防火墙服务的守护线程,(.service可以省略不写),当前防火墙正在运行,并且是自启动
[root@hadoop100 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2022-08-12 11:42:44 CST; 2h 37min ago
     Docs: man:firewalld(1)
 Main PID: 683 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─683 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

8月 12 11:42:42 hadoop100 systemd[1]: Starting firewalld - dynamic firewall daemon...
8月 12 11:42:44 hadoop100 systemd[1]: Started firewalld - dynamic firewall daemon.
8月 12 11:42:45 hadoop100 firewalld[683]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure... now.
Hint: Some lines were ellipsized, use -l to show in full.
-- 关闭防火墙
[root@hadoop100 ~]# systemctl stop firewalld.service
-- 查看防火墙状态,当前防火墙已经关闭,但依旧是自启动
[root@hadoop100 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since 五 2022-08-12 14:22:44 CST; 42s ago
     Docs: man:firewalld(1)
  Process: 683 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 683 (code=exited, status=0/SUCCESS)

8月 12 11:42:42 hadoop100 systemd[1]: Starting firewalld - dynamic firewall daemon...
8月 12 11:42:44 hadoop100 systemd[1]: Started firewalld - dynamic firewall daemon.
8月 12 11:42:45 hadoop100 firewalld[683]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure... now.
8月 12 14:22:43 hadoop100 systemd[1]: Stopping firewalld - dynamic firewall daemon...
8月 12 14:22:44 hadoop100 systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
-- 关闭防火墙自启动
[root@hadoop100 ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
-- 当前防火墙是关闭状态,并且自启动也被关闭
[root@hadoop100 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

8月 12 11:42:42 hadoop100 systemd[1]: Starting firewalld - dynamic firewall daemon...
8月 12 11:42:44 hadoop100 systemd[1]: Started firewalld - dynamic firewall daemon.
8月 12 11:42:45 hadoop100 firewalld[683]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure... now.
8月 12 14:22:43 hadoop100 systemd[1]: Stopping firewalld - dynamic firewall daemon...
8月 12 14:22:44 hadoop100 systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
-- 开启防火墙自启动
[root@hadoop100 ~]# systemctl enable firewalld.service
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
-- 开启防火墙
[root@hadoop100 ~]# systemctl start firewalld.service
-- 查看当前防火墙状态,已经启动,并且是开机自启动
[root@hadoop100 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2022-08-12 14:30:44 CST; 1min 48s ago
     Docs: man:firewalld(1)
 Main PID: 4954 (firewalld)
    Tasks: 2
   CGroup: /system.slice/firewalld.service
           └─4954 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

8月 12 14:30:45 hadoop100 firewalld[4954]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter ...in?).
8月 12 14:30:45 hadoop100 firewalld[4954]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter ...in?).
8月 12 14:30:45 hadoop100 firewalld[4954]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter ...in?).
8月 12 14:30:45 hadoop100 firewalld[4954]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter ...name.
8月 12 14:30:45 hadoop100 firewalld[4954]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter ...name.
8月 12 14:30:45 hadoop100 firewalld[4954]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter ...in?).
8月 12 14:30:45 hadoop100 firewalld[4954]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter ...in?).
8月 12 14:30:45 hadoop100 firewalld[4954]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter ...in?).
8月 12 14:30:45 hadoop100 firewalld[4954]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter ...in?).
8月 12 14:30:45 hadoop100 firewalld[4954]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -w --table filter ...in?).
Hint: Some lines were ellipsized, use -l to show in full.
View Code

 

CentOS 6运行级别

-- 查看默认运行级别,在根目录下etc目录下的inittab文件中
vim /etc/inittab
-- CentOS 6中,可以使用当前命令进入到指定的运行级别(CentOS 7依旧兼容此命令)
init runLevel
-- 例如
init 3
init 5

 

CentOS 7运行级别

multi-user.target:运行级别3

graphical.target:运行级别5

使用命令 systemctl get-default查看系统运行级别

使用命令 systemctl set-default设置系统运行级别

-- 查看系统运行级别
[root@hadoop100 ~]# systemctl get-default
-- 运行级别 5
graphical.target
-- 设置系统运行级别 3
[root@hadoop100 ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
-- 再次输入,即可有效(自测过)
[root@hadoop100 ~]# systemctl set-default multi-user.target

关机命令 

-- 将数据由内存写入到硬盘
sync

-- 停机、关闭系统,但不断电
halt

-- 关机、断电
poweroff

-- 重启系统(相当于 shutdown -r now)
reboot

-- 关机操作
-- 选项:-H:相当于 --halt;
-- 选项:-r:相当于 reboot;
-- 选项:-P:相当于 --poweroff;
-- 时间:可以写一个系统时间,可以写一个数字,表示分钟,可以写 now 表示现在;
shutdown [选项] 时间
-- 关机,后面不跟时间,则默认一分钟之后关机,可使用 shutdown -c 命令进行取消操作
[root@hadoop100 ~]# shutdown
Shutdown scheduled for2022-08-12 14:35:13 CST, use 'shutdown -c' to cancel.
[root@hadoop100 ~]#
Broadcast message from root@hadoop100 (Fri 2022-08-12 14:34:14 CST):

The system is going down for power-off at Fri 2022-08-12 14:35:13 CST!
-- 使用快捷键 crtl + C
^C
[root@hadoop100 ~]# shutdown -c

Broadcast message from root@hadoop100 (Fri 2022-08-12 14:34:34 CST):

The system shutdown has been cancelled at Fri 2022-08-12 14:35:34 CST!

-- 系统时间为15:30时关机,可使用 shutdown -c 命令取消操作
[root@hadoop100 ~]# shutdown 15:30
Shutdown scheduled for2022-08-12 15:30:00 CST, use 'shutdown -c' to cancel.

-- 3分钟之后关机,可使用 shutdown -c 命令取消操作
[root@hadoop100 ~]# shutdown 3
Shutdown scheduled for2022-08-12 14:39:21 CST, use 'shutdown -c' to cancel.
[root@hadoop100 ~]#
Broadcast message from root@hadoop100 (Fri 2022-08-12 14:36:21 CST):

The system is going down for power-off at Fri 2022-08-12 14:39:21 CST!

-- 立刻关机
[root@hadoop100 ~]# shutdown now
View Code

 

总结命令

-- 开启服务、停止服务、重启服务、查看服务状态
-- start:开启
-- stop:停止
-- restart:重启
-- status:状态
-- serviceName:服务名
systemctl start|stop|restart|status serviceName

-- 查看服务自启动列表
systemctl list-unit-files

-- 对服务设置是否自启动
-- enable:开启
-- disbale:关闭
-- serviceName:服务名
systemctl enable|disable serviceName
-- 输入setup命令可进入到一个管理服务的图形化界面
setup
-- 查看默认运行级别,在根目录下etc目录下的inittab文件中(CentOS 7被遗弃)
vim /etc/inittab

-- 查看当前系统运行级别
systemctl get-default

-- 设置当前系统运行级别
-- runLevel:选择 multi-user.target、graphical.target中的一种
systemctl set-default runLevel
-- 立刻关机
shutdown now
shutdown -h now
poweroff
-- 指定多长时间关机,写3表示3分钟之后,写15:30表示系统时间为15:30时关机
shutdown -h [时间] shutdown [时间]

-- 立刻重启
shutdown -r now
reboot

 

接下篇:Linux实操篇(查看命令帮助文档)

posted @ 2022-08-12 09:55  DHaiLin  阅读(227)  评论(0编辑  收藏  举报