简体   繁体   English

如何让我的 bash 脚本为给定用户创建 ssh 密钥对

[英]How can I make my bash script create an ssh key pair for a given user

I am new to bash scripting.我是 bash 脚本的新手。 I wrote a script that can create groups and users which works just fine, but my challenge now is how do I make the script create an ssh key pair for a particular user.我编写了一个脚本,可以创建工作正常的组和用户,但我现在的挑战是如何让脚本为特定用户创建 ssh 密钥对。 From my script, it stopped working immediately after you switch to that user and it doesn't proceed to create the ssh key pair.在我的脚本中,它在您切换到该用户后立即停止工作,并且不会继续创建 ssh 密钥对。

Below is my script.下面是我的脚本。

    for group in admin support engineering
    do
            sudo groupadd $group
            sudo useradd -m -s /bin/bash achebeh_${group}
    done
    sudo passwd achebeh_admin
    sudo su achebeh_admin
    ssh-keygen -t rsa

So please how can I go about creating an ssh pair for the achebeh_admin user using this script.那么,我该如何使用此脚本为 achebeh_admin 用户创建 ssh 对。 I am open to learn.我愿意学习。 Please this is my firs script after following a tutorial course.请这是我学习完教程课程后的第一个脚本。

@Achebe-peter If I got your requirements correctly from your short description, this will do the job for you. @Achebe-peter 如果我从您的简短描述中正确地得到了您的要求,这将为您完成工作。

Note: Try this script in a test environment at your own risk!注意:在测试环境中尝试此脚本,风险自负!

#!/bin/bash
### Configuration Parameters Start ###
## The username that doesn't exist and you want to create.
user_name_prefix='testuser'

## The groups array that doesn't exist and you want to create and assign them to your user.
groups=(testadmin testsupport testengineering)

## SSH-key lenght
ssh_key_lenght='2048'
### Configuration Parameters End ###


for group in ${groups[@]} ;do
  # Set username containing the prefix and group name
  user="${user_name_prefix}_${group}"

  # create such user if not exist
  getent passwd ${user} &>/dev/null
  if [ "$?" -ne 0 ] ;then
    sudo useradd -m -s /bin/bash "${user}"

    echo -e "\nType password for: ${user}"
    sudo passwd ${user}
  fi

  # Create '.ssh' directory in user's home directory
  if ! [ -d /home/${user}/.ssh ] ;then
    sudo mkdir /home/${user}/.ssh
  fi

  # Generate ssh-key pair and move them to correspondig user's '.ssh/' dir.
  ssh_file_name="./${user}_ssh"
  (
  echo "${ssh_file_name}"
  echo ""
  echo "" 
  ) | ssh-keygen -t rsa -b ${ssh_key_lenght}

  sudo mv -i "${ssh_file_name}" "${ssh_file_name}.pub" /home/${user}/.ssh/
  sudo chown ${user}:${user} /home/${user}

  # Create the groups (if does not exist)
  getent group ${group} &>/dev/null
  if [ "$?" -ne 0 ] ;then
    sudo groupadd ${group}
  fi

  # Assign relevant group to the new user
  sudo usermod -aG ${group} ${user}
done

exit 0
Tested in经测试
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

PS. PS。 Please vote up my answer and mark it as the correct answer if it satisfies your requirements.如果满足您的要求,请投票给我的答案并将其标记为正确答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的脚本在 ssh 断开连接后继续运行而不强制它进入后台? - How can I make my script keep running after a ssh disconnect without forcing it to be backgrounded? 从以root身份运行的bash脚本向用户添加ssh密钥 - adding ssh key to a user from a bash script being run as root 如何在旧版本的 Bash 上测试我的 Bash 脚本? - How can I test my Bash script on older versions of Bash? 如何获得实时值以在BASH中专门制作我自己的脚本,如附件所示 - How can i get the realtime values to make my own script in BASH specially as shown the attachment 在执行bash脚本期间如何更改用户身份,并继续以新用户身份运行命令? - How can I change users in during my bash script excecution and continue running commands with the new user? 以root用户身份运行bash脚本后,如何让脚本自动强制我的用户退出root用户? - After running a bash script as root user, how can I have the script automatically force my user to exit root? 如何使用ssh使我的自定义shell工作? - How can I make my custom shell work with ssh? 如何从bash脚本知道用户是否突然关闭ssh会话 - How to know from a bash script if the user abruptly closes ssh session 无法从我的 bash 脚本执行 ssh IPAddressA -l user "ssh -l IPAddressB ls" - Trouble executing ssh IPAddressA -l user "ssh -l IPAddressB ls" from my bash script 如何使bash脚本不区分大小写? - How can I make a bash script case insensitive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM