如何为多个用户设置单个 .bashrc 文件?

use*_*584 40 bash

在我的工作中,我需要不断地向 bashrc 添加别名命令,其中大部分命令需要由其他用户运行。有什么办法可以从外部源向 bashrc 添加别名命令吗?

Wil*_*ilf 50

用户主目录中的许多配置文件只是覆盖/添加到/etc- 例如用户主目录中的GIMP 设置在 中~/.gimp-2.*,它添加到系统范围的配置中/etc/gimp/2.0

因此,对于~/.bashrc,您可以编辑系统范围的配置文件/etc/bash.bashrc (对于函数/别名)或/etc/profile (对于环境内容) -您可以从man bash以下位置获得完整列表:

FILES
       /bin/bash
              The bash executable
       /etc/profile
              The systemwide initialization file, executed for login shells
       /etc/bash.bash_logout
              The systemwide login shell cleanup file, executed when a login shell exits
       ~/.bash_profile
              The personal initialization file, executed for login shells
       ~/.bashrc
              The individual per-interactive-shell startup file
       ~/.bash_logout
              The individual login shell cleanup file, executed when a login shell exits
       ~/.inputrc
              Individual readline initialization file
Run Code Online (Sandbox Code Playgroud)

此警告针对文件中的一些 Linux 系统给出:

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
Run Code Online (Sandbox Code Playgroud)

因此,您可以编辑这些文件,您可能希望首先备份它们(cp /etc/bash.bashrc /etc/bash.bashrc-backup例如),或者在其中创建一个 shell 脚本/etc/profile.d——例如,您可以使用这些命令(使用 sudo/作为 root)创建一个:

touch /etc/profile.d/custom.sh
chmod +x /etc/profile.d/custom.sh
Run Code Online (Sandbox Code Playgroud)

然后用 nano /etc/profile.d/custom.sh

#!/bin/sh
alias ls='ls -lah'
Run Code Online (Sandbox Code Playgroud)

并通过查看它是否出现在输出中来检查它是否有效alias- 请注意,您可能需要注销/登录或重新启动以查看任何更改(如果您不想,source /etc/profile在任何更改后运行可能会起作用)

  • 那里有很多好的建议。我认为还有另一种选择,这也值得一提。可以编辑`/etc/skel/.bashrc` 来更改`~/.bashrc` 的内容对于新创建的用户的外观。 (6认同)

小智 8

/etc/bash.bashrc

vim /etc/bash.bashrc

并在那里制作您的别名。

在最后一行添加您的别名。

别名 abc="随便"

该别名将成为所有用户的全局别名。

但出于安全原因,我们不建议您这样做。

profile.d一个包含用户环境文件的目录

cd /etc/profile.d/

vim 别名

并在此处添加您的别名。

不影响您的系统文件。使用您的环境文件是安全且正确的方式。