Set server Public key login SH script

Source: Internet
Author: User

#!/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.