1,基本介绍

1,在Linux中,每个执行的进程(代码)都称为一个进程。每一个进程都分配一个ID号。
2,每一个进程,都会对应一个父进程,而这个父进程可以复制多个子进程,
3,第一个进程都可能以两种方式存在,前台和后台
4,一般系统的服务都是以后台进程方式存在,而且都会常驻系统中。

2,ps 显示系统的执行过程

ps 查看进程指令, 一般来说使用的参数是 ps -aux
image.png
ps -a 显示当前终端的所有进程信息
ps -u 以用户的格式显示进程信息
ps -x 显示后台进程运行的参数
image.png

3,ps 指令详解

1,指令说明

VSZ :进程占用的虚拟内存大小
RSS :进程战胜的物理内存大小
STAT :进程状态, S:睡眠,s:进程是会话的先导进程,N:进程拥有比普通优先级更低的优先级,R:正在运行,D:短期等待,Z:僵尸进程,T:被跟踪或者被停止进程
TIME :CPU时间,进程使用CPU的总时间

2,ps -aux|grep xxx

3,ps -ef 合格式显示所有进程

-e 显示所有进程 -f 合格式

4,kill killall 终止进程

kill [选项] 进程号
killlall [选项] 进程名称
-9 强迫进程立即停止

5,pstree 查看进程树

pstree [选项] 可以更直观的查看进程信息
-p 显示进的PID
-u 显示进程的所属用户

4,Service 服务管理

1,介绍

服务本质就是进程,但是是运行在后台的。通常会监听某个端口,等待其它程序的请求,如(mysql, sshd, 防火墙等),因此我们又称为守护进程
image.png

2,service 管理指令

service 服务名 [start|stop|restart|status], 在CentOS7中不再使用service 而是systemctl

3,查看服务名

/etc/init.d/服务名称
ls -l /etc/init.d
image.png

4,服务的运行级别

查看/修改:vi /etc/inittab
image.png

5,开机的流程

image.png

6,chkconfig 指令

通过 chkconfig命令可以给每个服务的各个运行级别设置自动启动/关闭
重新设置服务自启或关闭,需要重启机器 reboot 才能生效
1,chkconfig —list|grep xxx
image.png
image.png
2,chkconfig 服务名 —list
image.png
3,chkconfig —level 5 服务名 on/off
将sshd 服务在运行级别为 5 的情况下,不要自启动
image.png

7,应用实例

image.png

5,top 动态监控进程

1,介绍

top 和 ps 命令很相似,都是用来显示正在执行的进程。不同之处 top 在执行一段时间后可以更新正在运行的进程

2,语法

top [进程]
-d 指定top命令在几秒后更新,默认是3秒, 在top的交互模式中可以执行的命令
-i 使top不显示任何闲置或者僵死进程
-p 通过指定监控进程ID来仅仅监控某个进程的状态
交互说明
p 以CPU使用率排序,默认就是些项
M 以内存的使用率排序
N 以PID排序
q 退出top

6, netstat 查看网络情况

netstat [选项]
netstat -anp
-an 安一定顺序排列输出
-p 显示哪个进程在调用
eg : netstat -anp | more →查看所有网络服务
eg:netstat -anp | grep sshd →查看服务为sshd的服务信息

7,防火墙

1、查看firewall服务状态

systemctl status firewalld
7,进程/服务/防火墙管理 - 图12

2、查看firewall的状态

firewall-cmd —state
7,进程/服务/防火墙管理 - 图13

3、开启、重启、关闭、firewalld.service服务

开启
service firewalld start
# 重启
service firewalld restart
# 关闭
service firewalld stop

4、查看防火墙规则

firewall-cmd —list-all
image.png

5、查询、开放、关闭端口

查询端口是否开放
firewall-cmd —query-port=8080/tcp
# 开放80端口
firewall-cmd —permanent —add-port=80/tcp
# 移除端口
firewall-cmd —permanent —remove-port=8080/tcp

重启防火墙(修改配置后要重启防火墙)
systemctl restart firewalld.service;
重新载入配置
firewall-cmd —reload
# 参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、—permanent:表示设置为持久;
3、—add-port:标识添加的端口;

八,Centos6防火墙

  1. 查看防火墙 service iptables status
  2. 关闭防火墙 service iptables stop
  3. 开启防火墙 service iptables start

永久关闭防火墙

  1. 查询开启iptables是否启动
  2. [root@CactiEZ ~]# chkconfig --list | grep iptables
  3. iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  4. 由此可见:2/3/4/5都是开机启动,数字代表runlevel等级
  5. [root@CactiEZ ~]# chkconfig iptables off
  6. [root@CactiEZ ~]# chkconfig --list | grep iptables
  7. iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off

永久开启防火墙

  1. [root@CactiEZ ~]# chkconfig iptables on
  2. [root@CactiEZ ~]# chkconfig --list | grep iptables
  3. iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off

九,Centos7防火墙

  1. 查看防火墙规则 firewall-cmd --list-all
  2. 防火墙服务状态 systemctl status firewalld.service
  3. 防火墙状态 firewall-cmd --state
  4. 开启防火墙 systemctl start firewalld.service
  5. 重启防火墙 systemctl restart firewalld.service
  6. 关闭防火墙 systemctl stop firewald.service
  7. 永久关闭防火墙 systemctl disable firewalld
  8. 永久关闭防火墙 systemctl disable firewalld.service
  9. 永久开启防火墙 systemctl enable firewalld.service
  10. 开放80端口 firewall-cmd --permanent --add-port=80/tcp
  11. 移除80端口 firewall-cmd --permanent --remove-port=80/tcp
  12. 重启防火墙 systemctl restart firewalld.service
  13. 重新载入配置 firewall-cmd --reload