<b>2.6.5 自动化类脚本</b>
1.批量生成账户脚本
在内网开发环境中,有时需要为开发组的同事批量生成账户,如果手动添加的话会非常麻烦,这时可以写一段shell脚本来自动完成这项工作。在首次登录时密码均是统一的,在移交给开发人员使用时让他们自行更改即可,脚本代码如下(此脚本在centos 5.8 / 6.4 x86_64下均已测试通过):
#!/bin/bash
#此脚本应用于开发环境下批量生成用户
for name in tom
jerry joe jane yhc brain
do
useradd $name
echo redhat | passwd --stdin $name
don
passwd --stdin这行代码的的作用是将前面的输入通过管道命令作为自己的输出,从而避免脚本交互,达到自动化的目的。
笔者个人觉得用脚本的方式来批量自动添加用户的方法较之ansible的user模块更为简便,有兴趣的朋友也可以研究比较下。
2.系统初始化脚本
此脚本用于新装linux的相关配置工作,比如禁用iptables、selinux及ipv6,优化系统内核,停掉一些没必要启动的系统服务等。此脚本可用于公司内部的开发机器的批量部署,脚本代码如下所示(此脚本在centos 6.4 x86_64下已测试通过):
#添加epel外部yum扩展源
cd
/usr/local/src
wget
http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh
epel-release-6-8.noarch.rpm
#安装gcc基础库文件及sysstat工具
yum -y install
gcc gcc-c++ vim-enhanced unzip unrar sysstat
#配置ntpdate自动对时
ntp
echo "01 01
* * * /usr/sbin/ntpdate ntp.api.bz
>> /dev/null 2>&1" >> /etc/crontab
ntpdate
ntp.api.bz
service crond
restart
#配置文件的ulimit值
ulimit -shn
65534
echo
"ulimit -shn 65534" >> /etc/rc.local
cat >>
/etc/security/limits.conf << eof
* soft nofile 65534
* hard nofile 65534
eof
#基础系统内核优化
/etc/sysctl.conf << eof
net.ipv4.tcp_syncookies
= 1
net.ipv4.tcp_syn_retries
net.ipv4.tcp_tw_recycle
net.ipv4.tcp_tw_reuse
net.ipv4.tcp_fin_timeout
net.ipv4.tcp_keepalive_time
= 1200
net.ipv4.ip_local_port_range
= 1024 65535
net.ipv4.tcp_max_syn_backlog
= 16384
net.ipv4.tcp_max_tw_buckets
= 36000
net.ipv4.route.gc_timeout
= 100
net.ipv4.tcp_synack_retries
net.core.somaxconn
net.core.netdev_max_backlog
net.ipv4.tcp_max_orphans
/sbin/sysctl -p
#禁用control-alt-delete组合键以防止误操作
sed -i
's@ca::ctrlaltdel:/sbin/shutdown -t3 -r now@#ca::ctrlaltdel:/sbin/shutdown -t3
-r now@' /etc/inittab
#关闭selinux
's@selinux=enforcing@selinux=disabled@' /etc/selinux/config
#关闭iptables
service iptables
stop
chkconfig
iptables off
#ssh服务配置优化,请保持机器中至少有一个具有sudo权限的用户,下面的配置会禁止root远程登录
's@#permitrootlogin yes@permitrootlogin no@' /etc/ssh/sshd_config #禁止
root远程登录
's@#permitemptypasswords no@permitemptypasswords no@' /etc/ssh/sshd_config #禁止空密码登录
's@#usedns yes@usedns no@' /etc/ssh/sshd_config /etc/ssh/sshd_config
service sshd
#禁用ipv6地址
echo "alias
net-pf-10 off" >> /etc/modprobe.d/dist.conf
ipv6 off" >> /etc/modprobe.d/dist.conf
ip6tables off
#vim基础语法优化
"syntax on" >> /root/.vimrc
echo "set
nohlsearch" >> /root/.vimrc
#精简开机自启动服务,安装最小化服务的机器初始可以只保留crond、network、rsyslog、sshd这4个服务。
for i in
`chkconfig --list|grep 3:on|awk '{print $1}'`;do chkconfig --level 3 $i
off;done
for cursrv in crond rsyslog sshd network;do chkconfig
--level 3 $cursrv on;done
#重启服务器
reboot