#!/bin/bash # desc: This script is used to create the user and set the public key login .# author:[email protected]# date: 2015-06-18# Setting Environment Variables Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export path# Enter the user name you want to create read -p "Input the user name:" username# Verify that the appropriate file is uploaded read -p "Do you upload user public key file (e.g. xumin.pub) to home directory before excute this script? (y or n): " fileflagif [ "$fileflag" != "Y" ] && [ "$fileflag" != "Y" ]; then echo "please upload user public key file (e.g. xumin.pub) to home directory.\n " exit 2 fiecho "start setting....... \n" #检查公钥文件是否已经上传filepath =~/$username. pubif [ -f $filepath  ];THEN&Nbsp; echo "$filepath file exists!" else echo "$filepath file does not exist or you have entered the wrong path" exit 3fi# New user group groupadd $usernameif [ $? == 0 ];then echo " Group add success\n "else exit 4fi# add users and automatically create folders and initial configuration, set a password useradd -g $username -d /home/$username $usernameif [ $? == 0 ];then echo "user add success\n" else exit 5fi# set initial password echo "123" | passwd --stdin $username # New SSH file directory mkdir /home/$username/.ssh# Rename the public key to the corresponding place mv ~/$username. pub /home/$username/.ssh/authorized_keys# Modify folder permissions and file permissions chown -r $ Username. $username /home/$username/.sshchmod 700 /home/$username/.sshchmod 644 /home/ $username/.ssh/authorized_keys# Change to sudoer configuration, add Sudoerchmod 777 /etc/sudoersecho "$username all= (All) all" >>/etc /sudoers# Permissions Change back chmod 440 /etc/sudoersecho "end setting....... \n" #删除用户并删除其主目录 #userdel -r $username
Modifying the Public key
#!/bin/bash # desc: This script is used to modify the user login public key # author:[email protected]# date: 2015-06-18# Setting Environment Variables Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export path# Enter the user name you want to create read -p "Input the user name:" username# Verify that the appropriate file is uploaded read -p "Do you upload user public key file (e.g. xumin.pub) to home directory before excute this script? (y or n): " fileflagif [ "$fileflag" != "Y" ] && [ "$fileflag" != "Y" ]; then echo "please upload user public key file (e.g. xumin.pub) to home directory.\n " exit 2 fiecho "start changing....... \n" #检查公钥文件是否已经上传filepath =~/$username .pubif [ -f $filepath ];then echo "$filepath file exists!" else echo "$filepath file does not exist or you have entered the wrong path" exit 3firm -rf /home/$username/.ssh/authorized_keys# Rename the public key to the corresponding place mv ~/$username. pub /home/$username/. ssh/authorized_keys# Modify folder permissions and file Permissions chown -r $username. $username /home/$username/.sshchmod 700 /home/$username/.sshchmod 644 /home/$username/.ssh/authorized_keysecho "end changing ... \n "#删除用户并删除其主目录 #userdel -r $username
Set server Public key login SH script