Linux系统管理

Linux 2024-08-15 34 次浏览 次点赞

[root@fmujieserver ~]# ls /usr/sbin/ | grep service
service
[root@fmujieserver ~]# service network start

基本语法

service 服务名 start|stop|resart|status

经验技巧

查看服务的方法:etc/init.d/服务名/////.d(守护进程)

[root@fmujieserver /]# cd etc/init.d
[root@fmujieserver init.d]# ls
bt  functions  ifritd  jdog_service  mysqld  netconsole  network  nginx  php-fpm-72  pure-ftpd  README  redis
[root@fmujieserver init.d]# ls -al
total 100
drwxr-xr-x.  2 root root  4096 Jun 29 12:54 .
drwxr-xr-x. 10 root root  4096 Nov 17  2022 ..
-rwxr-xr-x   1 root root 15900 Aug 14 22:16 bt
-rw-r--r--.  1 root root 18281 May 22  2020 functions
lrwxrwxrwx   1 root root    41 Jun 25 21:57 ifritd -> /usr/local/share/jcloud/ifrit/sbin/ifritd
-rwxrwxr-x   1 root root  6281 Jun 26 06:59 jdog_service
-rwxr-xr-x   1 root root 10672 Jun 25 23:21 mysqld
-rwxr-xr-x.  1 root root  4569 May 22  2020 netconsole
-rwxr-xr-x.  1 root root  7928 May 22  2020 network
-rwxr-xr-x   1 root root  3558 Mar  6 11:48 nginx
-rwxr-xr-x   1 root root  2408 Jun 25 22:58 php-fpm-72
-rwxr-xr-x   1 root root  1447 Aug 17  2016 pure-ftpd
-rw-r--r--.  1 root root  1160 Oct  2  2020 README
-rwxr-xr-x   1 root root  2150 Aug 19  2021 redis

centos7

基本语法

systemctl start|stop|resart|status 服务名

#### 经验技巧

查看服务的方法:/usr/lib/systemd/system////target(一组服务的集合)

[root@fmujieserver /]# cd /usr/lib/systemd/system
[root@fmujieserver system]# ls -al
total 1044
drwxr-xr-x. 25 root root 20480 Jun 29 13:03 .
drwxr-xr-x. 13 root root  4096 Nov 17  2022 ..
-rw-r--r--.  1 root root   275 Aug  9  2019 arp-ethers.service
-rw-r--r--.  1 root root  1384 Aug  8  2019 auditd.service
-rw-r--r--.  1 root root   646 Oct 14  2021 auth-rpcgss-module.service
lrwxrwxrwx.  1 root root    14 Nov 17  2022 autovt@.service -> getty@.service
-rw-r--r--.  1 root root   517 Oct  2  2020 basic.target
drwxr-xr-x.  2 root root  4096 Nov 17  2022 basic.target.wants
-r--r--r--.  1 root root   429 Oct  1  2020 blk-availability.service
-rw-r--r--.  1 root root   379 Oct  2  2020 bluetooth.target
-rw-r--r--.  1 root root   160 May 22  2020 brandbot.path
-rw-r--r--.  1 root root   116 May 22  2020 brandbot.service
-rw-r--r--.  1 root root   209 Aug  6  2019 chrony-dnssrv@.service
-rw-r--r--.  1 root root   138 Aug  6  2019 chrony-dnssrv@.timer
-rw-r--r--.  1 root root   495 Aug  8  2019 chronyd.service
-rw-r--r--.  1 root root   472 Sep 19  2018 chrony-wait.service
-rw-r--r--.  1 root root   386 Apr  1  2020 conntrackd.service
-rw-r--r--.  1 root root   787 Oct  2  2020 console-getty.service
-rw-r--r--.  1 root root   749 Oct  2  2020 console-shell.service
-rw-r--r--.  1 root root   808 Oct  2  2020 container-getty@.service
-rw-r--r--.  1 root root   294 Oct 20  2020 cpupower.service
-rw-r--r--.  1 root root   318 Aug  9  2019 crond.service
-rw-r--r--.  1 root root   425 Oct  2  2020 cryptsetup-pre.target
-rw-r--r--.  1 root root   372 Oct  2  2020 cryptsetup.target
……

案例实操

[root@fmujieserver system]# 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 Wed 2024-08-14 22:16:23 CST; 16h ago
     Docs: man:systemd-sysv-generator(8)
  Process: 711 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS)

Aug 14 22:16:23 fmujieserver systemd[1]: Starting LSB: Bring up/down networking...
Aug 14 22:16:23 fmujieserver network[711]: Bringing up loopback interface:  [  OK  ]
Aug 14 22:16:23 fmujieserver network[711]: Bringing up interface eth0:  [  OK  ]
Aug 14 22:16:23 fmujieserver systemd[1]: Started LSB: Bring up/down networking.

Linux运行级别

image-20240815143952472.png

centos7运行级别简化

multi-user.target等价于原运行级别3(多用户含网络, 无图形界面)

graphical.target等价于原运行级别5(多用户含网络,有图形界面)

查看当前运行级别(启动后的默认运行级别)

[root@fmujieserver /]# systemctl get-default
multi-user.target

修改当前运行级别

[root@fmujieserver /]# systemctl set-default multi-user.target/graphical.target

centos6种(因为是centos7, 所以全注释了)

[root@fmujieserver /]# vim /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target

如何更改系统服务的自启动

centos

[root@fmujieserver /]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

bt                 0:off    1:off    2:on    3:on    4:on    5:on    6:off
jdog_service       0:off    1:off    2:on    3:on    4:on    5:on    6:off
mysqld             0:off    1:off    2:on    3:on    4:on    5:on    6:off
netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
nginx              0:off    1:off    2:on    3:on    4:on    5:on    6:off
php-fpm-72         0:off    1:off    2:on    3:on    4:on    5:on    6:off
pure-ftpd          0:off    1:off    2:on    3:on    4:on    5:on    6:off
redis              0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@fmujieserver /]# chkconfig network off/on
[root@fmujieserver /]# chkconfig --level 3 network off/on

使用XXXctl

[root@fmujieserver /]# systemctl status NetworkManager
● NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2024-08-14 22:16:22 CST; 18h ago
     Docs: man:NetworkManager(8)
 Main PID: 569 (NetworkManager)
   CGroup: /system.slice/NetworkManager.service
           ├─569 /usr/sbin/NetworkManager --no-daemon
           └─697 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-eth0.pid -lf /var/lib/NetworkManager/dhclient-8085b9a9-fb82-47dc-...

Aug 15 09:22:04 fmujieserver dhclient[697]: DHCPACK from 172.16.0.2 (xid=0x30353e59)
Aug 15 09:22:04 fmujieserver NetworkManager[569]: <info>  [1723684924.2303] dhcp4 (eth0):   address 172.16.0.3
Aug 15 09:22:04 fmujieserver NetworkManager[569]: <info>  [1723684924.2304] dhcp4 (eth0):   plen 16 (255.255.0.0)
Aug 15 09:22:04 fmujieserver NetworkManager[569]: <info>  [1723684924.2304] dhcp4 (eth0):   classless static route 169.254.169.0/24 gw 172.16.0.1
Aug 15 09:22:04 fmujieserver NetworkManager[569]: <info>  [1723684924.2304] dhcp4 (eth0):   gateway 172.16.0.1
Aug 15 09:22:04 fmujieserver NetworkManager[569]: <info>  [1723684924.2304] dhcp4 (eth0):   lease time 86400
Aug 15 09:22:04 fmujieserver NetworkManager[569]: <info>  [1723684924.2304] dhcp4 (eth0):   nameserver '103.224.222.222'
Aug 15 09:22:04 fmujieserver NetworkManager[569]: <info>  [1723684924.2304] dhcp4 (eth0):   nameserver '103.224.222.223'
Aug 15 09:22:04 fmujieserver NetworkManager[569]: <info>  [1723684924.2304] dhcp4 (eth0): state changed bound -> bound
Aug 15 09:22:04 fmujieserver dhclient[697]: bound to 172.16.0.3 -- renewal in 39809 seconds.

Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)开机自启动

[root@fmujieserver /]# systemctl disable NetworkManager  # 禁止开机自启动

查看所有的服务

[root@fmujieserver /]# 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  
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
systemd-ask-password-console.path             static  
systemd-ask-password-plymouth.path            static  
systemd-ask-password-wall.path                static  
session-116.scope                             static  
session-c1.scope                              static  
arp-ethers.service                            disabled
auditd.service                                enabled 
auth-rpcgss-module.service                    static  
autovt@.service                               enabled 
blk-availability.service                      disabled
brandbot.service                              static  
……

防火墙开机自启动

centos6

iptables

centos7

firewalld

[root@fmujieserver /]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2024-08-14 22:16:22 CST; 18h ago
     Docs: man:firewalld(1)
 Main PID: 547 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─547 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

Aug 14 22:16:21 fmujieserver systemd[1]: Starting firewalld - dynamic firewall daemon...
Aug 14 22:16:22 fmujieserver systemd[1]: Started firewalld - dynamic firewall daemon.
Aug 14 22:16:22 fmujieserver firewalld[547]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be ...g it now.
Hint: Some lines were ellipsized, use -l to show in full.

关机重启

shutdown: 默认一分钟后关机,在输入shutdown -c取消关机操作

shutdown now: 马上关机

shutdown -n: n分钟后关机

shutdown 15:28: 定时关机

[root@fmujieserver /]# shutdown 16:50
Shutdown scheduled for Thu 2024-08-15 16:50:00 CST, use 'shutdown -c' to cancel.
[root@fmujieserver /]# 
Broadcast message from root@fmujieserver (Thu 2024-08-15 16:43:39 CST):

The system is going down for power-off at Thu 2024-08-15 16:50:00 CST!

[root@fmujieserver /]# shutdown -c

Broadcast message from root@fmujieserver (Thu 2024-08-15 16:43:48 CST):

The system shutdown has been cancelled at Thu 2024-08-15 16:44:48 CST!
  • sync:将数据由内存同步到硬盘中(预读,迟写(buffer))
  • halt:停机,关闭系统,但不断电
  • poweroff:关机,断电
  • reboot:重启,相当于shutdown -r now
  • shutdown [选项] 时间

    • -H: 相当于--halt,停机
    • -r: 相当于reboot,重启
    • -P/-h: 相当于关机

本文由 fmujie 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论

召唤看板娘