我应该如何更改当前登录用户的用户名?
当我打开Bash时,它会自动使用当前用户登录。
LxRun.exe
已被弃用,如果你从Windows商店安装Ubuntu,你需要在Windows命令提示符下运行的命令是:ubuntu config --default-user <username>
(注意:根据您安装的版本,这可能是ubuntu1804.exe
或ubuntu2004.exe
)
如果您正在使用WSL2,则已经完成。
对于WSL1,您还需要重新启动LxssManager服务。
sc stop LxssManager
sc start LxssManager
/etc/wsl.conf
中进行设置。我在下面写了一篇文章。 - Jacktose最佳答案(ubuntu config --default-user root
)目前适用于默认实例。但是如果你有多个实例,你需要稍微费点功夫:
在实例内部,以root身份创建/编辑/etc/wsl.conf
并添加以下内容:
[user]
default=username
wsl --terminate <distro name>
,然后重新启动它。
这是来自WSL存储库的问题。注册表修改的答案也在其中,但现在正式支持的解决方案是/etc/wsl.conf
。
wsl --export
和wsl --import
将我的发行版移动到另一个驱动器的情况适用。 - Constructorwsl.exe --shutdown
,而不必去PowerShell中终止。 - Lan Nguyenubuntu1604 config --default-user <username>
ubuntu1804 config --default-user <username>
如果您使用了默认的那个,则为:
ubuntu config --default-user <username>
ubuntu1804 config --default-user <username>
LxRun.exe /setdefaultuser <new_name>
wsl -l -v
wsl --terminate <distro> # <distro> from the previous command
wsl -u root -d <distro> # "-d <distro>" is only needed for non-default instances
# Now from in WSL
usermod -l newname oldname
# Optionally change the home directory name to match
usermod -d /home/doug -m doug
# Optionally change the group name to match
groupmod -n newname oldname
sudo editor /etc/wsl.conf
命令:
[user]
default=username
退出后,从PowerShell中执行wsl --terminate <distro name>
命令并重新启动WSL。
虽然 ubuntu.exe --config
方法能够起作用,但存在两个问题:
它并非总是 "ubuntu.exe"。可执行文件的名称(技术上称为“应用执行别名”)取决于您从商店中安装的版本。多年来,Ubuntu 在商店中有多个软件包。每个受支持的发行版都有一个软件包(例如 ubuntu2004.exe
等),还有一个用于“当前”版本的软件包(ubuntu.exe
)。尽管这里是 Ask Ubuntu,如果您使用其他发行版,也可以是 alpine.exe
等。
正如其他答案中指出的那样,如果您安装了多个实例(这是 WSL 的一个很好的功能),它也不起作用。它只对第一个通过商店安装的实例起作用。
当然,还有一些基于注册表的方法可以更改默认名称,但既然我们有更安全、支持的方法,就不要在这里涵盖那些方法了。 :-)
在命令提示符中
查看已安装的发行版
wslconfig /l
我有Ubuntu,并且想将默认用户设置为root
ubuntu config --default-user root
Documentation: https://docs.microsoft.com/en-us/windows/wsl/user-support
ubuntu
在cmd.exe
(Windows 10 / WSL Ubuntu 20.04)中无法识别,因此上述解决方案对我无效。
对我有效的方法(来自Windows终端):
wsl.exe -u root
sudo usermod -l newUsername oldUsername
sudo usermod -d /home/newHomeDir -m newUsername
sudo kill processId
。 - pmiguelpintogrep "DISTRIB_RELEASE=" /etc/lsb-release | grep -E [0-9]{2}.[0-9]{2} -o
ubuntu2204 config --default-user <username>
ubuntu2204
,那么将其编辑到现有答案中应该很简单。如果您想进行这个编辑,我很乐意为您批准。谢谢! - NotTheDr01ds
ubuntu2004
命令将可供使用:ubuntu2004 config --default-user robert
。 - BSalita