#!/bin/bash # desc: 此脚本用来创建用户,并设置成公钥登陆. # author:xumin@joyport.com # date: 2015-06-18 # 设置环境变量 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH #输入需要创建的用户名 read -p "Input the user name:" username #确认是否上传相应的文件 read -p "Do you upload user public key file(e.g. xumin.pub) to home directory before excute this script? (y or n):" fileflag if [ "$fileflag" != "Y" ] && [ "$fileflag" != "y" ]; then echo "please upload user public key file(e.g. xumin.pub) to home directory.\n" exit 2 fi echo "start setting....... \n" #检查公钥文件是否已经上传 filepath=~/$username.pub if [ -f $filepath ];then echo "$filepath 文件存在!" else echo "$filepath 文件不存在或者您输入的路径有误" exit 3 fi #新增用户组 groupadd $username if [ $? == 0 ];then echo "group add success\n" else exit 4 fi #新增用户并自动建立文件夹和初始配置,设置个密码 useradd -g $username -d /home/$username $username if [ $? == 0 ];then echo "user add success\n" else exit 5 fi #设置初始密码 echo "123" | passwd --stdin $username #新建ssh文件目录 mkdir /home/$username/.ssh #把公钥改名放到对应的地方 mv ~/$username.pub /home/$username/.ssh/authorized_keys #修改文件夹权限及文件权限 chown -R $username.$username /home/$username/.ssh chmod 700 /home/$username/.ssh chmod 644 /home/$username/.ssh/authorized_keys #改sudoer配置,加入sudoer chmod 777 /etc/sudoers echo "$username ALL=(ALL) ALL">>/etc/sudoers #权限改回去 chmod 440 /etc/sudoers echo "end setting....... \n" #删除用户并删除其主目录 #userdel -r $username
修改公钥
#!/bin/bash # desc: 此脚本用来修改用户登陆公钥 # author:xumin@joyport.com # date: 2015-06-18 # 设置环境变量 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH #输入需要创建的用户名 read -p "Input the user name:" username #确认是否上传相应的文件 read -p "Do you upload user public key file(e.g. xumin.pub) to home directory before excute this script? (y or n):" fileflag if [ "$fileflag" != "Y" ] && [ "$fileflag" != "y" ]; then echo "please upload user public key file(e.g. xumin.pub) to home directory.\n" exit 2 fi echo "start changing....... \n" #检查公钥文件是否已经上传 filepath=~/$username.pub if [ -f $filepath ];then echo "$filepath 文件存在!" else echo "$filepath 文件不存在或者您输入的路径有误" exit 3 fi rm -rf /home/$username/.ssh/authorized_keys #把公钥改名放到对应的地方 mv ~/$username.pub /home/$username/.ssh/authorized_keys #修改文件夹权限及文件权限 chown -R $username.$username /home/$username/.ssh chmod 700 /home/$username/.ssh chmod 644 /home/$username/.ssh/authorized_keys echo "end changing....... \n" #删除用户并删除其主目录 #userdel -r $username