Centos创建用户设置密钥并给予sudo权限
1、添加一个普通用户
useradd whsir
|---|---------------| | 1 | useradd whsir |
2、添加这个用户的密钥
mkdir ~whsir/.ssh cat >> ~whsir/.ssh/authorized_keys << EOF #whsir ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEApINpHWhNHj6W3vKLw9ykSygP0a2aqM+EO5aDuLJvu7r0qn9njIjaPjNRnJ4IC05VM/zvWkBEp6/jvdPTqxW5D40by9FcWWUqCzi+OSq/9R4qAaDyP7CQnj60U5JohuRa29MaIX63UANCj4oQo3SLQttQcFUSTA3azMYLnFBHf04AFJTRndkGiO7b3qAmUhceuCOrknU83VjHXBFYeMvFeBSsEpSi5Zzii+jtgupOrOMJreUY57na0Lyy+PjC7EQsDrX8Ykfz0aoOa7f1cguK2v/ZBriwTN1SRt0MWqeNpmCfHL1xkJ0chZsFwZkhEaSoA7uQzD7xwyA9TAj8o3m3Sw== 这是一个示例 EOF
|-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 | mkdir ~whsir/.ssh cat >> ~whsir/.ssh/authorized_keys << EOF #whsir ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEApINpHWhNHj6W3vKLw9ykSygP0a2aqM+EO5aDuLJvu7r0qn9njIjaPjNRnJ4IC05VM/zvWkBEp6/jvdPTqxW5D40by9FcWWUqCzi+OSq/9R4qAaDyP7CQnj60U5JohuRa29MaIX63UANCj4oQo3SLQttQcFUSTA3azMYLnFBHf04AFJTRndkGiO7b3qAmUhceuCOrknU83VjHXBFYeMvFeBSsEpSi5Zzii+jtgupOrOMJreUY57na0Lyy+PjC7EQsDrX8Ykfz0aoOa7f1cguK2v/ZBriwTN1SRt0MWqeNpmCfHL1xkJ0chZsFwZkhEaSoA7uQzD7xwyA9TAj8o3m3Sw== 这是一个示例 EOF |
3、设置权限
chown whsir:whsir ~whsir/.ssh chmod 0755 ~whsir/.ssh/ chmod 0644 ~whsir/.ssh/authorized_keys echo "whsir ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/whsir
|---------|--------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 | chown whsir:whsir ~whsir/.ssh chmod 0755 ~whsir/.ssh/ chmod 0644 ~whsir/.ssh/authorized_keys echo "whsir ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/whsir |
4、设置ssh
sed -i -e "s/#UseDNS yes/UseDNS no/g" -e "s/#PermitRootLogin yes/PermitRootLogin no/g" -e "s/PermitRootLogin yes/PermitRootLogin no/g" -e "s/#PermitEmptyPasswords no/PermitEmptyPasswords no/g" /etc/ssh/sshd_config
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 | sed -i -e "s/#UseDNS yes/UseDNS no/g" -e "s/#PermitRootLogin yes/PermitRootLogin no/g" -e "s/PermitRootLogin yes/PermitRootLogin no/g" -e "s/#PermitEmptyPasswords no/PermitEmptyPasswords no/g" /etc/ssh/sshd_config |
5、重启ssh服务
service sshd restart
|---|----------------------| | 1 | service sshd restart |
ssh密钥设置可参考: https://blog.whsir.com/post-996.html
脚本如下
#adduser useradd whsir mkdir ~whsir/.ssh cat >> ~whsir/.ssh/authorized_keys << EOF #whsir ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEApINpHWhNHj6W3vKLw9ykSygP0a2aqM+EO5aDuLJvu7r0qn9njIjaPjNRnJ4IC05VM/zvWkBEp6/jvdPTqxW5D40by9FcWWUqCzi+OSq/9R4qAaDyP7CQnj60U5JohuRa29MaIX63UANCj4oQo3SLQttQcFUSTA3azMYLnFBHf04AFJTRndkGiO7b3qAmUhceuCOrknU83VjHXBFYeMvFeBSsEpSi5Zzii+jtgupOrOMJreUY57na0Lyy+PjC7EQsDrX8Ykfz0aoOa7f1cguK2v/ZBriwTN1SRt0MWqeNpmCfHL1xkJ0chZsFwZkhEaSoA7uQzD7xwyA9TAj8o3m3Sw== EOF chown whsir:whsir ~whsir/.ssh chmod 0755 ~whsir/.ssh/ chmod 0644 ~whsir/.ssh/authorized_keys echo "whsir ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/wuhao #sshd sed -i "s/#UseDNS yes/UseDNS no/g" /etc/ssh/sshd_config sed -i "s/#PermitRootLogin yes/PermitRootLogin no/g" /etc/ssh/sshd_config sed -i "s/PermitRootLogin yes/PermitRootLogin no/g" /etc/ssh/sshd_config sed -i "s/#PermitEmptyPasswords no/PermitEmptyPasswords no/g" /etc/ssh/sshd_config service sshd restart
|-------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #adduser useradd whsir mkdir ~whsir/.ssh cat >> ~whsir/.ssh/authorized_keys << EOF #whsir ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEApINpHWhNHj6W3vKLw9ykSygP0a2aqM+EO5aDuLJvu7r0qn9njIjaPjNRnJ4IC05VM/zvWkBEp6/jvdPTqxW5D40by9FcWWUqCzi+OSq/9R4qAaDyP7CQnj60U5JohuRa29MaIX63UANCj4oQo3SLQttQcFUSTA3azMYLnFBHf04AFJTRndkGiO7b3qAmUhceuCOrknU83VjHXBFYeMvFeBSsEpSi5Zzii+jtgupOrOMJreUY57na0Lyy+PjC7EQsDrX8Ykfz0aoOa7f1cguK2v/ZBriwTN1SRt0MWqeNpmCfHL1xkJ0chZsFwZkhEaSoA7uQzD7xwyA9TAj8o3m3Sw== EOF chown whsir:whsir ~whsir/.ssh chmod 0755 ~whsir/.ssh/ chmod 0644 ~whsir/.ssh/authorized_keys echo "whsir ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/wuhao #sshd sed -i "s/#UseDNS yes/UseDNS no/g" /etc/ssh/sshd_config sed -i "s/#PermitRootLogin yes/PermitRootLogin no/g" /etc/ssh/sshd_config sed -i "s/PermitRootLogin yes/PermitRootLogin no/g" /etc/ssh/sshd_config sed -i "s/#PermitEmptyPasswords no/PermitEmptyPasswords no/g" /etc/ssh/sshd_config service sshd restart |