您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在Linux中,您可以使用Ruby脚本来管理用户认证
sudo apt-get update
sudo apt-get install ruby
user_management.rb
的新Ruby脚本文件:touch user_management.rb
user_management.rb
文件并添加以下代码:#!/usr/bin/env ruby
def add_user(username, password)
system("useradd", "-m", username)
system("echo", "'#{username}:#{password}'", "|", "chpasswd")
puts "User '#{username}' added with password '#{password}'"
end
def delete_user(username)
system("userdel", "-r", username)
puts "User '#{username}' deleted"
end
def change_password(username, new_password)
system("echo", "'#{username}:#{new_password}'", "|", "chpasswd")
puts "Password for user '#{username}' changed to '#{new_password}'"
end
def list_users
users = `cat /etc/passwd | cut -d: -f1`.split("\n")
puts "Users:"
users.each { |user| puts "- #{user}" }
end
action = ARGV[0]
username = ARGV[1]
password = ARGV[2]
case action
when "add"
add_user(username, password)
when "delete"
delete_user(username)
when "change_password"
change_password(username, password)
when "list"
list_users
else
puts "Usage: #{$0} <add|delete|change_password|list> [username] [password]"
end
chmod +x user_management.rb
./user_management.rb add myuser mypassword
要删除用户,运行:
./user_management.rb delete myuser
要更改用户密码,运行:
./user_management.rb change_password myuser newpassword
要列出所有用户,运行:
./user_management.rb list
请注意,这个脚本需要root权限才能运行,因此在运行脚本时,您可能需要使用sudo
。同时,确保在生产环境中使用更安全的方法来存储和传递密码。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。