Linux-服务管理

Linux-服务管理

起男 350 2022-09-21

Linux-服务管理

计算机中,一个正在执行的程序或命令,被叫做“进程”(process)

启动之后一直存在,常驻内存的进程,一般被叫做”服务“(service)

基本语法

centos6

service 服务名 start|stop|restart|status
  • 服务名就是etc/init.d/目录下的可执行文件名(SysV服务)

centos7(兼容6)

systemctl start|stop|restart|status 服务名
  • 服务位于/usr/lib/systemd/system/中(systemd服务)

系统运行级别

centos6

有7种运行级别:常用的是3和5

  • 0:系统停机状态,系统默认运行级别不能设置为0,否则不能正常启动
  • 1:单用户工作状态,root权限,用于系统维护,禁止远程登录
  • 2:多用户状态(没有nfs),不支持网络
  • 3:完全的多用户状态(有nfs),登录后进入控制台命令模式
  • 4:系统未使用,保留
  • 5:x11控制台,登陆后进入图形gui模式
  • 6:系统正常关闭并重启,默认允许状态不能设置未6

查看默认级别:vi /etc/inittab

设置运行级别:init 级别

centos7

简化为:

  • multi-user.target:等价于原运行级别3(多用户有网,无图形界面)
  • graphical.target:等价于原运行级别5(多用户有网,有图形界面)

查看当前运行级别:systemctl get-default

设置运行级别:systemctl set-default 级别

开机自启动

centos6

查看启动状态

chkconfig --list  这种方式只能查看SysV服务

开启或关闭

chkconfig 服务名 off|on

针对级别的开关

chkconfig --level 级别 服务名 off|on

centos7

查看启动状态

systemctl list-unit-files
  • enabled:开启状态
  • disabled:关闭状态
  • static:需要其他服务启动才能启动

查看服务状态

systemctl status 服务名

开启和关闭

systemctl enable|disable 服务名

配置防火墙

contos7使用firewalld,contos6中的是iptables

查看防火墙状态

systemctl status firewalld

关闭防火墙

systemctl status firewalld

开启防火墙

systemctl start firewalld

关闭开机自启动

systemctl disable firewalld.service

开启开机自启动

systemctl enable firewalld.service

关键重启

  1. 关机
shutdown [选项][时间]
  • 默认:等待一段时间(默认1分钟)后关机
  • -c:停止关机
  • 数字:指定多少分钟后关机
  • 时间:xx:xx形式,在指定时间关机,now就是立刻
  • -r :重启,和reboot相同
  • -H:停机,和halt相同
  • -h或-P:关机,和poweroff相同
  1. 停机,关闭系统,但不断电
halt
  1. 关机断电
poweroff
  1. 重启
reboot

关机前系统会执行sync命令,将数据由内存同步到硬盘中