180天Linux小白到大神-Linux系统服务

14.Linux系统服务

14.Linux系统服务

1.系统启动流程

1.1 CentOS6

1.2 CentOS7

2.启动运行级别

2.1 什么是运行级别

2.2 调整运行级别

3.Systemd管理

3.1 systemd的由来

3.2 systemd的优势

3.3 systemd相关命令

3.4 systemd管理Nginx

4.救援模式

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

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

4.3 场景3-误删除grub文件修复

5.系统优化

5.1 调整yum源

5.2 关闭防火墙

5.3 ulimit资源限制

5.3.1 限制进程最大数量

5.3.2 限制打开文件数量

5.3.3 调整ulimit限制参数

1.系统启动流程

1.1 CentOS6

centos6 开机启动流程,传送门

1.2 CentOS7

2.启动运行级别

2.1 什么是运行级别

运行级别:指操作系统当前正在运行的功能级别;

System V systemd目标名称 作用
init运行级别

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

2.2 调整运行级别

systemd 使用 targets 而不是 runlevels 有两个

主要目标:

multi-user.target :类似于运行级别3 graphical.target : 类似于运行级别5

1.查看系统默认运行级别

[root@student ~]# systemctl get-default

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

[root@student ~]# systemctl set-default TARGET.target

3.Systemd管理

3.1 systemd的由来

Linux 一直以来都是采用init 进程作为祖宗进程,但是init 有两个缺点:

1.系统启动时间长,init 进程是串行启动,只有

前一个进程启动完,才会启动下一个进程;

2.启动脚本复杂,初始化系统后会加载很多脚本,脚本都会处理各自的情况,并且脚本多而杂; 写脚本;

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

wget http://nginx.org/download/nginx-1.21.1.tar.gz

tar xf nginx-1.21.1.tar.gz cd nginx-1.21.1

./configure --prefix=/usr/local/nginx-1.21.1 \

–with-http_ssl_module \

–with-http_stub_status_module make && make install

ln -s /usr/local/nginx-1.21.1 /usr/local/nginx

2.常规启动nginx 方式

/usr/local/nginx/sbin/nginx # 启

/usr/local/nginx/sbin/nginx -s reload # 重

/usr/local/nginx/sbin/nginx -s stop # 关

  1. systemd 管理nginx

Before、After:定义启动顺序。

Before=xxx.service 代表本服务在xxx.service启动之前启动

After=xxx.service 代表本服务在xxx.service之后启动

Wants=xxx.service 代表该服务启动了,它依赖的服务也会被启动;依赖的服务如果被停止,不影响本服

vim /usr/lib/systemd/system/nginx.service [Unit]

Description=nginx

After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target

[Service]

Type=forking

#Environment=“PATH=$PATH:/usr/local/nginx/s bin”

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s

reload

ExecStop=/usr/local/nginx/sbin/nginx -s

stop

PrivateTmp=true

[Install]

WantedBy=multi-user.target

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

重新生成配置

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@oldxu ~]# ulimit -u 3

2.登陆用户启动多个进程测试

[root@oldxu ~]# su - oldxu [oldxu01@oldxu ~]$ sleep 1000 &

[1] 10857

[oldxu01@oldxu ~]$ sleep 1000 &

[2] 10858

[oldxu01@oldxu ~]$ sleep 1000 & -bash: fork: retry: 资源暂时不可用 -bash: fork: retry: 资源暂时不可用 -bash: fork: retry: 资源暂时不可用

5.3.2 限制打开文件数量

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

[root@oldxu ~]# ulimit -n 10

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

[root@oldxu ~]# cat open_file.py #!/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

time.sleep(2)

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

Exception in thread Thread-9:

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 “open_file.py”, line 9, in new_file IOError: [Errno 24] Too many open files: ‘9.file’

5.3.3 调整ulimit限制参数

通过ulimit 方式调整打开的文件数量,以及进程数量,都是临时操作,所以需要进行永久配置

配置文件:/etc/security/limits.conf

调整模式:

soft :软限制,超过则提示; hard :硬限制,超过则停止;

[root@oldxu ~]# tail /etc/security/limits.conf

max user processes * soft nproc 60000 * hard nproc 60000

open files

  • soft nofile 100000 ( 2^16-1 )
  • hard nofile 100000 ( 2^16-1 = 65535 )

系统级,添加如下字段(调整内核才可以生效)

[root@node ~]# tail /etc/sysctl.conf fs.file-max = 100000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值