linux 系统服务

1.系统启动流程

在这里插入图片描述

2.启动运行级别

2.1什么是运行级别

运行级别:是指操作系统正在运行的功能级别

system V init 运行级别systemd目标名称作用
0runlevel0.target,poweroff.target关机
1runlevel1.target,rescue.target单用户模式(修改密码)
2runlevel2.target,multi-user.target
3runlevel3.target,multi-user.target字符界面(最多的)
4runlevel4.target,multi-user.target
5runlevel5.target,graphical.target图形界面
6runlevel6.target,reboot.target重启

2.2 调整运行级别

  • systemd 使用 targets而不是 runlevels有两个
    主要目标:
    multi-user.target:类似于运行级别3
    graph ical.target: 类似于运行级别5
    1.查看系统默认运行级别
[root@localhost ~]# systemctl get-default 
multi-user.target

2、要设置默认目标,请运行

[root@localhost ~]# systemctl set-default TARGET.target
# TARGET.target 运行目标

3.systemd管理

3.1 systemd的由来

  • Linux一直以来都是采用init进程作为祖宗进程,但
    是init有两个缺点:
    1.系统启动时间长,init进程是串行启动,只有
    前一个进程启动完,才会启动下一个进程;
    2.启动脚本复杂,初始化系统后会加载很多脚本,脚本都会处理各自的情况,并且脚本多而杂;
    3.写脚本;
  • systemd 即为 system daemon 守护进程,systemd主要解决上文的问题而诞生
  • systemd 的目标是,为系统的启动和管理提供一套完整的解决方案;
    在这里插入图片描述
  • Centos5 启动速度慢,串行启动过程,无论进程相互之间有无依赖关系。
  • Centos6 启动速度有所改进,有依赖的进程之间依次启动而其他与之没有依赖关系的则并行同步启动。
  • Centos7 所有进程无论有无依赖关系则都是并行启动(当然很多时候进程没有真正启动而是只有一个信号或者说是标记而已,在真正利用的时候才会真正启动。)

3.2 systemd的优势

  • 1、最新系统都采用 systemd 管理 RedHat7、 CentOS7、Ubuntu15;
  • 2、Centos7支持开机并行启动服务,显著提高开机启动效率;
  • 3、Centos7关机只关闭正在运行的服务,而Centos6 全部都关闭一次;
  • 4、Centos7服务的启动与停止不在使用脚本进行管理,也就是 /etc/init.d 下不在有脚本;
  • 5、Centos7使用systemd解决原有模式缺陷,比如原有service不会关闭程序产生的子进程;

3.3 systemd相关命令

  • /usr/lib/systemd/system/:服务启停文件,通过systemctl命令对其文件启动、停止、重载等操作
命令含义
systemctl start crond启动服务
systemctl stop crond停止服务
systemctl restart crond重启服务
systemctl reload crond重载服务
systemctl enable crond服务设定为开机运行
systemctl disabled crond服务设定为开机不运行
systemctl daemon-reload crond创建服务启动文件需要重载配置
systemctl list-unit-files查看各个级别下服务的启动与禁用
systemctl is-enabled crond.service查看特定服务是否为开机自启动
systemctl is-active crond查看服务是否在运行

3.4 systemd管理Nginx

1、编译nginx

[root@localhost nginx-1.21.1]# wget http://nginx.org/download/nginx- 1.21.1.tar.gz
[root@localhost nginx-1.21.1]# tar xf nginx-1.21.1.tar.gz 
[root@localhost nginx-1.21.1]# ./configure --prefix=/usr/local/nginx-1.21.1 --with-http_ssl_module --with-http_stub_status_module --without-http
[root@localhost nginx-1.21.1]# make && make install
[root@localhost nginx-1.21.1]# ln -s /usr/local/nginx-1.21.1/ /usr/local/nginx

2、常规启动nginx方式

4.救援模式

4.1 场景1-忘记超级管理员密码

  • 如何使用单用户模式进行变更系统密码?以Centos7系统为例:
    。第1步:重启 Linux 系统主机并出现引导界面时,按下键盘上的 e 键进入内核编辑界面
    。第2步:在linux16这行的后面添加enforcing=0 init=/bin/bash,然后按Ctrl + x进入
    。第3步:进入到系统的单用户模式,依次输入以下命令,重启操作系统,使用新密码登录

1、mount -o rw,remount /默认/分区只读,
重新挂载为读写模式
2、echo “123” | passwd --stdin root:非
交互式修改密码
3、exec /sbin/init:重新引导系统

4.2 场景2-系统损坏需备份数据

  • 当系统坏了,无法登陆系统,但需要把里面的数据复制出来,怎么办?
    第一步:挂载 ISO 镜像文件,修改BIOS,调整
    DVD光盘为第一引导;
    第二步:选择 Troubleshooting,继续选择
    Rescue a CentOS system 救援模式;
    第三步:输入1,然后执行命令 chroot /mnt/sysimage,挂载真实系统;
    第四步:备份系统中的数据文件至其他磁盘;

4.3 场景3-误删除grub文件修复Centos误删除grub文件如何进行修复

第一步:模拟误删除故障 rm -rf /boot/grub2 && reboot
第二步:系统无法正常启动起来(提示grub找不
到)
第三步:然后按照之前的操作进入救援模式,执行
chroot /mnt/sysimage挂载真实的操作系统;
第四步:使用grub2相关命令修复
grub2-install /dev/sda 重新添加mbr引导
grub2-mkconfig -o /boot/grub2/grub.cfg
重新生成配置
输入 exit
再 reboot
ls /boot/grub2/grub.cfg
在这里插入图片描述
在这里插入图片描述重启后
在这里插入图片描述

5.系统优化

  • cpu:
    。1、虚拟化支持
    。2、cpu超线程
  • 磁盘
    。1、SAS
    。2、SSD

5.1 调整yum源

  • 默认安装系统对外提供的yum仓库为国外站点,可以将站点修改为国内,加速软件包下载
# base 
[root@oldxu ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos- 7.repo 
# epel 
[root@oldxu ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 
# mysql 、 zabbix、elk软件包

5.2 关闭防火墙

  • 默认情况下还会采用关闭防火墙,以避免影响后期服务的使用;
    。云主机:有硬件提供的安全组产品来提供防护;
    。物理机:一般公司在入口层面都会有硬件防火墙;
# firewalld 
[root@oldxu ~]# systemctl stop firewalld
[root@oldxu ~]# systemctl disabled firewalld 
# selinux 
[root@oldxu ~]# setenforce 0 [root@oldxu ~]# sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config

5.3 ulimit资源限制

  • ulimit命令可以对系统资源进行控制
    。-u:限制普通用户所能打开的最大进程数目;(每个用户 )
    。-n:限制用户能分配到的文件描述符数量 ;

5.3.1 限制进程最大数量

1、限制每个用户最大能打开3个进程

[root@localhost ~]# ulimit -u 500
#临时生效,当前窗口切换用户

5.3.2 限制打开文件数量

1、限制进程最多打开文件描述符为10

[root@localhost ~]# ulimit -n 10
#临时生效,当前窗口切换用户

2、编写python程序模拟打开多个文件

#!/usr/bin/env python 
import time,os
from threading import Thread

print(os.getpid())

def new_file(n):
    with open('%s.file' %n,mode='wt') as f1:
                time.sleep(2000)

if __name__ == "__main__":
        count=1
        while True:
                Thread(target=new_file,args= (count,)).start()
        count+=1

3、等待了一段时间,当程序打开文件超过限制,则会提示
异常;

Exception in thread Thread-16128:
Traceback (most recent call last):
  File "/usr/lib64/python2.7/threading.py", line 812, in __bootstrap_inner
  File "/usr/lib64/python2.7/threading.py", line 765, in run
  File "o.py", line 8, in new_file
IOError: [Errno 24] Too many open files: '1.file'

5.3.3 调整ulimit限制参数

  • 通过ulimit方式调整打开的文件数量,以及进程数量,都是临时操作,所以需要进行永久配置
    。配置文件:/etc/security/limits.conf
    。调整模式:
    soft:软限制,超过则提示;
    hard:硬限制,超过则停止;
[root@localhost ~]# tail /etc/security/limits.conf 
#ftp             hard    nproc           0
#@student        -       maxlogins       4

# End of file
# max user processes 
* soft nproc 60000 
* hard nproc 60000 
# open files # 
* soft nofile 100000 
#2^16-1(2的16次方剪1)
* hard nofile 100000
#2^16-1(2的16次方剪1)
# 系统级,添加如下字段(调整内核才可以生效)
[root@localhost ~]# tail -10 /etc/sysctl.conf 
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
fs.file-max = 100000

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值