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

在我的工作中,我需要不断向bashrc添加别名命令,其中大多数命令需要被其他用户破坏。 有什么办法可以从外部源向bashrc添加别名命令吗?

用户主目录中的许多配置文件只是覆盖/添加到/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 

对于文件中的一些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. 

所以你可以编辑这些文件,你可能想先备份它们(例如cp /etc/bash.bashrc /etc/bash.bashrc-backup ),或者在/etc/profile.d创建一个shell脚本 – 例如你可以使用这些命令创建一个(使用sudo / root):

 touch /etc/profile.d/custom.sh chmod +x /etc/profile.d/custom.sh 

然后用nano /etc/profile.d/custom.sh打开它

 #!/bin/sh alias ls='ls -lah' 

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

转到/etc/bash.bashrc

vim /etc/bash.bashrc

并在那里做你的别名。

在最后一行添加别名。

alias abc =“whatever”

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

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

profile.d目录,其中包含用户环境文件

cd /etc/profile.d/

vim别名

并在此处添加别名。

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

这里已经有了一个已经接受的答案,但您可以考虑使用某种forms的环境模块来处理用户环境的系统范围配置,而不是弄乱/etc/profile.d等中的文件。如果您想要这样做,尤其如此在许多shell中的一个地方管理这个控件。 Lmod (非常活跃的开发), C / TCL模块 (经典解决方案),或Cmod (轻量级)。

我是Linux操作系统的新手,但我做了一个bash脚本的草图,它修改了所有用户.bashrc文件,而不是系统文件/etc/.bashrc文件。

 #!/bin/bash X=$( cat etc/passwd | cut -f1 -d: ) #All-users For X in /home/*/.bashrc ; do echo "alias ls='ls -al'" >> $X 2>/dev/null done source $X exit 0 

好吧所以我知道那个脚本有效,但我不知道如果它没有故障:)你也可以修改它所以它不涉及所有用户,也许你为需要他们的.bashrc文件的所有用户创建一个文件定制。