Linux chsh 命令初学者教程(5 个示例)
在此页
- Linux chsh 命令
- Q1。如何使用chsh?
- Q2。如何使用 chsh 更改登录 shell?
- Q3。如何更改其他用户的登录 shell?
- Q4。如何让 chsh 以非交互模式运行?
- Q5。如何查看系统上可用的登录 shell 列表?
- 结论
bash shell 是 Linux 中使用最广泛的登录 shell 之一。但也存在其他 shell,您可以将它们用于命令行工作(当然,除非您的工作有特定要求)。在本文中,我们将讨论一个名为 chsh 的工具,它可以让您切换到与当前 shell 不同的登录 shell。
但在此之前,值得一提的是,本教程中的所有示例都已在 Ubuntu 18.04 LTS 机器上进行了测试。
Linux chsh 命令
正如在上面的介绍中已经解释的那样,chsh 命令可以让你改变你的登录 shell。以下是它的语法:
chsh [options] [LOGIN]
下面是工具手册页关于它的内容:
The chsh command changes the user login shell. This determines the name
of the user's initial login command. A normal user may only change the
login shell for her own account; the superuser may change the login
shell for any account.
以下是一些 Q&A 风格的示例,应该可以让您对 chsh 命令的工作原理有一个很好的了解。
Q1。如何使用chsh?
基本用法相当简单——只需执行无参数的 chsh 命令。系统会要求您输入登录密码,成功输入密码后,您可以选择更改登录 shell。
chsh

请注意,默认情况下,当前用户的登录 shell 已更改。
Q2。如何使用 chsh 更改登录 shell?
如您所料,只需输入新值(参见上一示例中的屏幕截图),您的 shell 就会更改为新值。
例如,我输入 /bin/sh 作为新值。

要验证更改,您可以再次运行 chsh 命令。方括号中显示的值是当前 shell。所以在我的例子中,当前的 shell 变成了 /bin/sh:

Q3.如何更改其他用户的登录 shell?
为此,只需指定用户名作为 chsh 命令的输入。
chsh [username]
例如,要更改 root 的登录 shell,请按以下方式使用 chsh:
chsh root

Q4.如何让 chsh 以非交互模式运行?
为此使用 -s 命令行选项,这需要将 shell 名称作为输入传递给它。
例如:
chsh -s /bin/sh
这样一来,当前用户的登录 shell 将以非交互方式更改为 /bin/sh。
Q5.如何查看系统上可用的登录 shell 列表?
要快速查看系统中可用的 shell 列表,只需 cat /etc/shells 文件即可。
例如,这是在我的系统上产生的输出:
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/bin/rbash
/bin/dash
笔记:
The only restriction placed on the login shell is that the command name
must be listed in /etc/shells, unless the invoker is the superuser, and
then any value may be added. An account with a restricted login shell
may not change her login shell. For this reason, placing /bin/rsh in
/etc/shells is discouraged since accidentally changing to a restricted
shell would prevent the user from ever changing her login shell back to
its original value.
结论
所以你看,chsh 命令是命令行用户应该知道的一个重要工具。在本教程中,我们讨论了该工具提供的大部分命令行选项。要了解有关 chsh 的更多信息,请访问其手册页。