更新脚本

This commit is contained in:
Zhang Peng 2019-02-22 18:57:46 +08:00
parent 2664d075dd
commit a16682276a
13 changed files with 164 additions and 78 deletions

View File

@ -7,24 +7,9 @@
# passwd elk # passwd elk
# 获取当前设备IP # 获取当前设备IP
IP="" ipaddr='127.0.0.1'
getDeviceIp() { function getDeviceIp() {
IP=`ifconfig eth0 | grep "inet" | awk '{ print $2}' | awk -F: '{print $2}'` ipaddr=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
if [ "$IP" == "" ]; then
IP=`ifconfig eth0 | grep "inet" | awk '{ print $2}'`
fi
if [ "$IP" == "" ]; then
IP=`ifconfig ens32 | grep "inet"|grep "broadcast" | awk '{ print $2}' | awk -F: '{print $1}'`
fi
if [ "${IP}" == "" ]; then
echo " "
echo " 请输入服务器IP地址................ "
echo " "
exit 0
else
echo "当前设备IP: $IP"
fi
} }
# 检查文件是否存在,不存在则退出脚本 # 检查文件是否存在,不存在则退出脚本

View File

@ -0,0 +1,77 @@
# 脚本使用说明
> 建议按照顺序执行本文脚本。
<!-- TOC depthFrom:2 depthTo:3 -->
- [替换 yum repo 源](#替换-yum-repo-源)
- [安装基本的命令工具(可选)](#安装基本的命令工具可选)
- [关闭防火墙](#关闭防火墙)
- [设置 DNS](#设置-dns)
- [设置 ntp](#设置-ntp)
<!-- /TOC -->
## 替换 yum repo 源
由于 CentOS 默认 yum 源,访问速度很慢,所以推荐使用国内镜像。
使用方法:执行以下任意命令即可执行脚本。
```sh
curl -o- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/yum/change-yum-repo.sh | bash
wget -qO- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/yum/change-yum-repo.sh | bash
```
## 安装基本的命令工具(可选)
工具清单(可以根据需要,在 install-cmd-tool.sh 中把不需要的工具注掉):
```
# 核心工具df、du、chkconfig
# 网络工具ifconfig、netstat、route
# IP工具ip、ss、ping、tracepath、traceroute
# DNS工具dig、host、nslookup、whois
# 端口工具lsof、nc、telnet
# 下载工具curl、wget
# 防火墙工具iptables
# 编辑工具emacs、vim
# 流量工具iftop、nethogs
# 抓包工具tcpdump
# 压缩工具unzip、zip
# 版本控制工具git
```
使用方法:执行以下任意命令即可执行脚本。
```sh
curl -o- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/install-cmd-tool.sh | bash
wget -qO- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/install-cmd-tool.sh | bash
```
## 关闭防火墙
使用方法:执行以下任意命令即可执行脚本。
```sh
curl -o- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/stop-firewall.sh | bash
wget -qO- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/stop-firewall.sh | bash
```
## 设置 DNS
使用方法:执行以下任意命令即可执行脚本。
```sh
curl -o- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/set-dns.sh | bash
wget -qO- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/set-dns.sh | bash
```
## 设置 ntp
使用方法:执行以下任意命令即可执行脚本。
```sh
curl -o- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/set-ntp.sh | bash
wget -qO- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/set-ntp.sh | bash
```

View File

@ -6,49 +6,6 @@
# Author: Zhang Peng # Author: Zhang Peng
################################################################################### ###################################################################################
# 获取当前机器 IP
ip=""
function getDeviceIp() {
ip=`ifconfig eth0 | grep "inet addr" | awk '{ print $2}' | awk -F: '{print $2}'`
if [ "$ip" == "" ]
then
ip=`ifconfig ens32 | grep "inet"|grep "broadcast" | awk '{ print $2}' | awk -F: '{print $1}'`
fi
if [ "$ip" == "" ]
then
ip=`echo $1`
fi
if [ "${ip}" == "" ]
then
echo "无法获取IP地址"
exit 0
fi
}
function setDNS() {
getDeviceIp
host=`hostname`
cat >> /etc/hosts << EOF
${ip} ${host}
EOF
}
function setNameServer() {
echo "添加域名服务器"
echo "nameserver 218.2.135.1" >> /etc/resolv.conf
}
function setNtp() {
# 时钟同步工具
yum -y install ntp
# 同步上海交通大学网络中心NTP服务器
echo "* 4 * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn > /dev/null 2>&1" >> /var/spool/cron/root
# 以一个服务器时间为标准定时更新时间(有时需要以公司中的服务器作为标准)
#echo "*/30 * * * * /usr/local/bin/ntpdate 192.168.16.182" >> /var/spool/cron/root
}
function setLimit() { function setLimit() {
cat >> /etc/security/limits.conf << EOF cat >> /etc/security/limits.conf << EOF
* - nofile 65535 * - nofile 65535
@ -74,13 +31,6 @@ function closeSelinux() {
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
} }
function closeFirewall() {
echo "关闭防火墙"
# see https://www.cnblogs.com/moxiaoan/p/5683743.html
systemctl stop firewalld
systemctl disable firewalld
}
function setBootMode() { function setBootMode() {
# 1. 停机(记得不要把 initdefault 配置为 0因为这样会使 Linux 不能启动) # 1. 停机(记得不要把 initdefault 配置为 0因为这样会使 Linux 不能启动)
@ -136,15 +86,13 @@ echo "NETWORKING_IPV6=off" >> /etc/sysconfig/network
######################################## MAIN ######################################## ######################################## MAIN ########################################
echo -e "\n>>>>>>>>> 配置系统环境" echo -e "\n>>>>>>>>> 配置系统环境"
filepath=$(cd "$(dirname "$0")"; pwd)
# 关闭 selinux # 关闭 selinux
closeSelinux closeSelinux
# 关闭防火墙
closeFirewall
# 设置 DNS 服务器和本机 Host # 设置 DNS 服务器和本机 Host
setNameServer ${filepath}/set-dns.sh
setDNS
# 设置时间同步 # 设置时间同步
setNtp setNtp

View File

@ -21,6 +21,7 @@
# 流量工具iftop、nethogs # 流量工具iftop、nethogs
# 抓包工具tcpdump # 抓包工具tcpdump
# 压缩工具unzip、zip # 压缩工具unzip、zip
# 版本控制工具git
################################################################################### ###################################################################################
# 核心工具 # 核心工具
echo -e "\n>>>>>>>>> install coreutils(df、du)" echo -e "\n>>>>>>>>> install coreutils(df、du)"
@ -81,4 +82,9 @@ echo -e "\n>>>>>>>>> install tcpdump"
yum install -y tcpdump yum install -y tcpdump
# 压缩工具 # 压缩工具
echo -e "\n>>>>>>>>> install unzip"
yum install -y unzip yum install -y unzip
# 版本控制工具
echo -e "\n>>>>>>>>> install git"
yum install -y git

View File

@ -0,0 +1,32 @@
#!/usr/bin/env bash
###################################################################################
# 在 /etc/resolv.conf 中设置 DNS 服务器
# 在 /etc/hosts 中设置本机域名
# Author: Zhang Peng
###################################################################################
ip='127.0.0.1'
function getDeviceIp() {
ip=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
}
function setDNSServer() {
echo -e "设置DNS服务器"
cat >> /etc/resolv.conf << EOF
nameserver 114.114.114.114
nameserver 8.8.8.8
EOF
}
function setHosts() {
getDeviceIp
host=`hostname`
cat >> /etc/hosts << EOF
${ip} ${host}
EOF
}
######################################## MAIN ########################################
echo -e "\n>>>>>>>>> 配置系统环境"
setDNSServer
setHosts

View File

@ -0,0 +1,28 @@
#!/usr/bin/env bash
###################################################################################
# 使用 NTP 进行时间同步
# 参考https://www.cnblogs.com/quchunhui/p/7658853.html
# Author: Zhang Peng
###################################################################################
echo -e "\n>>>>>>>>> 设置 ntp"
echo -e "先安装时钟同步工具 ntp"
yum -y install ntp
ip=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
/sbin/iptables -A INPUT -p UDP -i eth0 -s ${ip}/24 --dport 123 -j ACCEPT
echo -e "启动 NTP 服务"
systemctl start ntpd.service
echo -e "立即执行时间同步"
/usr/sbin/ntpdate ntp.sjtu.edu.cn
echo -e "自动定时同步时间"
echo "* 3 * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn" >> /etc/crontab
systemctl restart crond.service
echo -e "同步后系统时间:"
date

View File

@ -0,0 +1,10 @@
#!/usr/bin/env bash
###################################################################################
# 彻底关闭防火墙
# 参考https://www.cnblogs.com/moxiaoan/p/5683743.html
# Author: Zhang Peng
###################################################################################
systemctl stop firewalld
systemctl disable firewalld

View File

@ -7,6 +7,6 @@
使用方法:执行以下任意命令即可执行脚本。 使用方法:执行以下任意命令即可执行脚本。
```sh ```sh
curl -o- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/sys/yum/change-yum-repo.sh | bash curl -o- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/yum/change-yum-repo.sh | bash
wget -qO- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/sys/yum/change-yum-repo.sh | bash wget -qO- https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/yum/change-yum-repo.sh | bash
``` ```

View File

@ -20,7 +20,7 @@ cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
# 根据发型版本选择相应 yum 镜像 # 根据发型版本选择相应 yum 镜像
if [[ ${version} == 5 ]]; then if [[ ${version} == 5 ]]; then
# Cento5 已废弃,只能使用 http://vault.CentOS.org/ 替换,但由于是国外镜像,速度较慢 # Cento5 已废弃,只能使用 http://vault.CentOS.org/ 替换,但由于是国外镜像,速度较慢
wget -N https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/sys/yum/Centos-5.repo -O /etc/yum.repos.d/CentOS-Base.repo wget -N https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/yum/Centos-5.repo -O /etc/yum.repos.d/CentOS-Base.repo
# 根据实际发型版本情况替换 # 根据实际发型版本情况替换
detailVersion=`lsb_release -r | awk '{print substr($2,1,3)}'` detailVersion=`lsb_release -r | awk '{print substr($2,1,3)}'`
@ -29,9 +29,9 @@ if [[ ${version} == 5 ]]; then
# 不替换下面的开关可能会出现错误Could not open/read repomd.xml # 不替换下面的开关可能会出现错误Could not open/read repomd.xml
sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/CentOS-Media.repo sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/CentOS-Media.repo
elif [[ ${version} == 6 ]]; then elif [[ ${version} == 6 ]]; then
wget -N https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/sys/yum/Centos-6.repo -O /etc/yum.repos.d/CentOS-Base.repo wget -N https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/yum/Centos-6.repo -O /etc/yum.repos.d/CentOS-Base.repo
elif [[ ${version} == 7 ]]; then elif [[ ${version} == 7 ]]; then
wget -N https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/sys/yum/Centos-7.repo -O /etc/yum.repos.d/CentOS-Base.repo wget -N https://raw.githubusercontent.com/dunwu/os-tutorial/master/codes/linux/ops/sys/yum/Centos-7.repo -O /etc/yum.repos.d/CentOS-Base.repo
else else
echo -e "版本不支持,替换 yum repo 失败" echo -e "版本不支持,替换 yum repo 失败"
fi fi