Skip to content

Commit a166822

Browse files
committed
更新脚本
1 parent 2664d07 commit a166822

13 files changed

Lines changed: 164 additions & 78 deletions

File tree

codes/linux/ops/service/elk/install-elk.sh

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,9 @@
77
# passwd elk
88

99
# 获取当前设备IP
10-
IP=""
11-
getDeviceIp() {
12-
IP=`ifconfig eth0 | grep "inet" | awk '{ print $2}' | awk -F: '{print $2}'`
13-
if [ "$IP" == "" ]; then
14-
IP=`ifconfig eth0 | grep "inet" | awk '{ print $2}'`
15-
fi
16-
if [ "$IP" == "" ]; then
17-
IP=`ifconfig ens32 | grep "inet"|grep "broadcast" | awk '{ print $2}' | awk -F: '{print $1}'`
18-
fi
19-
20-
if [ "${IP}" == "" ]; then
21-
echo " "
22-
echo " 请输入服务器IP地址................ "
23-
echo " "
24-
exit 0
25-
else
26-
echo "当前设备IP: $IP"
27-
fi
10+
ipaddr='127.0.0.1'
11+
function getDeviceIp() {
12+
ipaddr=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
2813
}
2914

3015
# 检查文件是否存在,不存在则退出脚本

codes/linux/ops/sys/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 脚本使用说明
2+
3+
> 建议按照顺序执行本文脚本。
4+
5+
<!-- TOC depthFrom:2 depthTo:3 -->
6+
7+
- [替换 yum repo 源](#替换-yum-repo-源)
8+
- [安装基本的命令工具(可选)](#安装基本的命令工具可选)
9+
- [关闭防火墙](#关闭防火墙)
10+
- [设置 DNS](#设置-dns)
11+
- [设置 ntp](#设置-ntp)
12+
13+
<!-- /TOC -->
14+
15+
## 替换 yum repo 源
16+
17+
由于 CentOS 默认 yum 源,访问速度很慢,所以推荐使用国内镜像。
18+
19+
使用方法:执行以下任意命令即可执行脚本。
20+
21+
```sh
22+
curl -o- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/yum/change-yum-repo.sh | bash
23+
wget -qO- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/yum/change-yum-repo.sh | bash
24+
```
25+
26+
## 安装基本的命令工具(可选)
27+
28+
工具清单(可以根据需要,在 install-cmd-tool.sh 中把不需要的工具注掉):
29+
30+
```
31+
# 核心工具:df、du、chkconfig
32+
# 网络工具:ifconfig、netstat、route
33+
# IP工具:ip、ss、ping、tracepath、traceroute
34+
# DNS工具:dig、host、nslookup、whois
35+
# 端口工具:lsof、nc、telnet
36+
# 下载工具:curl、wget
37+
# 防火墙工具:iptables
38+
# 编辑工具:emacs、vim
39+
# 流量工具:iftop、nethogs
40+
# 抓包工具:tcpdump
41+
# 压缩工具:unzip、zip
42+
# 版本控制工具:git
43+
```
44+
45+
使用方法:执行以下任意命令即可执行脚本。
46+
47+
```sh
48+
curl -o- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/install-cmd-tool.sh | bash
49+
wget -qO- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/install-cmd-tool.sh | bash
50+
```
51+
52+
## 关闭防火墙
53+
54+
使用方法:执行以下任意命令即可执行脚本。
55+
56+
```sh
57+
curl -o- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/stop-firewall.sh | bash
58+
wget -qO- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/stop-firewall.sh | bash
59+
```
60+
61+
## 设置 DNS
62+
63+
使用方法:执行以下任意命令即可执行脚本。
64+
65+
```sh
66+
curl -o- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/set-dns.sh | bash
67+
wget -qO- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/set-dns.sh | bash
68+
```
69+
70+
## 设置 ntp
71+
72+
使用方法:执行以下任意命令即可执行脚本。
73+
74+
```sh
75+
curl -o- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/set-ntp.sh | bash
76+
wget -qO- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/set-ntp.sh | bash
77+
```
Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,6 @@
66
# Author: Zhang Peng
77
###################################################################################
88

9-
# 获取当前机器 IP
10-
ip=""
11-
function getDeviceIp() {
12-
ip=`ifconfig eth0 | grep "inet addr" | awk '{ print $2}' | awk -F: '{print $2}'`
13-
if [ "$ip" == "" ]
14-
then
15-
ip=`ifconfig ens32 | grep "inet"|grep "broadcast" | awk '{ print $2}' | awk -F: '{print $1}'`
16-
fi
17-
18-
if [ "$ip" == "" ]
19-
then
20-
ip=`echo $1`
21-
fi
22-
23-
if [ "${ip}" == "" ]
24-
then
25-
echo "无法获取IP地址"
26-
exit 0
27-
fi
28-
}
29-
30-
function setDNS() {
31-
getDeviceIp
32-
host=`hostname`
33-
cat >> /etc/hosts << EOF
34-
${ip} ${host}
35-
EOF
36-
}
37-
38-
function setNameServer() {
39-
echo "添加域名服务器"
40-
echo "nameserver 218.2.135.1" >> /etc/resolv.conf
41-
}
42-
43-
function setNtp() {
44-
# 时钟同步工具
45-
yum -y install ntp
46-
# 同步上海交通大学网络中心NTP服务器
47-
echo "* 4 * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn > /dev/null 2>&1" >> /var/spool/cron/root
48-
# 以一个服务器时间为标准定时更新时间(有时需要以公司中的服务器作为标准)
49-
#echo "*/30 * * * * /usr/local/bin/ntpdate 192.168.16.182" >> /var/spool/cron/root
50-
}
51-
529
function setLimit() {
5310
cat >> /etc/security/limits.conf << EOF
5411
* - nofile 65535
@@ -74,13 +31,6 @@ function closeSelinux() {
7431
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
7532
}
7633

77-
function closeFirewall() {
78-
echo "关闭防火墙"
79-
80-
# see https://www.cnblogs.com/moxiaoan/p/5683743.html
81-
systemctl stop firewalld
82-
systemctl disable firewalld
83-
}
8434

8535
function setBootMode() {
8636
# 1. 停机(记得不要把 initdefault 配置为 0,因为这样会使 Linux 不能启动)
@@ -136,15 +86,13 @@ echo "NETWORKING_IPV6=off" >> /etc/sysconfig/network
13686
######################################## MAIN ########################################
13787
echo -e "\n>>>>>>>>> 配置系统环境"
13888

89+
filepath=$(cd "$(dirname "$0")"; pwd)
90+
13991
# 关闭 selinux
14092
closeSelinux
14193

142-
# 关闭防火墙
143-
closeFirewall
144-
14594
# 设置 DNS 服务器和本机 Host
146-
setNameServer
147-
setDNS
95+
${filepath}/set-dns.sh
14896

14997
# 设置时间同步
15098
setNtp
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# 流量工具:iftop、nethogs
2222
# 抓包工具:tcpdump
2323
# 压缩工具:unzip、zip
24+
# 版本控制工具:git
2425
###################################################################################
2526
# 核心工具
2627
echo -e "\n>>>>>>>>> install coreutils(df、du)"
@@ -81,4 +82,9 @@ echo -e "\n>>>>>>>>> install tcpdump"
8182
yum install -y tcpdump
8283

8384
# 压缩工具
85+
echo -e "\n>>>>>>>>> install unzip"
8486
yum install -y unzip
87+
88+
# 版本控制工具
89+
echo -e "\n>>>>>>>>> install git"
90+
yum install -y git

codes/linux/ops/sys/set-dns.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
3+
###################################################################################
4+
# 在 /etc/resolv.conf 中设置 DNS 服务器
5+
# 在 /etc/hosts 中设置本机域名
6+
# Author: Zhang Peng
7+
###################################################################################
8+
ip='127.0.0.1'
9+
function getDeviceIp() {
10+
ip=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
11+
}
12+
13+
function setDNSServer() {
14+
echo -e "设置DNS服务器"
15+
cat >> /etc/resolv.conf << EOF
16+
nameserver 114.114.114.114
17+
nameserver 8.8.8.8
18+
EOF
19+
}
20+
21+
function setHosts() {
22+
getDeviceIp
23+
host=`hostname`
24+
cat >> /etc/hosts << EOF
25+
${ip} ${host}
26+
EOF
27+
}
28+
29+
######################################## MAIN ########################################
30+
echo -e "\n>>>>>>>>> 配置系统环境"
31+
setDNSServer
32+
setHosts

codes/linux/ops/sys/set-ntp.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
###################################################################################
4+
# 使用 NTP 进行时间同步
5+
# 参考:https://www.cnblogs.com/quchunhui/p/7658853.html
6+
# Author: Zhang Peng
7+
###################################################################################
8+
9+
echo -e "\n>>>>>>>>> 设置 ntp"
10+
11+
echo -e "先安装时钟同步工具 ntp"
12+
yum -y install ntp
13+
14+
ip=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
15+
/sbin/iptables -A INPUT -p UDP -i eth0 -s ${ip}/24 --dport 123 -j ACCEPT
16+
17+
echo -e "启动 NTP 服务"
18+
systemctl start ntpd.service
19+
20+
echo -e "立即执行时间同步"
21+
/usr/sbin/ntpdate ntp.sjtu.edu.cn
22+
23+
echo -e "自动定时同步时间"
24+
echo "* 3 * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn" >> /etc/crontab
25+
systemctl restart crond.service
26+
27+
echo -e "同步后系统时间:"
28+
date
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
###################################################################################
4+
# 彻底关闭防火墙
5+
# 参考:https://www.cnblogs.com/moxiaoan/p/5683743.html
6+
# Author: Zhang Peng
7+
###################################################################################
8+
9+
systemctl stop firewalld
10+
systemctl disable firewalld

0 commit comments

Comments
 (0)