码迷,mamicode.com
首页 > 系统相关 > 详细

shell脚本安装 nfs-server

时间:2018-03-21 11:55:22      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:shell nfs-server

#!/bin/bash
: <<DESC
Program:
NFS Server installbr/>372032196@qq.com
History:
20171129 v1.0
20171130 v1.1 修改内容如下:
1、取消grep "样式" 文件 &>/dev/null if [ $? -ne 0 ];then的使用方法,修改为 if grep -q "样式" 文件;then方法
2、全都改为函数,用户可以根据需要调用,如不需要判断是否能连接到Internet,可以在main主函数中注释不然其生效
DESC
export LANG="zh_CN.UTF-8"
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
[ -e /etc/init.d/functions ] && . /etc/init.d/functions

1 判断是否为root

root_user(){
[ $UID -ne 0 ] && { echo "请使用root用户";exit 1; }
}

2 判断是否能连接到Internet

con_internet(){
ping -c2 mirrors.aliyun.com &>/dev/null
[ $? -ne 0 ] && { echo "无法连接到Internet";exit 1; }
}

3 SELINUX是否关闭

selinux_cls(){
if [ getenforce != "Disabled" ];then
setenforce 0
sed -ir ‘s/^(SELINUX=).*/\1disabled/‘ /etc/sysconfig/selinux
fi
}

4 IPTABLES是否关闭

iptables_cls(){
if ! /etc/init.d/iptables status|grep -q ‘not running‘;then
/etc/init.d/iptables stop
chkconfig iptables off
fi
}

5 yum修改

yum_mod(){
if ! grep -q aliyun /etc/yum.repos.d/CentOS-Base.repo;then
\cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.reop.bak
wget -O /etc/yum.repos./CentOS-Base.reop
http://mirrors.aliyun.com/repo/Centos-6.repo
fi
}

6 install NFS Server

ins_nfs(){
if [ rpm -aq rpcbind nfs-utils|wc -l -lt 2 ];then
yum install -y rpcbind nfs-utils &>/dev/null
if [ $? -ne 0 ];then
echo "NFS Server install fail"
exit 1
fi
/etc/init.d/rpcbind start &>/dev/null
/etc/init.d/nfs start &>/dev/null
fi
if [ netstat -lntup|egrep "rpcbind|nfs"|wc -l -lt 6 ];then
action "NFS Server install..." /bin/fasle
else
action "NFS Server install..." /bin/true
fi
}

7 配置 /etc/exports 文档,NFS Server默认目录设置为 /nfsnobody

nfs_conf(){
[ -d /nfsnobody ] || mkdir /nfsnobody
chown -R nfsnobody:nfsnobody /nfsnobody
if [ -s /etc/exports ];then
echo ‘/nfsnobody 192.168.0.0/24(rw,all_squash,sync)‘ >>/etc/exports
else
echo ‘/nfsnobody 192.168.0.0/24(rw,all_squash,sync)‘ >/etc/exports
fi
exportfs -r &>/dev/null
}

8 优化NFS,设置 /etc/sysctl.conf 文档

sysctl_opt(){
default=$(sysctl -a|grep "[rw]mem_default"|awk ‘{print $NF}‘)
max=$(sysctl -a|grep "[rw]mem_max"|awk ‘{print $NF}‘)
if [ "$default" != "8388608 8388608" -a "$max" != "16777216 16777216" ];then
cat >>/etc/sysctl.conf<<EOF
net.core.rmem_default = 8388608
net.core.wmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
EOF
sysctl -p &>/dev/null
fi
}

9 把NFS Server开机启动放置到 /etc/rc.local

nfs_boot_start(){
if [ egrep "(rpcbind|nfs) start" /etc/rc.local|wc -l -lt 2 ];then
echo -e "\n#nfs server 20171129\n/etc/init.d/rpcbind start\n/etc/init.d/nfs start" >>/etc/rc.local
fi
}

main(){
root_user
con_internet
selinux_cls
iptables_cls
yum_mod
ins_nfs
nfs_conf
sysctl_opt
nfs_boot_start
}
main

shell脚本安装 nfs-server

标签:shell nfs-server

原文地址:http://blog.51cto.com/lehappy/2089213

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!