Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
qianjr2002
ShellLearning
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
ShellLearning
/
adduser_planA.sh
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
51 lines (43 loc) · 1.52 KB
main
Breadcrumbs
ShellLearning
/
adduser_planA.sh
Copy path
Top
File metadata and controls
Code
Blame
51 lines (43 loc) · 1.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
# 检查是否为 root 用户
if [ "$(id -u)" -ne 0 ]; then
echo "请使用 root 用户执行此脚本。"
exit 1
fi
# 检查参数数量
if [ "$#" -ne 3 ]; then
echo "用法: $0 <username> <keyname> <administername>"
echo "username: the name of new added user; keyname: the file name of key file; administername: the name of you operator"
exit 1
fi
# 设置变量
USERNAME=$1
KEYNAME=$2
ADMINISTER=$3
PASSWORD="xxxxxxx"
DEST_DIR="/home/$ADMINISTER/.ssh"
GROUP="xxxxx"
# 创建用户并加入 "speech" 组
useradd -m -s /bin/bash -G "$GROUP" "$USERNAME"
echo "$USERNAME:$PASSWORD" | chpasswd
echo "用户 $USERNAME 已创建,并加入 $GROUP 组,默认密码为 $PASSWORD"
# 设置主目录的权限
chmod 750 "/home/$USERNAME"
chgrp "$GROUP" "/home/$USERNAME"
echo "已将 /home/$USERNAME 的权限设置为 750,并将组设置为 $GROUP。"
# 切换到新用户生成密钥对并配置 authorized_keys
USER_HOME=$(eval echo "~$USERNAME")
sudo -u "$USERNAME" bash <<EOF
mkdir -p ~/.ssh
chmod 700 ~/.ssh
ssh-keygen -t rsa -f ~/.ssh/$KEYNAME -N "" -q
cat ~/.ssh/$KEYNAME.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
EOF
echo "密钥对已生成并配置 authorized_keys。"
# 切换回 root,将生成的私钥复制到目标目录并设置权限
mkdir -p "$DEST_DIR"
cp "$USER_HOME/.ssh/$KEYNAME" "$DEST_DIR"
chmod 777 "$DEST_DIR/$KEYNAME"
echo "私钥已复制到 $DEST_DIR,并设置权限为 777。"
echo "脚本执行完成。用户 $USERNAME 已创建,公私钥已生成并配置。"
You can’t perform that action at this time.