初识Linux:服务
跳到导航
跳到搜索
服务
已安装服务的启停操作:
# 查看xxx服务状态
service xxx status
# 启动xxx服务
service xxx start
# 停止xxx服务
service xxx stop
# 重启xxx服务
service xxx restart
- “service xxx start”等效于
- /etc/init.d/xxx start
- /etc/rd.d/init.d/xxx start
- systemctl start xxx.service
service 与 systemctl
service
service命令其实是去/etc/init.d目录下,去执行相关程序。长期以来 Linux 的启动一直采用init进程,这种方法有两个缺点:
- 启动时间长。init进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
- 启动脚本复杂。init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。
systemctl
systemctl是 Systemd 的主命令,用于管理系统。
Systemd 是 Linux 系统工具,用来启动守护进程(Systemd 中的 d,即守护进程 daemon),能够快速并发地启动进程。
Systemd 并不是一个命令,而是一组命令,涉及到系统管理的方方面面。(参见阮一峰《Systemd 入门教程:命令篇》)
- 使用了 Systemd,就不需要再用init了。Systemd 取代了initd,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。
- systemctl 兼容了 service、chkconfig命令,如:
# 管理服务的启动、重启、停止、重载、查看 service foo start systemctl start foo.service 启动服务 service foo restart systemctl restart foo.service 重启服务 service foo stop systemctl stop foo.service 停止服务 service foo reload systemctl reload foo.service 重新加载配置文件(不终止服务) service foo status systemctl status foo.service 查看服务状态 # 设置服务开机启动、不启动、查看各级别下服务启动状态 chkconfig foo on systemctl enable foo.service 开机自动启动 chkconfig foo off systemctl disable foo.service 开机不自动启动 chkconfig foo systemctl is-enabled foo.service 查看特定服务是否为开机自动启动 chkconfig --list systemctl list-unit-files --type=service 查看各个级别下服务的启动与禁用情况
防火墙
Linux中有两种防火墙软件,ConterOS7.0以上使用的是firewall,ConterOS7.0以下使用的是iptables:
firewall
操作 | 命令 |
---|---|
开启防火墙: | systemctl start firewalld |
关闭防火墙: | systemctl stop firewalld |
查看防火墙状态: | systemctl status firewalld |
设置开机启动: | systemctl enable firewalld |
禁用开机启动: | systemctl disable firewalld |
重启防火墙: | firewall-cmd --reload |
开放端口(修改后需要重启防火墙方可生效): | firewall-cmd --zone=public --add-port=8080/tcp --permanent |
查看开放的端口: | firewall-cmd --list-ports |
关闭端口: | firewall-cmd --zone=public --remove-port=8080/tcp --permanent |
iptables
安装
CenterOS7.0以上版本并没有预装Iptables,需要自行装。
- 安装前先关闭firewall防火墙
操作 | 命令 |
---|---|
安装iptables | yum install iptables |
安装iptables-services | yum install iptables-services |
使用
操作 | 命令 |
---|---|
开启防火墙: | systemctl start iptables.service |
关闭防火墙: | systemctl stop iptables.service |
查看防火墙状态: | systemctl status iptables.service |
设置开机启动: | systemctl enable iptables.service |
禁用开机启动: | systemctl disable iptables.service |
查看filter表的几条链规则(INPUT链可以看出开放了哪些端口): | iptables -L -n |
查看NAT表的链规则: | iptables -t nat -L -n |
清除防火墙所有规则: |
|
给INPUT链添加规则(开放8080端口): | iptables -I INPUT -p tcp --dport 8080 -j ACCEPT |
查找规则所在行号: | iptables -L INPUT --line-numbers -n |
根据行号删除过滤规则(关闭8080端口): | iptables -D INPUT 1 |