1+ ---
2+ title : Linux 典型运维应用
3+ date : 2019-03-06
4+ ---
5+
16# Linux 典型运维应用
27
38> :bulb : 如果没有特殊说明,本文的案例都是针对 Centos 发行版本。
@@ -27,49 +32,63 @@ nameserver 8.8.8.8
2732>
2833> 8.8.8.8 是 Google DNS
2934>
30- > 👉 参考:[ 公共 DNS 哪家强] ( https://www.zhihu.com/question/32229915 )
35+ > : point_right : 参考:[ 公共 DNS 哪家强] ( https://www.zhihu.com/question/32229915 )
3136
3237(3)测试一下能否 ping 通 www.baidu.com
3338
3439### 开启、关闭防火墙
3540
36- ``` bash
37- # 开启防火墙 22 端口
38- iptables -I INPUT -p tcp --dport 22 -j accept
41+ firewalld 的基本使用
3942
40- # 彻底关闭防火墙
41- sudo systemctl status firewalld.service
42- sudo systemctl stop firewalld.service
43- sudo systemctl disable firewalld.service
43+ ``` sh
44+ 启动:systemctl start firewalld
45+ 关闭:systemctl stop firewalld
46+ 查看状态:systemctl status firewalld
47+ 开机禁用:systemctl disable firewalld
48+ 开机启用:systemctl enable firewalld
4449```
4550
46- ## 系统维护
51+ systemctl 是 CentOS7 的服务管理工具中主要的工具,它融合之前 service 和 chkconfig 的功能于一体。
4752
48- ### 查看操作系统版本和位数
53+ ```
54+ 启动一个服务:systemctl start firewalld.service
55+ 关闭一个服务:systemctl stop firewalld.service
56+ 重启一个服务:systemctl restart firewalld.service
57+ 显示一个服务的状态:systemctl status firewalld.service
58+ 在开机时启用一个服务:systemctl enable firewalld.service
59+ 在开机时禁用一个服务:systemctl disable firewalld.service
60+ 查看服务是否开机启动:systemctl is-enabled firewalld.service
61+ 查看已启动的服务列表:systemctl list-unit-files|grep enabled
62+ 查看启动失败的服务列表:systemctl --failed
63+ ```
4964
50- 查看系统版本
65+ 配置 firewalld-cmd
5166
52- ``` bash
53- # 方法一
54- cat /etc/redhat-release
55- # 方法二
56- cat /proc/version
57- # 方法三
58- uname -a
59- # 方法四
60- lsb_release -r
67+ ```
68+ 查看版本:firewall-cmd --version
69+ 查看帮助:firewall-cmd --help
70+ 显示状态:firewall-cmd --state
71+ 查看所有打开的端口:firewall-cmd --zone=public --list-ports
72+ 更新防火墙规则:firewall-cmd --reload
73+ 查看区域信息: firewall-cmd --get-active-zones
74+ 查看指定接口所属区域:firewall-cmd --get-zone-of-interface=eth0
75+ 拒绝所有包:firewall-cmd --panic-on
76+ 取消拒绝状态:firewall-cmd --panic-off
77+ 查看是否拒绝:firewall-cmd --query-panic
6178```
6279
63- 查看系统位数:
80+ 开启防火墙端口
6481
65- ``` bash
66- # 方法一
67- getconf LONG_BIT
68- # 方法二
69- file /bin/ls
70- # 方法三
71- uname -i
7282```
83+ 添加:firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效)
84+ 重新载入:firewall-cmd --reload
85+ 查看:firewall-cmd --zone= public --query-port=80/tcp
86+ 删除:firewall-cmd --zone= public --remove-port=80/tcp --permanent
87+ ```
88+
89+ > :point_right : 参考:[ CentOS7 使用 firewalld 打开关闭防火墙与端口] ( https://www.cnblogs.com/moxiaoan/p/5683743.html )
90+
91+ ## 系统维护
7392
7493### 使用 NTP 进行时间同步
7594
@@ -114,6 +133,151 @@ systemctl restart crond.service
114133
115134> :point_right : 参考:https://www.cnblogs.com/quchunhui/p/7658853.html
116135
136+ ## 自动化脚本
137+
138+ ### Linux 开机自启动脚本
139+
140+ (1)在 ` /etc/rc.local ` 文件中添加命令
141+
142+ 如果不想将脚本粘来粘去,或创建链接,可以在 ` /etc/rc.local ` 文件中添加启动命令
143+
144+ 1 . 先修改好脚本,使其所有模块都能在任意目录启动时正常执行;
145+ 2 . 再在 ` /etc/rc.local ` 的末尾添加一行以绝对路径启动脚本的行;
146+
147+ 例:
148+
149+ 执行 ` vim /etc/rc.local ` 命令,输入以下内容:
150+
151+ ``` sh
152+ #! /bin/sh
153+ #
154+ # This script will be executed *after* all the other init scripts.
155+ # You can put your own initialization stuff in here if you don't
156+ # want to do the full Sys V style init stuff.
157+
158+ touch /var/lock/subsys/local
159+ /opt/pjt_test/test.pl
160+ ```
161+
162+ (2)在 ` /etc/rc.d/init.d ` 目录下添加自启动脚本
163+
164+ Linux 在 ` /etc/rc.d/init.d ` 下有很多的文件,每个文件都是可以看到内容的,其实都是一些 shell 脚本或者可执行二进制文件。
165+
166+ Linux 开机的时候,会加载运行 ` /etc/rc.d/init.d ` 目录下的程序,因此我们可以把想要自动运行的脚本放到这个目录下即可。系统服务的启动就是通过这种方式实现的。
167+
168+ (3)运行级别设置
169+
170+ 简单的说,运行级就是操作系统当前正在运行的功能级别。
171+
172+ ```
173+ 不同的运行级定义如下:
174+ # 0 - 停机(千万不能把initdefault 设置为0 )
175+ # 1 - 单用户模式 进入方法#init s = init 1
176+ # 2 - 多用户,没有 NFS
177+ # 3 - 完全多用户模式(标准的运行级)
178+ # 4 - 没有用到
179+ # 5 - X11 多用户图形模式(xwindow)
180+ # 6 - 重新启动 (千万不要把initdefault 设置为6 )
181+ ```
182+
183+ 这些级别在 ` /etc/inittab ` 文件里指定,这个文件是 init 程序寻找的主要文件,最先运行的服务是放在/etc/rc.d 目录下的文件。
184+
185+ 在 ` /etc ` 目录下面有这么几个目录值得注意:rcS.d rc0.d rc1.d ... rc6.d (0,1... 6 代表启动级别 0 代表停止,1 代表单用户模式,2-5 代表多用户模式,6 代表重启) 它们的作用就相当于 redhat 下的 rc.d ,你可以把脚本放到 rcS.d,然后修改文件名,给它一个启动序号,如: S88mysql
186+
187+ 不过,最好的办法是放到相应的启动级别下面。具体作法:
188+
189+ (1)先把脚本 mysql 放到 /etc/init.d 目录下
190+
191+ (2)查看当前系统的启动级别
192+
193+ ``` sh
194+ $ runlevel
195+ N 3
196+ ```
197+
198+ (3)设定启动级别
199+
200+ ```
201+ # 98 为启动序号
202+ # 2 是系统的运行级别,可自己调整,注意不要忘了结尾的句点
203+ $ update-rc.d mysql start 98 2 .
204+ ```
205+
206+ 现在我们到 /etc/rc2.d 下,就多了一个 S98mysql 这样的符号链接。
207+
208+ (4)重启系统,验证设置是否有效。
209+
210+ (5)移除符号链接
211+
212+ 当你需要移除这个符号连接时,方法有三种:
213+
214+ 1 . 直接到 ` /etc/rc2.d ` 下删掉相应的链接,当然不是最好的方法;
215+
216+ 2 . 推荐做法:` update-rc.d -f s10 remove `
217+ 3 . 如果 update-rc.d 命令你不熟悉,还可以试试看 rcconf 这个命令,也很方便。
218+
219+ > :point_right : 参考:
220+ >
221+ > - https://blog.csdn.net/linuxshine/article/details/50717272
222+ > - https://www.cnblogs.com/ssooking/p/6094740.html
223+
224+ ### 定时执行脚本
225+
226+ (1)安装 crontab
227+
228+ (2)开启 crontab 服务
229+
230+ 开机自动启动 crond 服务:` chkconfig crond on `
231+
232+ 或者,按以下命令手动启动:
233+
234+ ``` bash
235+ # 启动服务
236+ systemctl start crond.service
237+ # 停止服务
238+ systemctl stop crond.service
239+ # 重启服务
240+ systemctl restart crond.service
241+ # 重新载入配置
242+ systemctl reload crond.service
243+ # 查看状态
244+ systemctl status crond.service
245+ ```
246+
247+ (3)设置需要执行的脚本
248+
249+ 有两种方法:
250+
251+ - 在命令行输入:` crontab -e ` 然后添加相应的任务,存盘退出。
252+ - 直接编辑 ` /etc/crontab ` 文件,即 ` vi /etc/crontab ` ,添加相应的任务。
253+
254+ 示例:
255+
256+ ``` bash
257+ SHELL=/bin/bash
258+ PATH=/sbin:/bin:/usr/sbin:/usr/bin
259+ MAILTO=root
260+
261+ # For details see man 4 crontabs
262+
263+ # Example of job definition:
264+ # .---------------- minute (0 - 59)
265+ # | .------------- hour (0 - 23)
266+ # | | .---------- day of month (1 - 31)
267+ # | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
268+ # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
269+ # | | | | |
270+ # * * * * * user-name command to be executed
271+
272+ # 每天早上3点时钟同步
273+ * 3 * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn
274+
275+ # 每两个小时以root身份执行 /home/hello.sh 脚本
276+ 0 * /2 * * * root /home/hello.sh
277+ ```
278+
279+ > :point_right : 参考:[ linux 定时执行脚本] ( https://blog.csdn.net/z_yong_cool/article/details/79288397 )
280+
117281## 配置
118282
119283### 设置 Linux 启动模式
@@ -131,3 +295,9 @@ systemctl restart crond.service
131295``` sh
132296$ sed -i ' s/id:5:initdefault:/id:3:initdefault:/' /etc/inittab
133297```
298+
299+ ## 参考资料
300+
301+ - [ CentOS7 使用 firewalld 打开关闭防火墙与端口] ( https://www.cnblogs.com/moxiaoan/p/5683743.html )
302+
303+ - [ linux 定时执行脚本] ( https://blog.csdn.net/z_yong_cool/article/details/79288397 )
0 commit comments