解决在脚本中source 或者 . 不生效的问题


简单的脚本初始化一个新机器
解决在脚本中source 或者 . 不生效的问题

#!/bin/bash

使用www普通用户发布,及免密

Usage : source ./useradd-trust-init-v2.sh or . ./useradd-trust-init-v2.sh

read -p " 输入需要创建的用户名 : " User
www=grep -w "$User" /etc/passwd|awk -F':' '{print $1}'
Pass=2fZotoQdt0k8nWWs1T8scUbm07dVWSKvZmB672XkG

判断是否为空

if [ -z $www ]
then
useradd "$User"
echo "$Pass" | passwd --stdin $User 1> /dev/null
fi

Home=grep -w "$User" /etc/passwd |awk -F'/' '{print $2}'
Bash=grep -w "$User" /etc/passwd |awk -F'/' '{print $NF}'
if [ $Home == home ] && [ $Bash == bash ]
then
echo -e "\n\033[34m sshpass -p '$Pass' ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $User@hostname -I\033[0m\n"
echo -e "\033[34m sshpass -p '$Pass' ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $User@curl -m 10 -s ip.cip.cc\033[0m\n"
else
usermod -d /home/$User -m $User -s /bin/bash
echo -e "\033[31m usermod To change the\033[0m"
echo -e "\n\033[34m sshpass -p '$Pass' ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $User@hostname -I\033[0m\n"
echo -e "\033[34m sshpass -p '$Pass' ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $User@curl -m 10 -s ip.cip.cc\033[0m\n"
fi

systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
sed -i "s/grep -vE '^$|^#|SELINUXTYPE' /etc/selinux/config/SELINUX=disabled/" /etc/selinux/config

yum -y install \
gcc gcc-c++ make autoconf libtool-ltdl-devel gd-devel freetype-devel libxml2-devel \
libjpeg-devel libpng-devel openssl-devel curl-devel bison patch unzip libmcrypt-devel \
libmhash-devel ncurses-devel sudo bzip2 mlocate flex lrzsz sysstat lsof setuptool ntp \
libaio-devel wget nmap finger elinks net-tools telnet curlftpfs bind-utils cmake ncurses \
ncurses-devel zlib zlib-devel bison bison-devel perl perl-devel autoconf automake xz >/dev/null 2>&1

read -p " 需要新的显性主机名么? [Y/y/N/n]: " NAME
case $NAME in
[yY][eE][sS]|[yY])
NAME="yes"
read -p " (输入服务器主机名or角色): " HostName
cat > /etc/motd << EOF

                     $HostName

     如需(启动或重启)服务,请以程序对应普通用户启动
     PHP 使用 www 用户
     定时任务使用root账户添加到/var/spool/cron/www

EOF

cat >> /etc/bashrc << EOF
PS1="[\${debian_chroot:+(\$debian_chroot)}\u@$HostName - `hostname -I|awk -F" " '{print \$1}'` \w]\\$ "
export USER_IP=`who -u am i 2>/dev/null| awk '{print \$NF}'|sed -e 's/[()]//g'`
export HISTTIMEFORMAT="[%F %T][`whoami`][\${USER_IP}] "
export PROMPT_COMMAND='{ msg=\$(history 1 | { read x y; echo \$y; });user=\$(whoami); echo \$(date "+%Y-%m-%d %H:%M:%S"):\$user:`pwd`/:\$msg ---- \$(who am i); } >> /tmp/`date +%F`-`hostname`-`whoami`-history.log'
alias cd='cdp(){ cd \$1;pwd;};cdp'

EOF
. /etc/bashrc
;;
[nN][oO]|[nN])
NAME="no"
echo "Don't need Exit"
;;
*)
echo " Input error, should be [Y/n]."
exit 20
esac

来源:https://blog.51cto.com/amunlinux/2501068


码神部落- 版权声明 1、本主题所有言论和图片纯属会员个人意见,与码神部落立场无关。
2、本站所有主题由该帖子作者发表,该帖子作者木槿暖夏码神部落享有帖子相关版权。
3、码神部落管理员和版主有权不事先通知发贴者而删除本文。
4、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者木槿暖夏码神部落的同意。
5、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任。
6、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责。
7、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意。

最新回复 (0)
    • 码神部落
      2
        立即登录 立即注册 GitHub登录
返回
发新帖
作者最近主题: