Linux 服务管理

1 服务管理命令 ⭐️⭐️⭐️⭐️⭐️

ctl大礼包
hostnamectl 修改和查看主机名
systemctl 服务管理
timedatectl 查看和修改时间(时区)
localectl 查看和修改语言,字符集

1.1 systemctl

  • 一般来说rpm/deb,yum/apt安装的服务,这些服务默认通过systemctl管理.
  • 二进制安装,编译安装默认无法通过systemctl管理服务.
  • 服务开关重启,查看状态
  • 服务开机自启动设置
点击查看代码
# 1.查看crond服务状态(是否运行,是否开机自启动)
[root@Kylin-V10-sp3 ~systemctl status crond
* crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2024-09-04 11:52:02 CST; 7h ago
 Main PID: 877 (crond)
    Tasks: 1
   Memory: 7.9M
   CGroup: /system.slice/crond.service
           `-877 /usr/sbin/crond -n

Sep 04 11:52:02 Kylin-V10-sp3 crond[877]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 94% if used.)
Sep 04 11:52:02 Kylin-V10-sp3 crond[877]: (CRON) INFO (running with inotify support)
Sep 04 12:01:01 Kylin-V10-sp3 CROND[1225]: (root) CMD (run-parts /etc/cron.hourly)
Sep 04 13:01:01 Kylin-V10-sp3 CROND[1410]: (root) CMD (run-parts /etc/cron.hourly)
Sep 04 14:01:01 Kylin-V10-sp3 CROND[1598]: (root) CMD (run-parts /etc/cron.hourly)
Sep 04 15:01:01 Kylin-V10-sp3 CROND[1782]: (root) CMD (run-parts /etc/cron.hourly)
Sep 04 16:01:01 Kylin-V10-sp3 CROND[2049]: (root) CMD (run-parts /etc/cron.hourly)
Sep 04 17:01:01 Kylin-V10-sp3 CROND[2723]: (root) CMD (run-parts /etc/cron.hourly)
Sep 04 18:01:01 Kylin-V10-sp3 CROND[2994]: (root) CMD (run-parts /etc/cron.hourly)
Sep 04 19:01:01 Kylin-V10-sp3 CROND[3178]: (root) CMD (run-parts /etc/cron.hourly)
[root@Kylin-V10-sp3 ~]# 
[root@Kylin-V10-sp3 ~]# 
[root@Kylin-V10-sp3 ~]# sysyemctl is-enabled crond
-bash: sysyemctl: command not found
[root@Kylin-V10-sp3 ~]# systemctl is-enabled crond
enabled
[root@Kylin-V10-sp3 ~]# 

# 2.开启crond服务 设置crond服务开机自启动
[root@Kylin-V10-sp3 ~]# systemctl start crond
[root@Kylin-V10-sp3 ~]# 
[root@Kylin-V10-sp3 ~]# systemctl enable crond
[root@Kylin-V10-sp3 ~]# 

# 3.关闭防火墙firewalld,ufw(ubt系统)
[root@Kylin-V10-sp3 ~]# systemctl stop firewalld
[root@Kylin-V10-sp3 ~]# 
[root@Kylin-V10-sp3 ~]# systemctl is-active firewalld
inactive
[root@Kylin-V10-sp3 ~]# systemctl is-enabled firewalld
enabled
[root@Kylin-V10-sp3 ~]# 

[root@ubuntu2204 ~]# systemctl stop ufw
[root@ubuntu2204 ~]#
[root@ubuntu2204 ~]# systemctl is-active ufw
inactive
[root@ubuntu2204 ~]# systemctl is-enabled ufw
disabled
[root@ubuntu2204 ~]# 

'''
Linux常用系统开机自启基础服务
  crond 定时任务
  sshd 远程连接服务
  rsyslog 日志服务
'''

1.2 timedatectl

查看时间(时区),修改时间(时区).

点击查看代码
# 未来时间,日期的修改通过date/ntpdate
# 1.查看时间与时区(Kylin和Ubuntu以以一样)
[root@Kylin-V10-sp3 ~]# timedatectl
               Local time: Wed 2024-09-04 19:45:27 CST
           Universal time: Wed 2024-09-04 11:45:27 UTC
                 RTC time: Wed 2024-09-04 11:45:27
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@Kylin-V10-sp3 ~]# 

# 2.修改时区为上海(Kylin和Ubuntu以以一样)
[root@Kylin-V10-sp3 ~]# 
[root@Kylin-V10-sp3 ~]# timedatectl 
list-timezones   revert           set-ntp          set-timezone     show-timesync    timesync-status  
ntp-servers      set-local-rtc    set-time         show             status           
[root@Kylin-V10-sp3 ~]# timedatectl set-timezone Asia/Shanghai
[root@Kylin-V10-sp3 ~]# 

1.3 localectl语言,字符集

点击查看代码
# 1 查看当前系统的语言和字符集

System Locale: LANG=zh_CN.UTF-8
  VC Keymap: cn
  X11 Layout: cn
# LANG==LANGUAGE 语言
  zh_CN 中文
  en_US 英文

UTF-8 字符集
# 字符集: 字母符号,文字在计算机中表示方法.
常用字符集:
  UTF-8 通用.
  GBK 国标

# 2 修改字符集
[root@Kylin-V10-sp3 ~]# localectl set-locale LANG=zh_CN.UTF-8
[root@Kylin-V10-sp3 ~]# 

# 3 脚本中临时修改语言为英文 ⭐ ⭐ ⭐ ⭐ ⭐
# 书写脚本的时候,统一改为英文,方便进行过滤与操作.
[root@Kylin-V10-sp3 ~]# export LANG=en_US.utf-8
[root@Kylin-V10-sp3 ~]# 

1.4 ctl系列小结

ctl命令大礼包 
hostnamectl    # 主机名,内核版本
systemctl      # 服务管理,开,关,重启,查看状态
timedatectl    # 时区.
localectl      # 修改语言,字符集. 一般用export LANG临时修改.
networkctl     # ubt修改网卡后配置  networkctl reload
点击查看代码
# 开启
  systemctl start xxx 
  systemctl enable xxx   # 开机自启动
  systemctl enalbe --now xxxx # 开机自启动立马生效

# 关闭
  systemctl stop xxx 
  systemctl disable xxx  # 关闭自启动
  systemctl disable --now xxx  # 关闭自启动立马生效

# 查看:
  systemctl status xxx

# 重启服务
  systemctl restart xxx

# 更加精简的查看(脚本用的多)
  systemctl is-active xxx    # 是否正在运行
  systemctl is-enabled xxx   # 是否开机自启动

# 查看系统所有服务 systemctl list-unit-files 或list-units 
[root@Kylin-V10-sp3 ~]# systemctl list-unit-files
UNIT FILE                                  STATE          
proc-sys-fs-binfmt_misc.automount          static         
-.mount                                    generated      
backup.mount                               generated      
boot.mount                                 generated      
dev-hugepages.mount                        static         
dev-mqueue.mount                           static                 
tmp.mount                                  disabled       
var-lib-nfs-rpc_pipefs.mount               static         
ostree-finalize-staged.path                disabled       
systemd-ask-password-console.path          static    

[root@Kylin-V10-sp3 ~]# systemctl list-units
  UNIT                                                                                LOAD   ACTIVE SUB       DESCRIPTION                                                 
  proc-sys-fs-binfmt_misc.automount                                                   loaded active waiting   Arbitrary Executable File Formats File System Automount Poin
  sys-devices-pci0000:00-0000:00:07.1-ata2-host1-target1:0:0-1:0:0:0-block-sr0.device loaded active plugged   VMware_Virtual_IDE_CDROM_Drive Kylin-Server-10              
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged   VMware_Virtual_S 1                                          
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda-sda2.device loaded active plugged   VMware_Virtual_S 2                                          
  sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda.device      loaded active plugged   VMware_Virtual_S                                            
  sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device                   loaded active plugged   82545EM Gigabit Ethernet Controller (Copper) (PRO/1000 MT Si
  sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device                 loaded active plugged   ES1371/ES1373 / Creative Labs CT2518 (Audio PCI 64V/128/5200
  sys-devices-platform-serial8250-tty-ttyS0.device                                    loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS0                  
  sys-devices-platform-serial8250-tty-ttyS1.device                                    loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS1                  
  sys-devices-platform-serial8250-tty-ttyS10.device                                   loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS10 

2 Linux运行级别

运行级别runlevel,Linux系统的不同的状态,分别为关机、重启、命令行/多人默认、图形化界面/桌面模式

点击查看代码
# 运行级别 说明
0  # 关机 poweroff
1  # 单用户/救援模式 secure,没有网络.
2  # 多用户模式,命令行模式,文本模式,multi-user
3  # 多用户模式,命令行模式,文本模式,multi-user
4  # 多用户模式,命令行模式,文本模式,multi-user
5  # 图形界面,X11,桌面模式,graphical,vnc连接或本地连接.
6  # 重启 reboot

# 查看运行级别
[root@Kylin-V10-sp3 ~]# systemctl get-default
multi-user.target
[root@Kylin-V10-sp3 ~]# 

[root@ubuntu2204 /etc/netplan]# systemctl get-default
graphical.target
[root@ubuntu2204 /etc/netplan]# 

# 修改运行级别  不一定生效,比如多用户模式改为图形界面,图形界面未安装
[root@ubuntu2204 /etc/netplan]# systemctl set-default multi-user.target 
Created symlink /etc/systemd/system/default.target → /lib/systemd/system/multi-user.target.
[root@ubuntu2204 /etc/netplan]# 

#了解下面命令即可. init配合runlevel
init 0
init 6

3 Linux启动流程

https://www.processon.com/view/link/617a1ee21e0853741504c0f4

Linux启动流程 说明
开机自检 检查硬件是否有问题.
加载引导程序 检查硬件是否有问题.
GRUB菜单 选择内核
救援模式
加载内核 /boot/目录,加载内核相关镜像
启动第1个进程systemd 启动进程
读取运行级别 读取运行级别,然后准备启动对应的服务
进行系统初始化 设置主机名,ip地址等等信息
启动服务 启动各种开机自启动的服务.
启动完成显示登录页面 运行login程序
posted @ 2024-09-04 20:27  老虎死了还有狼  阅读(21)  评论(0编辑  收藏  举报