powershell - 如何使用Powershell中的命令将sudo用户添加到WSL?

标签 powershell passwords sudo ubuntu-18.04 windows-subsystem-for-linux

我正在尝试使用Powershell脚本自动化WSL设置。
我在Windows 10系统中为WSL安装了Ubuntu 18.04发行版。

我想在Powershell中运行以下命令以在WSL中使用密码tec设置sudo用户1

Ubuntu1804 run useradd -m tec
Ubuntu1804 run usermod --password $(echo 1 | openssl passwd -1 -stdin) tec
Ubuntu1804 run usermod -aG sudo tec

问题是,第二条命令未正确设置密码。试图成为root用户最终以
 sudo: 3 incorrect password attempts

如果我通过从Windows命令提示符下键入bash进入WSL,然后执行以下命令:
usermod --password $(echo 1 | openssl passwd -1 -stdin) tec

它可以正确设置密码。之后,可以在出现提示时输入密码1成为root用户。

谁能帮助我找到Powershell脚本出了什么问题?

最佳答案

我得到了答案。
只需在第二个命令中用单引号将加密的密码包装起来就可以了。

Ubuntu1804 run usermod --password '$(echo 1 | openssl passwd -1 -stdin)' tec

现在,powershell命令为WSL创建一个带有密码tec的sudo用户1看起来像:
# Creates the user
Ubuntu1804 run useradd -m tec

# sets password for user
Ubuntu1804 run usermod --password '$(echo 1 | openssl passwd -1 -stdin)' tec

# adds user to the sudo group
Ubuntu1804 run usermod -aG sudo tec

关于powershell - 如何使用Powershell中的命令将sudo用户添加到WSL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55254964/

相关文章:

passwords - 创新设置: How to create password wizard page only if component "X" selected

c# - 带有数字或特殊字符的密码复杂性正则表达式

java - 如何从java中受密码保护的文件夹下载文件

python - Python 中 sudo 自动传递密码

macos - 在 BASH 脚本中扩展 SUDO 时间戳

powershell - 将同名文件移动到匹配的命名文件夹

powershell - 过滤数据部分,包括起始行和结束行 - PowerShell

linux - 如何在 grep 中使用 sudoers 和正则表达式

powershell - 获取对给定 PowerShell 模块的所有引用

powershell - 使用 invoke-restmethod 时设置主机头