chsh_如何使用chsh更改Linux上的默认Shell

chsh

chsh

A stylized shell prompt on a Ubuntu-style Linux laptop system.
Fatmawati Achmad Zaenuri/Shutterstock Fatmawati Achmad Zaenuri / Shutterstock

Bash isn’t the only Linux shell. It’s easy to try out other shells, like Zsh, which is very popular. When you’ve found one you like, use the chsh command to make it your default shell. We’ll show you how.

Bash不是唯一的Linux Shell。 试用其他外壳很容易,例如Zsh ,它很受欢迎。 找到所需的外壳后,请使用chsh命令将其设置为默认外壳。 我们将向您展示如何。

为什么外壳很重要 (Why a Shell Is Important)

The shell sits between you and the operating system. It provides the environment inside a terminal window that allows you to type commands and run programs. The shell checks your input and figures out what you want. If it can perform your bidding itself, it does so. If it needs outside help, it searches the path and finds the programs that can do whatever it is you requested.

该外壳位于您和操作系统之间。 它在终端窗口内提供环境,使您可以键入命令并运行程序。 Shell检查您的输入并弄清楚您想要什么。 如果它可以自己执行出价,那么它可以执行。 如果需要外部帮助,它将搜索路径并找到可以完成您所要求的程序。

There are many different shells available on Linux. They all allow you to perform the same core tasks: explore the file system, work with files, launch programs, and run scripts. However, they each perform these tasks in their own way, and have their own special features and idiosyncrasies.

Linux上有许多不同的Shell。 它们全都允许您执行相同的核心任务:浏览文件系统,处理文件,启动程序和运行脚本。 但是,它们各自以自己的方式执行这些任务,并且具有自己的特殊功能和特质。

Shells tend to be designed by people who want things to behave in a specific way. If your thinking aligns with that designer’s, that shell might well be a good fit for you. Plus, trying a new shell on Linux is easy.

外壳往往是由希望事物以特定方式表现的人设计的。 如果您的想法与该设计师的想法一致,那么该外壳很可能适合您。 另外,在Linux上尝试新的外壳很容易。

In most Linux distributions, including Ubuntu, the default shell is bash. It does a great job and is very capable. However, another shell might offer a time-saving difference that would have a big impact on your workflow. You’ll never know if you don’t look!

在大多数Linux发行版中,包括Ubuntu, 默认的shell是bash 。 它做得很好并且非常有能力。 但是,另一个外壳程序可能会节省时间,这将对您的工作流产生重大影响。 您永远不会知道您是否不看!

一桶贝壳 (A Bucketful of Shells)

We’ve covered the different Linux shells before, but here’s a quick introduction to the most common:

之前我们已经介绍了不同的Linux shell ,但是以下是最常见的Linux的快速介绍:

  • bash: The Bourne again shell is the default in many distributions.

    bash:在许多发行版中, Bourne again shell是默认设置

  • rbash: This Restricted bash shell provides minimal functionality to the person or script running in it.

    rbash:受限制的 bash shell为其中运行的人员或脚本提供了最少的功能。

  • ash: The Almquist shell is a lighter version of bash.

    ash: Almquist外壳是bash的较浅版本。

  • dash: The Debian Alquist Shell is the default shell script in Ubuntu. While bash is the default login and interactive shell, dash is used to run system processes because it’s much lighter than bash.

    破折号: Debian Alquist Shell是Ubuntu中的默认Shell脚本 。 虽然bash是默认的登录和交互式shell,但dash用于运行系统进程,因为它比bash轻得多。

  • zsh: The Z shell is a modern take on the bash family of shells. It offers neat improvements, like command spellchecks and suggested corrections.

    zsh: Z shellbash系列shell的现代代表。 它提供了整洁的改进,例如命令拼写检查和建议的更正。

  • fish: This friendly interactive shell was written from scratch and isn’t derived from any of the other shell families. It’s intended to be user-friendly. Amongst its many other perks, fish offers suggestions for commands based on your history and the contents of the current folder, similar to predictive text.

    鱼:这个友好的交互式外壳是从头开始编写的,并不衍生自任何其他外壳系列。 目的是使用户友好。 在其众多特权中,fish会根据您的历史记录和当前文件夹的内容为命令提供建议,类似于预测文本

  • ksh: The KornShell provides a particularly strong scripting language.

    ksh: KornShell提供了一种特别强大的脚本语言。

列出已安装的外壳 (List the Installed Shells)

To see which shells are installed on your computer, use this command. It simply lists the contents of the /etc/shells file:

要查看您的计算机上安装了哪些外壳,请使用此命令。 它只是列出/etc/shells文件的内容:

cat /etc/shells
cat /etc/shells in a terminal window.

We’ve mentioned bash, dash, and rbash, but what is sh?

我们已经提到了bashdashrbash ,但是sh什么?

sh is the Thompson shell, written way back in 1971 by Ken Thompson of Bell Labs fame. It’s no longer maintained and has long since been superseded by modern shells. It’s included purely to maintain compatibility with older scripts that still have the following as their first line:

shThompson的外壳,早在1971年由Bell Labs的成名Ken Ken Thompson编写。 它不再维护,并且早已被现代外壳所取代。 纯粹是为了保持与旧脚本的兼容性,而旧脚本的第一行仍然是以下内容:

#!/bin/sh

This instructs the system to use the sh shell to execute the script. Do you really have that ancient shell on your machine, and is it being used to run your scripts? The which command will tell us which program actually runs when you type a command.

这指示系统使用sh shell执行脚本。 您的机器上确实有那个古老的外壳吗,它是否已用于运行脚本? 当您键入命令时, which命令将告诉我们哪个程序实际在运行。

Let’s see what runs when you type sh:

让我们看看键入sh时运行的内容:

which sh

This appears to find a binary. if we dig a little deeper, though, we’ll see that it’s a symbolic link that actually points to dash, the lightweight shell used to execute scripts:

这似乎是找到二进制文件。 但是,如果我们进行更深入的研究,我们会发现它实际上是指向dash的符号链接, dash是用于执行脚本的轻量级外壳:

ls -l /bin/sh
which sh in a terminal window.

That’s a neat, lightweight way to provide a safety net for scripts that expect to find sh on modern systems.

这是一个整洁的,轻量级的方式为希望找到脚本提供了一个安全网, sh现代系统。

安装另一个外壳 (Installing Another Shell)

Let’s install the fish shell and set it as the default for dave. On Ubuntu, we type the following command:

让我们安装的fish贝,并将其设置为默认的dave 。 在Ubuntu上,我们键入以下命令:

sudo apt-get install fish
sudo apt-get install fish in a terminal window.

On Manjaro, use pacman:

在Manjaro上,使用pacman

sudo pacman -Sy fish
sudo pacman -Sy fish in a terminal window.

On Fedora, type the following:

在Fedora上,键入以下内容:

sudo dnf install fish
sudo dnf install fish in a terminal window.

When the installation is complete, you can check the installed shells once more:

安装完成后,您可以再次检查已安装的外壳:

cat /etc/shells
cat /etc/shells in a terminal window.

Our new shell appears as /usr/bin/fish.  Take note of that path—you’ll need it shortly.

我们的新外壳显示为/usr/bin/fish 。 请注意该路径-您很快就会需要它。

$ SHELL环境变量 (The $SHELL Environment Variable)

The $SHELL environment variable holds the name of your current shell. We can check which one it’s set to with echo:

$SHELL 环境变量保存当前shell的名称。 我们可以使用echo检查设置为哪一个:

echo $SHELL

Let’s start the fish shell:

让我们开始fish贝:

fish

Now, let’s check again what the $SHELL environment variable says:

现在,让我们再次检查$SHELL环境变量的含义:

echo $SHELL
echo $SHELL in a terminal window.

The first time we use echo $SHELL, we’re in the bash shell. The environment variable holds the path to the bash executable, /bin/bash.

第一次使用echo $SHELL ,我们就在bash shell中。 环境变量保存bash可执行文件/bin/bash的路径。

When we launch the fish shell, we get a friendly welcome message and the command prompt changes. What might be surprising is the  $SHELL environment still holds the path to the bash executable, /bin/bash. That’s okay—this is normal.

当我们推出fish贝,我们得到了一个友好的欢迎消息和命令提示符的变化。 令人惊讶的是, $SHELL环境仍然包含bash可执行文件/bin/bash的路径。 没关系-这很正常。

When you launch a new shell (or any other program), it inherits the environment of the parent shell. So, the fish shell inherits the global and exported environment variables from the bash shell. Because the value in the $SHELL environment variable hasn’t been changed, it has the same value in the fish shell that it did in the bash shell.

启动新外壳程序(或任何其他程序)时,它将继承父外壳程序的环境。 因此, fish shell从bash shell继承了全局和导出的环境变量。 因为$SHELL环境变量中的值未更改,所以它在fish shell中的值与在bash shell中的值相同。

We’re running fish like any other program. We can also use exit to exit the fish shell. It closes like any other program, and we return to the bash shell.

我们像其他程序一样运行fish 。 我们也可以使用exit ,退出fish贝。 它像其他任何程序一样关闭,我们返回到bash shell。

That’s great for trying out new shells, seeing what they can do, and whether you get along with them. You can explore before you make the jump and adopt one as your go-to shell.

这对于尝试新的外壳,查看它们可以做什么以及是否与它们相处非常好。 您可以在进行跳转之前先进行探索,然后将其中一个作为您的入门指南。

If you decide to make the fish—or any other shell—your default, you’ll need to use the chsh command.

如果您决定将fish或任何其他贝壳)作为默认设置,则需要使用chsh命令。

chsh命令 (The chsh Command)

The chsh command allows you to change your default shell. The trick is being aware that it allows you to change both the default login and default interactive shells. You might want to change one or the other, or both.

chsh命令允许您更改默认外壳程序。 诀窍在于,它允许您更改默认登录名和默认交互式外壳。 您可能要更改一个或另一个,或两者都更改。

Whenever you log in to obtain a command prompt, you use the shell configured to be your login shell. When you’re already logged in and open a terminal window, you use the shell configured to be your interactive shell. These can either be the same or different shells.

无论何时登录以获得命令提示符,都将使用配置为登录shell的shell。 登录并打开终端窗口后,请使用配置为交互式外壳程序的外壳程序。 这些可以是相同或不同的外壳。

To set your login shell, use chsh with no parameters:

要设置您的登录shell,请使用不带参数的chsh

chsh
chsh in a terminal window.

You’re prompted for your password. Then, you must type the path to the new shell and hit Enter.

系统提示您输入密码。 然后,您必须键入新外壳的路径,然后按Enter。

If we make a remote connection to this test computer from another, we’ll find ourselves in the fish shell once we’ve logged in.

如果我们从另一本次测试计算机的远程连接,我们会在发现自己fish贝,一旦我们已经记录下来。

SSH connection showing the fish shell as the login shell, in a terminal window.

To change your interactive shell use chsh with the -s (shell) option. Pass the path to the new shell on the command line:

要更改交互式外壳,请使用带有-s (外壳)选项的chsh 。 在命令行中将路径传递到新的shell:

chsh -s /usr/bin/fish
chsh -s /usr/bin/fish in a terminal window.

You’re prompted for your password and returned to the command prompt of your current shell. You need to log out and back in for the change to take effect. When you do, you’ll see the greeting and the fish shell command prompt.

系统将提示您输入密码,并返回到当前Shell的命令提示符。 您需要注销然后重新登录,以使更改生效。 当你这样做,你会看到的问候和fish Shell命令提示符。

The $SHELL environment variable now holds the path to your new default shell:

$SHELL环境变量现在保存到新的默认Shell的路径:

echo $SHELL
echo $SHELL in a terminal window.

更改另一个用户帐户的外壳 (Changing Another User Account’s Shell)

If you have root privileges and can use sudo, you can change the shells of other user accounts. The command is the same as before, with the addition of that person’s username added to the command line:

如果您具有root特权并可以使用sudo ,则可以更改其他用户帐户的外壳。 该命令与之前的命令相同,只是在命令行中添加了该用户的用户名:

sudo chsh -s /usr/bin/fish mary
sudo chsh -s /usr/bin/fish mary in a terminal window.

When mary next logs in, she’ll see the new shell when she opens a terminal window.

mary next登录时,她在打开终端窗口时将看到新的外壳。

user mary at the fish shell command prompt, in a terminal window.

每个人都有最爱 (Everybody Has a Favorite)

As long as you’re comfortable with your choice of shell, and it works for you, that’s great! Just remember, it must be able to run common scripts, such as installation routines. For the shells mentioned here, this shouldn’t be a problem.

只要您对选择的外壳感到满意并且对您有用,那就太好了! 请记住,它必须能够运行常见的脚本,例如安装例程。 对于这里提到的shell,这应该不是问题。

Of course, you can also download and install a new shell, and take it for a test drive without making any configuration changes to your computer. When you’re ready to tie the knot, chsh will perform the ceremony for you.

当然,您也可以下载并安装新的外壳,并将其用于测试驱动器,而无需对计算机进行任何配置更改。 当您准备好打结时, chsh将为您执行仪式。

翻译自: https://www.howtogeek.com/669835/how-to-change-your-default-shell-on-linux-with-chsh/

chsh

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值