AI智能
改变未来

linux系统服务

文章目录

  • 开机启动流程
  • 系统的运行级别
  • systemd介绍
  • systemctl 系统服务管理器指令
  • 如何设置主机名
  • 如何修改字符集
  • export 设置环境变量
  • journalctl 系统日志管理
  • 单用户模式
  • 救援模式
  • 网卡命名规则
  • 开机启动流程

    1. 按下电源2. BIOS自检3. MBR引导4. 进入GRUB菜单5. 加载内核 Kernel6. systemd      init进程7. 读取运行级别[root@zzc ~]# ll  /etc/systemd/system/default.targetlrwxrwxrwx 1 root root 37 Jul  6 02:16 /etc/systemd/system/default.target -> /lib/systemd/system/multi-user.target[root@zzc ~]# ll  /usr/lib/systemd/system/runlevel*.targetlrwxrwxrwx. 1 root root 15 Jul  6 02:14 /usr/lib/systemd/system/runlevel0.target -> poweroff.targetlrwxrwxrwx. 1 root root 13 Jul  6 02:14 /usr/lib/systemd/system/runlevel1.target -> rescue.targetlrwxrwxrwx. 1 root root 17 Jul  6 02:14 /usr/lib/systemd/system/runlevel2.target -> multi-user.targetlrwxrwxrwx. 1 root root 17 Jul  6 02:14 /usr/lib/systemd/system/runlevel3.target -> multi-user.targetlrwxrwxrwx. 1 root root 17 Jul  6 02:14 /usr/lib/systemd/system/runlevel4.target -> multi-user.targetlrwxrwxrwx. 1 root root 16 Jul  6 02:14 /usr/lib/systemd/system/runlevel5.target -> graphical.targetlrwxrwxrwx. 1 root root 13 Jul  6 02:14 /usr/lib/systemd/system/runlevel6.target -> reboot.target8. 初始化系统[root@zzc ~]# ll  /usr/lib/systemd/system/sysinit.target-rw-r--r--. 1 root root 518 Oct 31  2018 /usr/lib/systemd/system/sysinit.target9. 并行启动开机自启动的服务         串行启动10. 运行getty文件,显示登陆界面[root@zzc ~]# ll  /usr/lib/systemd/system/getty.target-rw-r--r--. 1 root root 460 Oct 31  2018 /usr/lib/systemd/system/getty.targetCentos-6                     Centos-7的区别init管理进程		             systemd管理进程串行启动服务			         并行启动服务启动效率慢,容易故障	         启动效率高,不容易出现故障

    系统的运行级别

    1.什么是运行级别,运行级别就是操作系统当前正在运行的功能级别

    System V init运行级别 systemd目标名称 作用
    0 runlevel0.target, poweroff.target 关机
    1 runlevel1.target, rescue.target 单用户模式
    2 runlevel2.target, multi-user.target 多用户的文本界面
    3 runlevel3.target, multi-user.target 多用户的文本界面
    4 runlevel4.target, multi-user.target 多用户的文本界面
    5 runlevel5.target, graphical.target 多用户的图形界面
    6 runlevel6.target, reboot.target 重启
    [root@zzc ~]# ll  /usr/lib/systemd/system/runlevel*.targetlrwxrwxrwx. 1 root root 15 Jul  6 02:14 /usr/lib/systemd/system/runlevel0.target -> poweroff.targetlrwxrwxrwx. 1 root root 13 Jul  6 02:14 /usr/lib/systemd/system/runlevel1.target -> rescue.targetlrwxrwxrwx. 1 root root 17 Jul  6 02:14 /usr/lib/systemd/system/runlevel2.target -> multi-user.targetlrwxrwxrwx. 1 root root 17 Jul  6 02:14 /usr/lib/systemd/system/runlevel3.target -> multi-user.targetlrwxrwxrwx. 1 root root 17 Jul  6 02:14 /usr/lib/systemd/system/runlevel4.target -> multi-user.targetlrwxrwxrwx. 1 root root 16 Jul  6 02:14 /usr/lib/systemd/system/runlevel5.target -> graphical.targetlrwxrwxrwx. 1 root root 13 Jul  6 02:14 /usr/lib/systemd/system/runlevel6.target -> reboot.target

    2.查看系统运行级别

    Centos-6   #查看系统默认运行级别[root@zzc ~]# runlevelN 3#设置运行级别[root@zzc ~]# init  5[root@zzc ~]# runlevel3 5[root@zzc ~]# init  3Centos-7   #查看系统默认运行级别[root@zzc ~]# systemctl  get-defaultmulti-user.target#修改运行级别[root@zzc ~]# systemctl  set-default   graphical.targetRemoved symlink /etc/systemd/system/default.target.Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.[root@zzc ~]# systemctl  get-defaultgraphical.target[root@zzc ~]# systemctl  set-default   multi-user.targetRemoved symlink /etc/systemd/system/default.target.Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.[root@zzc ~]# systemctl  get-defaultmulti-user.target

    systemd介绍

    systemd	 为system   daemon守护进程#主配置文件目录[root@zzc ~]# ll  /etc/systemd/system/  -ddrwxr-xr-x. 12 root root 4096 Aug  5 09:05 /etc/systemd/system/#系统服务配置文件[root@zzc ~]# ll /usr/lib/systemd/system/  -ddrwxr-xr-x. 26 root root 12288 Jul 30 19:51 /usr/lib/systemd/system/#程序,进程运行时产生的一些文件[root@zzc ~]# ll  /run/systemd/system/  -ddrwxr-xr-x 5 root root 160 Aug  5 09:30 /run/systemd/system/

    systemctl 系统服务管理器指令

    #启动[root@zzc ~]# systemctl   start   nginx#停止[root@zzc ~]# systemctl   stop   nginx#状态[root@zzc ~]# systemctl   status   nginx#重启[root@zzc ~]# systemctl  restart   nginx#平滑重启[root@zzc ~]# systemctl  reload   nginx#禁用某个服务[root@zzc ~]# systemctl  mask   nginx#解除禁用[root@zzc ~]# systemctl  unmask   nginx#开机自启动[root@zzc ~]# systemctl  enable   nginx#取消开机自启动[root@zzc ~]# systemctl  disable  nginxstatus   #状态active (running)		#运行Active: inactive (dead)	#不在运行disabled				#开机不自启enabled					#开机自启failed					#失败#显示当前运行级别下开机自启动的服务[root@zzc ~]# systemctl   list-unit-filesCentOS7系统, 管理员可以使用 systemctl 命令来管理服务器启动与停止#关机相关命令systemctl poweroff      #立即关机,常用#重启相关命令systemctl reboot        #重启命令,常用

    如何设置主机名

    [root@zzc ~]# hostnamezzc[root@zzc ~]# hostnamectlStatic hostname: zzcIcon name: computer-vmChassis: vmMachine ID: 6b11e94f1fe04eb89579c1c41f6f5307Boot ID: 783be43c5a44420ead4bd26792a6d22cVirtualization: vmwareOperating System: CentOS Linux 7 (Core)CPE OS Name: cpe:/o:centos:centos:7Kernel: Linux 3.10.0-957.el7.x86_64Architecture: x86-64[root@zzc ~]# hostnamectl   set-hostname   web01[root@zzc ~]# logoutConnection closing...Socket close.Connection closed by foreign host.Disconnected from remote host(虚拟机-10.0.0.100) at 09:54:12.Type `help\' to learn how to use Xshell prompt.[C:\\~]$Connecting to 10.0.0.100:22...Connection established.To escape to local shell, press \'Ctrl+Alt+]\'.Last login: Wed Aug  5 17:51:59 2020 from 10.0.0.1[root@web01 ~]#[root@web01 ~]# cat  /etc/hostnameweb01Centos-6修改  临时生效[root@web01 ~]# hostname  zzc[root@web01 ~]# logoutConnection closing...Socket close.Connection closed by foreign host.Disconnected from remote host(虚拟机-10.0.0.100) at 09:55:10.Type `help\' to learn how to use Xshell prompt.[C:\\~]$Connecting to 10.0.0.100:22...Connection established.To escape to local shell, press \'Ctrl+Alt+]\'.Last login: Wed Aug  5 09:54:16 2020 from 10.0.0.1[root@zzc ~]# cat /etc/hostnameweb01

    如何修改字符集

    [root@zzc ~]# echo $LANGen_US.UTF-8[root@zzc ~]# localectl   set-locale   LANG=zh_CN.UTF-8[root@zzc ~]# logoutConnection closing...Socket close.Connection closed by foreign host.Disconnected from remote host(虚拟机-10.0.0.100) at 09:57:21.Type `help\' to learn how to use Xshell prompt.[C:\\~]$Connecting to 10.0.0.100:22...Connection established.To escape to local shell, press \'Ctrl+Alt+]\'.Last login: Wed Aug  5 09:55:13 2020 from 10.0.0.1[root@zzc ~]#[root@zzc ~]# cat  /etc/locale.confLANG=zh_CN.UTF-8[root@zzc ~]# yum  install  -y  sl已加载插件:fastestmirrorLoading mirror speeds from cached hostfile* base: mirrors.aliyun.com* extras: mirrors.aliyun.com* updates: mirrors.aliyun.com软件包 sl-5.02-1.el7.x86_64 已安装并且是最新版本无须任何处理

    export 设置环境变量

    #临时修改变量[root@zzc ~]# export   LANG=en_US.UTF-8[root@zzc ~]# echo  $LANGen_US.UTF-8

    journalctl 系统日志管理

    journalctl工具是CentOS-7才有的工具Systemd 统一管理所有 Unit 的启动日志。可以只用journalctl一个命令,查看所有日志(内核日志和应用日志)。日志的配置文件/etc/systemd/journald.conf选项:-xe     #查看当前服务的日志-n      #显示尾部10行日志内容-n num  #显示尾部指定多少行内容-f      #实时查看日志输出-p      #显示日志的级别-u      #只查看某个服务的日志[root@zzc ~]# journalctl  -xe			#查看当前服务的日志[root@zzc ~]# tail  /var/log/messagesAug  5 10:01:32 zzc systemd: Stopped The nginx HTTP and reverse proxy server.Aug  5 10:01:32 zzc systemd: Starting The nginx HTTP and reverse proxy server...Aug  5 10:01:32 zzc nginx: nginx: [emerg] unknown directive \"isten\" in /etc/nginx/nginx.conf:39Aug  5 10:01:32 zzc nginx: nginx: configuration file /etc/nginx/nginx.conf test failedAug  5 10:01:32 zzc systemd: nginx.service: control process exited, code=exited status=1Aug  5 10:01:32 zzc systemd: Failed to start The nginx HTTP and reverse proxy server.Aug  5 10:01:32 zzc systemd: Unit nginx.service entered failed state.Aug  5 10:01:32 zzc systemd: nginx.service failed.Aug  5 10:02:01 zzc systemd: Started Session 19 of user root.Aug  5 10:03:01 zzc systemd: Started Session 20 of user root.#查看日志的最后20行内容[root@zzc ~]# journalctl   -n 20#实时查看日志的输出[root@zzc ~]# journalctl   -f#显示日志的级别日志[root@zzc ~]# journalctl   -palert    crit     debug    emerg    err      info     notice   warning[root@zzc ~]# journalctl   -p  err-- Logs begin at Wed 2020-08-05 17:51:37 CST, end at Wed 2020-08-05 10:05:01 CST. --Aug 05 17:51:41 zzc kernel: sd 2:0:0:0: [sda] Assuming drive cache: write throughAug 05 17:51:41 zzc kernel: sd 2:0:1:0: [sdb] Assuming drive cache: write throughAug 05 17:51:41 zzc kernel: sd 2:0:2:0: [sdc] Assuming drive cache: write throughAug 05 17:51:44 zzc kernel: piix4_smbus 0000:00:07.3: SMBus Host Controller not enabled!Aug 05 09:54:13 web01 sshd[7162]: error: Received disconnect from 10.0.0.1 port 50011:0:Aug 05 09:55:11 zzc sshd[7333]: error: Received disconnect from 10.0.0.1 port 50033:0:Aug 05 09:57:22 zzc sshd[7369]: error: Received disconnect from 10.0.0.1 port 50041:0:Aug 05 09:59:29 zzc sshd[7419]: error: Received disconnect from 10.0.0.1 port 50067:0:Aug 05 10:00:51 zzc sshd[7473]: error: Received disconnect from 10.0.0.1 port 50097:0:Aug 05 10:01:32 zzc systemd[1]: Failed to start The nginx HTTP and reverse proxy server.#只查看某个服务的日志[root@zzc ~]# journalctl   -u  sshd-- Logs begin at Wed 2020-08-05 17:51:37 CST, end at Wed 2020-08-05 10:06:01 CST. --Aug 05 17:51:56 zzc systemd[1]: Starting OpenSSH server daemon...Aug 05 17:51:56 zzc sshd[6981]: Server listening on 0.0.0.0 port 22.Aug 05 17:51:56 zzc sshd[6981]: Server listening on :: port 22.Aug 05 17:51:56 zzc systemd[1]: Started OpenSSH server daemon.Aug 05 17:51:59 zzc sshd[7162]: Accepted password for root from 10.0.0.1 port 50011 ssh2Aug 05 09:54:16 web01 sshd[7333]: Accepted password for root from 10.0.0.1 port 50033 ssh2Aug 05 09:55:12 zzc sshd[7369]: Accepted password for root from 10.0.0.1 port 50041 ssh2Aug 05 09:57:26 zzc sshd[7419]: Accepted password for root from 10.0.0.1 port 50067 ssh2Aug 05 09:59:31 zzc sshd[7473]: Accepted password for root from 10.0.0.1 port 50097 ssh2Aug 05 10:00:52 zzc sshd[7525]: Accepted password for root from 10.0.0.1 port 50111 ssh2

    单用户模式

    如何使用单用户模式进行变更系统密码?以Centos7系统为例;(Centos6破解方式请自行百度)

    1. 重启Linux系统主机并出现引导界面时,按下键盘上的e键进入内核编辑界面

      2.在linux16这行的最后面添加init=/bin/bash enforcing=0 ,然后按下Ctrl + X组合键来运行修改过的内核程序

      3.进入到系统的单用户模式,依次输入以下命令,等待系统重启操作完毕,然后就可以使用新密码来登录Linux系统了

      运行级别被设置为重启情况下进入单用户模式
    [root@web01 ~]# systemctl   set-default   reboot.targetRemoved symlink /etc/systemd/system/default.target.Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/reboot.target.[root@web01 ~]# systemctl  get-defaultreboot.target开机在grub界面输入 e在linux16开头的行的最后面输入  rd.break按 ctrl键  +  x键重新挂载根分区   mount  -o  rw,remount   /sysroot赋予权限     chroot   /sysroot修改运行级别exitreboot

    救援模式

    场景一:修复MBR,主要出现在安装双系统时,后安装的系统把原来系统的MBR删除了,需要修复

    1.先挂载光盘,然后选择光盘引导为第一位。F10保存退出
    F10保存退出

    2.进入故障排除模式–>然后选择救援模式


    3.然后分配一个shell终端,挂载真实的操作系统进行修复

    4.修复MBR引导,然后重启连接服务器

    #修复MBRgrub2-install   /dev/sda#然后退出# exit#最后重启进入系统# reboot


    场景二:Centos7误删除grub文件如何进行修复
    1.模拟误删故障

    1.删除grub2[root@m01 ~]# rm -rf  /boot/grub22.重启计算机[root@m01 ~]# reboot

    2.重启系统,然后按照之前的操作进入救援模式,然后分配一个shell终端,挂载真实的操作系统进行修复

    3.使用grub2-install、grub2-mkconfig恢复配置文件

    #修复MBRgrub2-install   /dev/sdagrub2-mkconfig   -o   /boot/grub2/grub.cfg


    4.最后修改 BIOS 引导,让硬盘回归到第一引导

    网卡命名规则

    biosdevname=0net.ifnames=1    设置为0规则1:如果Firmware或者BIOS提供的设备索引信息可用就用此命名。比如eno1。否则使用规则2规则2:如果Firmware或Bios的PCI-E扩展插槽可用就用此命名。比如ens1,否则使用规则3规则3:如果硬件接口的位置信息可用就用此命名。比如enp2s0规则4:根据MAC地址命名,比如enx7d3e9f。默认不开启。规则5:上述均不可用时回归传统命名方式上面的所有命名规则需要依赖于一个安装包:biosdevname#命令行修改网卡命名规则mv /etc/sysconfig/network-scripts/ifcfg-ens33   /etc/sysconfig/network-scripts/ifcfg-eth0NAME=ens33DEVICE=ens33修改NAME=eth0DEVICE=eth0#重新生成gurb.cfg文件grub2-mkconfig   -o  /boot/grub2/grub.cfgreboot
    赞(0) 打赏
    未经允许不得转载:爱站程序员基地 » linux系统服务