该脚本的主要目的就是:修改主机名,添加用户名和密码并且挂载目录的脚本。
#!/bin/bash
#Add the hostname
read -p "Please input your hostname:" Hostname
hostname $Hostname 【必须得添加,如果不添加的话,我们的主机头就不会修改,
old=`cat /etc/sysconfig/network | grep HOSTNAME | awk -F= '{print $2}'`
sed -i "s/$old/$Hostname/g" /etc/sysconfig/network
echo "The /etc/sysconfig/network has changed,please check:"
echo "========================================="
cat /etc/sysconfig/network
echo "========================================="
ADDR=`ifconfig | grep Bcast | awk '{print $2}' | awk -F: '{print $2}'`
Hostn=`echo $Hostname|awk -F. '{print $1}'`
flage=`grep $ADDR /etc/hosts`
if [ -n "$flage" ];then
sed -i "/^$ADDR/d" /etc/hosts
echo "$ADDR $Hostname $Hostn" >> /etc/hosts
fi 【判断语句,主要是为了在/etc/rc.local中重复添加mount语句,同时应该注意判断语句用的精妙之处,可参考我的博客】
echo " "
echo "Hostname has added to /ec/hosts,please check:"
cat /etc/hosts
echo " "
#start the portmap service
service portmap start
chkconfig --level 2345 cups on 【开启端口映射】
cd
mkdir -p /nfs
mount -t nfs 192.168.55.233:/opt/nfs /nfs
echo "Please check the mount point whether or not added to /nfs:"
df -h
echo "******************************************"
flag=`grep mount /etc/rc.local`
if [ -n "$flag" ];then
sed -i '/^mount/d' /etc/rc.local
fi 【判断语句,主要是为了在/etc/rc.local中重复添加mount语句,同时应该注意判断语句用的精妙之处,可参考我的博客】
echo "mount -t nfs 192.168.55.233:/opt/nfs /nfs" >> /etc/rc.local
echo "We have added the mount point to /etc/rc.local,please check:"
cat /etc/rc.local
echo "******************************************"
#This shell is to add a user and its password!
echo "Now we'll add the user oracle and its password! "
useradd oracle
echo " "
echo 99billadmin | passwd --stdin oracle
cat /etc/passwd | grep oracle 【在shell脚本中添加用户名密码】
echo " "
echo "You have successfully added the user oracle and its password!"
测试结果如下:
退出重新登录,显示结果为:
注意:
主机名设置完成后应重启系统,没有条件重启系统应重启重要服务,如 apache,snmp,ntp 等。