Chapter 13. User Environments(第13章 用户环境)

This book’s primary focus is on the Linux system that normally lies underneath server processes and interactive user sessions. But eventually, the system and the user have to meet somewhere. Startup files play an important role at this point, because they set defaults for the shell and other interactive programs. They determine how the system behaves when a user logs in.

这本书的主要关注点是位于服务器进程和交互式用户会话之下的Linux系统。

但最终,系统和用户必须在某个地方相遇。

启动文件在这一点上起着重要作用,因为它们为shell和其他交互式程序设置了默认值。

它们决定了用户登录时系统的行为方式。

Most users don’t pay close attention to their startup files, only touching them when they want to add something for convenience, such as an alias. Over time, the files become cluttered with unnecessary environment variables and tests that can lead to annoying (or quite serious) problems.

大多数用户在启动文件上并不关注,只有在想要添加一些方便的内容时才会涉及,比如别名。

随着时间的推移,这些文件会积累一些不必要的环境变量和测试,可能会导致烦人(或相当严重的)问题。

If you’ve had your Linux machine for a while, you may notice that your home directory accumulates a bafflingly large array of startup files over time. These are sometimes called dot files because they nearly always start with a dot (.). Many of these are automatically created when you first run a program, and you’ll never need to change them. This chapter primarily covers shell startup files, which are the ones you’re most likely to modify or rewrite from scratch. Let’s first look at how much care you need to take when working on these files.

如果你使用Linux机器已经有一段时间了,你可能会注意到你的主目录随着时间的推移积累了大量令人困惑的启动文件。

它们有时被称为点文件,因为它们几乎总是以点(.)开头。

其中许多是在你第一次运行程序时自动创建的,你永远不需要更改它们。

本章主要介绍了shell启动文件,这些文件是你最有可能修改或从头开始重写的文件。

让我们首先看看在处理这些文件时需要注意多少。

13.1 Guidelines for Creating Startup Files(创建创业文件的指南)

When designing startup files, keep the user in mind. If you’re the only user on a machine, you don’t have much to worry about because errors only affect you and they’re easy enough to fix. However, if you’re creating startup files meant to be the defaults for all new users on a machine or network, or if you think that someone might copy your files for use on a different machine, your task becomes considerably more difficult. If you make an error in a startup file for 10 users, you might end up fixing this error 10 times.

在设计启动文件时,要考虑到用户的需求。

如果你是机器上唯一的用户,那么你就没什么可担心的,因为错误只会影响到你自己,并且很容易修复。

但是,如果你正在创建启动文件,这些文件将成为机器或网络上所有新用户的默认设置,或者你认为有人可能会复制你的文件在另一台机器上使用,那么你的任务就会变得更加困难。

如果你在为10个用户创建启动文件时犯了一个错误,那么你可能需要修复这个错误10次。

Keep two essential goals in mind when creating startup files for other users:

在为其他用户创建启动文件时,要牢记两个基本目标:

o Simplicity. Keep the number of startup files small, and keep the files as small and simple as possible so that they are easy to modify but hard to break. Each item in a startup file is just one more thing that can break.
o Readability. Use extensive comments in files so that the users get a good picture of what each part of a file does.

  • 简单性。保持启动文件的数量较少,使文件尽可能简单,这样它们就容易修改但不容易出错。启动文件中的每一项都可能导致出错。
  • 可读性。在文件中使用大量注释,以便用户清楚了解文件的每个部分的作用。

    13.2 When to Alter Startup Files(何时修改启动文件)

Before making a change to a startup file, ask yourself whether you really should be making that change. Here are some good reasons for changing startup files

在对启动文件进行更改之前,请问自己是否真的应该进行这种更改。以下是更改启动文件的一些好理由:

o You want to change the default prompt.
o You need to accommodate some critical locally installed software. (Consider using wrapper scripts first, though.)
o Your existing startup files are broken. If everything in your Linux distribution works, be careful. Sometimes the default startup files interact with other files in /etc.:
o Your existing startup files are broken.

  • 您想要更改默认提示符。
  • 您需要适应一些关键的本地安装软件。(不过,首先考虑使用包装脚本。)
  • 您现有的启动文件已损坏。如果您的Linux发行版的一切正常,请小心。有时默认的启动文件会与/etc目录中的其他文件相互作用。
  • 您现有的启动文件已损坏。

If everything in your Linux distribution works, be careful. Sometimes the default startup files interact with other files in /etc.

如果您的Linux发行版的一切正常,请小心。

有时默认的启动文件会与/etc目录中的其他文件相互作用。

That said, you probably wouldn’t be reading this chapter if you weren’t interested in changing the defaults, so let’s examine what’s important.

话虽如此,如果您对更改默认设置不感兴趣,您可能不会阅读本章,所以让我们来看看什么是重要的。

13.3 Shell Startup File Elements(Shell 启动文件元素)

What goes into a shell startup file? Some things might seem obvious, such as the path and a prompt setting. But what exactly should be in the path, and what does a reasonable prompt look like? And how much is too much to put in a startup file?

shell 启动文件包含哪些内容?有些东西看似显而易见,比如路径和提示设置。

但路径中到底应该包含哪些内容,合理的提示符应该是什么样的?在启动文件中放多少内容才算多?

The next few sections discuss the essentials of a shell startup file—from the command path, prompt, and aliases through the permissions mask.

接下来的几节将讨论 shell 启动文件的基本要素--从命令路径、提示符和别名到权限掩码。

13.3.1 The Command Path(命令路径)

The most important part of any shell startup file is the command path. The path should cover the directories that contain every application of interest to a regular user. At the very least, the path should contain these components, in order:

任何shell启动文件中最重要的部分是命令路径。

路径应该包括包含对常规用户有兴趣的每个应用程序的目录。

至少,路径应按照以下顺序包含以下组件:

/usr/local/bin
/usr/bin
/bin

This order ensures that you can override standard default programs with site-specific variants located in /usr/local.

这个命令确保您可以用位于/usr/local目录下的特定于站点的变体覆盖标准的默认程序。

Most Linux distributions install executables for nearly all packaged software in /usr/bin. There are occasional differences, such as putting games in /usr/games and graphical applications in a separate location, so check your system’s defaults first. And make sure that every general-use program on the system is available through one of the directories listed above. If not, your system is probably getting out of control. Don’t change the default path in your user environment to accommodate a new software installation directory. A cheap way to accommodate separate installation directories is to use symbolic links in /usr/local/bin.

大多数Linux发行版将几乎所有打包软件的可执行文件安装在/usr/bin目录下。

有时会有一些区别,比如将游戏放在/usr/games目录下,将图形应用程序放在其他位置,所以请先检查系统的默认设置。

确保系统上的每个通用程序都可以通过上述目录之一访问。

如果不能,那么您的系统可能已经失控了。

不要更改用户环境中的默认路径以适应新的软件安装目录。

一个简单的方法是在/usr/local/bin目录中使用符号链接来容纳独立的安装目录。

Many users use a bin directory of their own to store shell scripts and programs, so you may want to add this to the front of the path:

许多用户使用自己的bin目录来存储shell脚本和程序,所以您可能希望将其添加到路径的前面:

$HOME/bin

NOTE A newer convention is to place binaries in $HOME/.local/bin.

注意 新的惯例是将二进制文件放在 $HOME/.local/bin。

If you’re interested in systems utilities (such as traceroute, ping, and lsmod), add the sbin directories to your path:

如果你对系统实用程序(如 traceroute、ping 和 lsmod)感兴趣,请在路径中添加 sbin 目录:

/usr/local/sbin
/usr/sbin
/sbin

Adding Dot (.) to the Path(在路径中添加点 (.))

There is one small but controversial command path component to discuss: the dot. Placing a dot (.) in your path allows you to run programs in the current directory without using ./ in front of the program name. This may seem convenient when writing scripts or compiling programs, but it’s a bad idea for two reasons:

有一个小但有争议的命令路径组件需要讨论:点号(.)。

在路径中加入一个点号(.)可以让你在当前目录中运行程序,而无需在程序名称前加上./。

这在编写脚本或编译程序时可能很方便,但出于以下两个原因,这是一个不好的主意:

o It can be a security problem. You should never put a dot at the front of the path. Here’s an example of what can happen: An attacker could put a Trojan horse named ls in an archive distributed on the Internet. Even if a dot were at the end of the path, you’d still be vulnerable to typos such as sl or ks.
o It is inconsistent and can be confusing. A dot in a path can mean that a command’s behavior will change according to the current directory.

o 它可能会造成安全问题。你绝不应该在路径的开头放置一个点号。

下面是一个可能发生的例子:攻击者可以在互联网上分发一个名为ls的特洛伊木马。

即使路径的末尾有一个点号,你仍然容易受到类似于sl或ks的拼写错误的影响。

o 它不一致且容易引起混淆。路径中的点号可能意味着命令的行为会根据当前目录而改变。

13.3.2 The Manual Page Path(手册页面路径)

The traditional manual page path was determined by the MANPATH environment variable, but you shouldn’t set it because doing so overrides the system defaults in /etc/manpath.config.

传统的手动页面路径是由MANPATH环境变量确定的,但是您不应该设置它,因为这样会覆盖/etc/manpath.config中的系统默认值。

13.3.3 The Prompt(提示)

Experienced users tend to avoid long, complicated, useless prompts. In comparison, many administrators and distributions drag everything into a default prompt. Your choice should reflect your users’ needs; place the current working directory, hostname, and username in the prompt if it really helps.

Above all, avoid characters that mean something significant to the shell, such as these:

有经验的用户往往会避免使用冗长、复杂和无用的提示符。

相比之下,许多管理员和发行版会将所有内容都放入默认提示符中。

您的选择应该反映出用户的需求;只有在确实有帮助时,才将当前工作目录、主机名和用户名放入提示符中。

最重要的是,避免使用对shell来说具有重要意义的字符,比如以下字符:

{ } = & < >

NOTE Take extra care to avoid the > character, which can cause erratic, empty files to appear in your current directory if you accidentally copy and paste a section of your shell window (recall that > redirects output to a file).

注意:要格外小心避免使用大于号(>)字符,因为如果你在shell窗口中意外复制粘贴一部分内容,可能会导致在当前目录中出现不稳定的空文件(请回忆一下,> 用于将输出重定向到文件)。

Even a shell’s default prompt isn’t ideal. For example, the default bash prompt contains the shell name and version number.

即使是shell的默认提示符也不是最理想的。例如,默认的bash提示符包含了shell的名称和版本号。

This simple prompt setting for bash ends with the customary $ (the traditional csh prompt ends with %):

对于bash的这个简单提示符设置以$符号结尾(传统的csh提示符以%结尾)。

PS1='\u\$ '

The \u is a substitution for the current username (see the PROMPTING section of the bash(1) manual page). Other popular substitutions include the following:

\u 是当前用户名的替代符号(请参阅 bash(1) 手册页的 PROMPTING 部分)。其他常用的替代包括以下内容:

o \h The hostname (the short form, without domain names)
o \! The history number
o \w The current directory. Because this can become long, you can limit the display to just the final component with \W.
o $ $ if running as a user account, # if root

  • \h 主机名(短格式,不包含域名)
  • \! 历史记录编号
  • \w 当前目录。由于路径可能很长,您可以使用 \W 来仅显示最后一个组件。
  • $ 如果作为用户账户运行,则为 $,如果是 root 则为 #。

    13.3.4 Aliases(别名)

Among the stickier points of modern user environments is the role of aliases, a shell feature that substitutes one string for another before executing a command. These can be efficient shortcuts that save some typing. However, aliases also have these drawbacks:

在现代用户环境中,一个较为棘手的问题是别名的角色,别名是一种在执行命令之前用一个字符串替换另一个字符串的 shell 功能。

这些可以是高效的快捷方式,可以节省一些输入。

然而,别名也存在以下缺点:

o It can be tricky to manipulate arguments.
o They are confusing; a shell’s built-in which command can tell you if something is an alias, but it won’t tell you where it was defined.
o They are frowned upon in subshells and noninteractive shells; they do not work in other shells.

  • 操纵参数可能有些棘手。
  • 它们会让人感到困惑;shell 内置的 which 命令可以告诉你某个东西是否是别名,但它不会告诉你别名是在哪里定义的。
  • 在子 shell 和非交互式 shell 中,别名不受欢迎;它们在其他 shell 中不起作用。

Given these disadvantages, you should probably avoid aliases whenever possible because it’s easier to write a shell function or an entirely new shell script. Modern computers can start and execute shells so quickly that the difference between an alias and an entirely new command should mean nothing to you.

考虑到这些缺点,尽可能避免使用别名,因为编写一个 shell 函数或者一个全新的 shell 脚本会更容易。

现代计算机启动和执行 shell 的速度如此之快,以至于别名和全新命令之间的区别对你来说应该毫无意义。

That said, aliases do come in handy when you wish to alter a part of the shell’s environment. You can’t change an environment variable with a shell script, because scripts run as subshells. (You can also define shell functions to perform this task.)

话虽如此,当你希望修改 shell 环境的一部分时,别名确实会派上用场。

你无法通过 shell 脚本更改环境变量,因为脚本会作为子 shell 运行。(你也可以定义 shell 函数来执行此任务。)

13.3.5 The Permissions Mask(权限掩码)

As described in Chapter 2, a shell’s built-in umask (permissions mask) facility sets your default permissions. You should run umask in one of your startup files to make certain that any program you run creates files with your desired permissions. The two reasonable choices are these:

如第二章所述,shell的内置umask(权限掩码)功能用于设置默认权限。

您应该在启动文件中运行umask,以确保您运行的任何程序都以您所需的权限创建文件。有两个合理的选择:

o 077 This mask is the most restrictive permissions mask because it doesn’t give any other users access to new files and directories. This is often appropriate on a multi-user system where you don’t want other users to look at any of your files. However, when set as the default, it can sometimes lead to problems when your users want to share files but don’t understand how to set permissions correctly. (Inexperienced users have a tendency to set files to a world-writable mode.)
o 022 This mask gives other users read access to new files and directories. This can be important on a singleuser system because many daemons that run as pseudo-users are not be able to see files and directories created with the more restrictive 077 umask.

o 077 这个掩码是最严格的权限掩码,因为它不允许其他用户访问新文件和目录。

这在多用户系统中通常是适用的,因为您不希望其他用户查看您的任何文件。

然而,当设置为默认值时,当用户想要共享文件但不了解如何正确设置权限时,可能会导致问题。

(经验不足的用户有将文件设置为可写入模式的倾向。)

o 022 这个掩码允许其他用户对新文件和目录进行读取访问。这在单用户系统中可能很重要,因为许多以伪用户身份运行的守护进程无法看到使用更严格的077 umask创建的文件和目录。

NOTE Certain applications (especially mail programs) override the umask, changing it to 077 because they feel that their files are the business of no one but the file owner.

注意 某些应用程序(特别是邮件程序)会覆盖umask,将其更改为077,因为它们认为它们的文件只属于文件所有者。

13.4 Startup File Order and Examples(启动文件顺序和示例)

Now that you know what to put into shell startup files, it’s time to see some specific examples. Surprisingly, one of the most difficult and confusing parts of creating startup files is determining which of several startup files to use. The next sections cover the two most popular Unix shells: bash and tcsh.

既然你已经知道要将什么内容放入shell启动文件中,现在是时候看一些具体的示例了。

令人惊讶的是,创建启动文件中最困难和令人困惑的部分之一是确定要使用哪个启动文件中的几个。

接下来的部分将介绍两种最流行的Unix shell:bash和tcsh。

13.4.1 The bash Shell

In bash, you can choose from the startup filenames .bash_profile, .profile, .bash_login, and .bashrc. Which one is appropriate for your command path, manual page path, prompt, aliases, and permissions mask? The answer is that you should have a .bashrc file accompanied by a .bash_profile symbolic link pointing to .bashrc because there are a few different kinds of bash shell instance types.

在bash中,你可以从以下启动文件名中选择:.bash_profile、.profile、.bash_login和.bashrc。

哪一个适合你的命令路径、手册页路径、提示符、别名和权限掩码?

答案是你应该有一个伴随着一个指向.bashrc的.bash_profile符号链接的.bashrc文件,因为有几种不同类型的bash shell实例。

The two main shell instance types are interactive and noninteractive, but of those, only interactive shells are of interest because noninteractive shells (such as those that run shell scripts) usually don’t read any startup files. Interactive shells are the ones that you use to run commands from a terminal, such as the ones you’ve seen in this book, and they can be classified as login or non-login.

主要的两种shell实例类型是交互式和非交互式,但其中只有交互式shell是感兴趣的,因为非交互式shell(例如运行shell脚本的shell)通常不会读取任何启动文件。

交互式shell是你用来从终端运行命令的那些,就像你在本书中看到的那些,它们可以被分类为登录或非登录。

Login Shells(登陆 Shell)

Traditionally, a login shell is what you get when you first log in to a system with the terminal using a program such as /bin/login. Logging in remotely with SSH also gives you a login shell. The basic idea is that the login shell is an initial shell. You can tell if a shell is a login shell by running echo $0; if the first character is a -, the shell’s a login shell.

传统上,登录 shell 是当你使用类似 /bin/login 的程序在终端上首次登录系统时所得到的。

通过 SSH 远程登录也会给你一个登录 shell。

基本的概念是,登录 shell 是一个初始的 shell。

你可以通过运行 echo $0 来判断一个 shell 是否是登录 shell;

如果第一个字符是 -,那么这个 shell 就是一个登录 shell。

When bash runs as a login shell, it runs /etc/profile. Then it looks for a user’s .bash_profile, .bash_login, and .profile files, running only the first one that it sees.

当 bash 作为登录 shell 运行时,它会运行 /etc/profile。然后它会寻找用户的 .bash_profile、.bash_login 和 .profile 文件,并且只运行它所找到的第一个文件。

As strange as it sounds, it’s possible to run a noninteractive shell as a login shell to force it to run startup files. To do so, start the shell with the -l or --login option

尽管听起来很奇怪,但也有可能将一个非交互式的 shell 作为登录 shell 运行,以强制它运行启动文件。

要这样做,可以使用 -l 或 --login 选项启动 shell。

Non-Login Shells(非登录 shell)

A non-login shell is an additional shell that you run after you log in. It’s simply any interactive shell that’s not a login shell. Windowing system terminal programs (xterm, GNOME Terminal, and so on) start non-login shells unless you specifically ask for a login shell.

非登录shell是在登录后运行的额外shell。它只是一个交互式的shell,不是登录shell。

窗口系统终端程序(如xterm、GNOME Terminal等)在没有明确要求登录shell的情况下启动非登录shell。

Upon starting up as a non-login shell, bash runs /etc/bash.bashrc and then runs the user’s .bashrc.

作为非登录shell启动时,bash会运行/etc/bash.bashrc,然后运行用户的.bashrc文件。

The Consequences of Two Kinds of Shells(两种贝壳的后果)

The reasoning behind the two different startup filesystems is that in the old days, users logged in through a traditional terminal with a login shell, then started non-login subshells with windowing systems or the screen program. For the non-login subshells, it was deemed a waste to repeatedly set the user environment and run a bunch of programs that had already been run. With login shells, you could run fancy startup commands in a file such as .bash_profile, leaving only aliases and other “lightweight” things to your .bashrc.

两种不同的启动文件系统背后的原因是,在过去的日子里,用户通过传统终端登录,然后通过窗口系统或屏幕程序启动非登录子shell。

对于非登录子shell来说,重复设置用户环境并运行已经运行过的一堆程序被认为是浪费。

通过登录shell,您可以在类似.bash_profile的文件中运行花哨的启动命令,只留下别名和其他“轻量级”内容在.bashrc中。

Nowadays, most desktop users log in through a graphical display manager (you’ll learn more about these in the next chapter). Most of these start with one noninteractive login shell in order to preserve the login versus non-login model described above. When they do not, you need to set up your entire environment (path, manual path, and so on) in your .bashrc, or you’ll never see any of your environment in your terminal window shells. However, you also need a .bash_profile if you ever want to log in on the console or remotely, because those login shells don’t ever bother with .bashrc.

如今,大多数桌面用户通过图形显示管理器登录(您将在下一章中了解更多相关信息)。

其中大部分都会启动一个非交互式登录shell,以保留上述登录与非登录模型。

如果没有启动非交互式登录shell,您需要在.bashrc中设置整个环境(路径、手动路径等),否则您将无法在终端窗口shell中看到任何环境。

然而,如果您想在控制台或远程登录,您还需要一个.bash_profile,因为这些登录shell从不关心.bashrc。

Example .bashrc(.bashrc 示例)

In order to satisfy both non-login and login shells, how would you create a .bashrc that can also be used as your .bash_profile? Here’s one very elementary (yet perfectly sufficient) example:

为了同时满足非登录和登录 shell 的需要,你该如何创建一个 .bashrc 文件,并将其用作 .bash_profile 文件呢?

下面是一个非常简单(但完全足够)的例子:

# Command path.
PATH=/usr/local/bin:/usr/bin:/bin:/usr/games
PATH=$HOME/bin:$PATH
# PS1 is the regular prompt.
# Substitutions include:
# \u username \h hostname \w current directory
# \! history number \s shell name \$ $ if regular user
PS1='\u\$ '
# EDITOR and VISUAL determine the editor that programs such as less
# and mail clients invoke when asked to edit a file.
EDITOR=vi
VISUAL=vi
# PAGER is the default text file viewer for programs such as man.
PAGER=less
# These are some handy options for less.
# A different style is LESS=FRX
# (F=quit at end, R=show raw characters, X=don't use alt screen)
LESS=meiX
# You must export environment variables.
export PATH EDITOR VISUAL PAGER LESS
# By default, give other users read-only access to most new files.
umask 022

As described earlier, you can share this .bashrc file with .bash_profile via a symbolic link, or you can make the relationship even clearer by creating .bash_profile as this one-liner:

如前所述,你可以通过符号链接将 .bashrc 文件与 .bash_profile 文件共享,也可以将 .bash_profile 创建为单行文件,使两者之间的关系更加清晰:

. $HOME/.bashrc

Checking for Login and Interactive Shells(检查登录和交互式Shell)

With a .bashrc matching your .bash_profile, you don’t normally run extra commands for login shells. However, if you want to define different actions for login and non-login shells, you can add the following test to your .bashrc, which checks the shell’s $- variable for an i character:

如果你的 .bashrc 文件与你的 .bash_profile 文件相匹配,通常不需要为登录Shell运行额外的命令。

然而,如果你想为登录和非登录Shell定义不同的操作,你可以在你的 .bashrc 文件中添加以下测试,它会检查Shell的 $- 变量是否包含字符 "i":

case $- in
*i*) # interactive commands go here
 command
 --snip--
 ;;
*) # non-interactive commands go here
 command
 --snip-
 ;;
esac

13.4.2 The tcsh Shell(tcsh Shell)

The standard csh on virtually all Linux systems is tcsh, an enhanced C shell that popularized features such as command-line editing and multi-mode filename and command completion. Even if you don’t use tcsh as the default new user shell (we suggest using bash), you should still provide tcsh startup files in case your users happen to come across tcsh. You don’t have to worry about the difference between login shells and non-login shells in tcsh. Upon startup, tcsh looks for a .tcshrc file. Failing this, it looks for the csh shell’s .cshrc startup file. The reason for this order is that you can use the .tcshrc file for tcsh extensions that don’t work in csh. You should probably stick to using the traditional .cshrc instead of .tcshrc; it’s highly unlikely that anyone will ever use your startup files with csh. And if a user actually does come across csh on some other system, your .cshrc will work.

几乎所有Linux系统上的标准csh都是tcsh,这是一个增强型的C shell,它推广了命令行编辑、多模式文件名和命令补全等功能。

即使您不使用tcsh作为默认的新用户shell(我们建议使用bash),您仍然应该提供tcsh的启动文件,以防万一您的用户碰巧遇到tcsh。

您不必担心tcsh中登录shell和非登录shell之间的区别。

在启动时,tcsh会寻找.tcshrc文件。如果找不到,它会寻找csh shell的.cshrc启动文件。

之所以采用这个顺序,是因为您可以使用.tcshrc文件来进行tcsh扩展,而这些扩展在csh中不起作用。

您应该使用传统的.cshrc文件而不是.tcshrc文件;几乎没有人会在csh中使用您的启动文件。

而且,如果用户在其他系统上真的遇到csh,您的.cshrc文件也会起作用。

Example .cshrc(.cshrc 示例)

Here is sample .cshrc file:

下面是 .cshrc 文件的示例:

# Command path.
setenv PATH /usr/local/bin:/usr/bin:/bin:$HOME/bin
# EDITOR and VISUAL determine the editor that programs such as less
# and mail clients invoke when asked to edit a file.
setenv EDITOR vi
setenv VISUAL vi
# PAGER is the default text file viewer for programs such as man.
setenv PAGER less
# These are some handy options for less.
setenv LESS meiX
# By default, give other users read-only access to most new files.
umask 022
# Customize the prompt.
# Substitutions include:
# %n username %m hostname %/ current directory
# %h history number %l current terminal %% %
set prompt="%m%% "

13.5 Default User Settings(用户默认设置)

The best way to write startup files and choose defaults for new users is to experiment with a new test user on the system. Create the test user with an empty home directory and refrain from copying your own startup files to the test user’s directory. Write the new startup files from scratch

编写启动文件并选择新用户的默认设置的最佳方法是在系统上使用一个新的测试用户进行实验。

创建一个空的家目录并避免将自己的启动文件复制到测试用户的目录中。

从头开始编写新的启动文件。

When you think you have a working setup, log in as the new test user in all possible ways (on the console, remotely, and so on). Make sure that you test as many things as possible, including the windowing system operation and manual pages. When you’re happy with the test user, create a second test user, copying the startup files from the first test user. If everything still works, you now have a new set of startup files that you can distribute to new users.

当您认为已经设置好了,以所有可能的方式(在控制台上、远程等)作为新的测试用户登录。

确保尽可能多地测试各种事物,包括窗口系统操作和手册页。当您对测试用户满意时,创建第二个测试用户,并从第一个测试用户复制启动文件。

如果一切正常,您现在有一组新的启动文件可以分发给新用户。

The following sections outline reasonable defaults for new users.

以下部分概述了新用户的合理默认设置。

13.5.1 Shell Defaults(shell 默认值)

The default shell for any new user on a Linux system should be bash because:

任何Linux系统上新用户的默认shell应该是bash,因为:

o Users interact with the same shell that they use to write shell scripts (for example, csh is a notoriously bad
scripting tool—don’t even think about it).
o bash is standard on Linux systems.
o bash uses GNU readline, and therefore its interface is identical to that of many other tools.
o bash gives you fine, easy-to-understand control over I/O redirection and file handles.

o 用户与用于编写shell脚本的相同shell进行交互(例如,csh是一个臭名昭著的糟糕脚本工具-甚至不要想)。
o bash在Linux系统上是标准的。
o bash使用GNU readline,因此其界面与许多其他工具的界面完全相同。
o bash提供了对I/O重定向和文件句柄的精细且易于理解的控制。

However, many seasoned Unix wizards use shells such as csh and tcsh simply because they can’t bear to switch. Of course, you can choose any shell you like, but choose bash if you don’t have any preference, and use bash as the default shell for any new user on the system. (A user can change his or her shell with the chsh command to suit individual preferences.)

然而,许多经验丰富的Unix专家使用csh和tcsh等shell,仅仅是因为他们无法忍受切换。

当然,您可以选择任何您喜欢的shell,但如果没有任何偏好,请选择bash,并将其作为系统上任何新用户的默认shell。

(用户可以使用chsh命令更改他们的shell以适应个人偏好。)

NOTE There are plenty of other shells out there (rc, ksh, zsh, es, and so on). Some are not appropriate as beginner shells, but zsh and fish are sometimes popular with new users looking for an alternative shell.

注意 还有很多其他的shell(rc,ksh,zsh,es等)。

有些不适合初学者使用的shell,但zsh和fish有时受到新用户的欢迎,他们在寻找替代shell时会选择这两个。

13.5.2 Editor(编辑)

On a traditional system, the default editor should be vi or emacs. These are the only editors virtually guaranteed to exist on nearly any Unix system, which means they’ll cause the least trouble in the long run for a new user. However, Linux distributions often configure nano to be the default editor, because it’s easier for beginners to use.

在传统的系统上,默认的编辑器应该是vi或者emacs。

几乎所有的Unix系统都会有这两个编辑器,这意味着对于新用户来说,它们会在长期使用中带来最少的麻烦。

然而,Linux发行版通常将nano配置为默认编辑器,因为它对于初学者来说更容易使用。

As with shell startup files, avoid large default editor startup files. A little set showmatch in the .exrc startup file never hurt anyone but steer clear of anything that significantly changes the editor’s behavior or appearance, such as the showmode feature, auto-indentation, and wrap margins.

与shell启动文件一样,避免使用过大的默认编辑器启动文件。

在.exrc启动文件中设置一些简单的showmatch功能是没有问题的,但要避免使用任何显著改变编辑器行为或外观的功能,比如showmode特性、自动缩进和换行边界。

13.5.3 Pager(呼叫器)

It’s perfectly reasonable to set the default PAGER environment variable to less

将默认的PAGER环境变量设置为less是完全合理的。

13.6 Startup File Pitfalls(启动文件陷阱)

Avoid these in startup files:

在启动文件中避免以下内容:

o Don’t put any kind of graphical command in a shell startup file.
o Don’t set the DISPLAY environment variable in a shell startup file.
o Don’t set the terminal type in a shell startup file.
o Don’t skimp on descriptive comments in default startup files.
o Don’t run commands in a startup file that print to the standard output.
o Never set LD_LIBRARY_PATH in a shell startup file (see 15.1.4 Shared Libraries).

  • 不要在shell启动文件中放置任何图形命令。
  • 不要在shell启动文件中设置DISPLAY环境变量。
  • 不要在shell启动文件中设置终端类型。
  • 在默认的启动文件中不要吝啬描述性注释。
  • 不要在启动文件中运行打印到标准输出的命令。
  • 绝对不要在shell启动文件中设置LD_LIBRARY_PATH变量(参见15.1.4共享库)。

    13.7 Further Startup Topics(更多创业主题)

Because this book deals only with the underlying Linux system, we won’t cover windowing environment startup files. This is a large issue indeed, because the display manager that logs you in to a modern Linux system has its own set of startup files, such as .xsession, .xinitrc, and the endless combinations of GNOMEand KDE-related items.

因为本书仅涉及Linux系统的基础部分,我们不会涵盖窗口环境启动文件。

这确实是一个重要问题,因为登录到现代Linux系统的显示管理器有自己的一套启动文件,例如.xsession、.xinitrc以及与GNOME和KDE相关的无尽组合。

The windowing choices may seem bewildering, and there is no one common way to start a windowing environment in Linux. The next chapter describes some of the many possibilities. However, when you determine what your system does, you may get a little carried away with the files that relate to your graphical environment. That’s fine, but don’t carry it over to new users. The same tenet of keeping things simple in shell startup files works wonders for GUI startup files, too. In fact, you probably don’t need to change your GUI startup files at all.

窗口环境的选择可能令人困惑,并且在Linux中没有一种通用的启动窗口环境的方法。

下一章将介绍一些许多可能性。

然而,当你确定了你的系统所做的事情时,你可能会对与你的图形环境相关的文件感到有些着迷。

这没问题,但不要把它带给新用户。

在shell启动文件中保持简单的原则同样适用于GUI启动文件。

实际上,你可能根本不需要改变你的GUI启动文件。


Xander
195 声望50 粉丝