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


30

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

Answers:


40

用户主目录中的许多配置文件只是覆盖/添加到其中/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-例如,您可以使用以下命令(使用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在进行任何更改后运行即可)

[编辑:删除了该死链接] www.thelinuxdaily.com/2010/08/setup-a-universal-user-profile-with-etcprofile-dcustom-sh/]


5
那里有很多好的建议。我认为还有另一种选择,这也值得一提。可以编辑/etc/skel/.bashrc以更改~/.bashrc新创建用户的内容。
kasperd 2014年

@kasperd-好主意(如果需要,您可以做一个答案)
Wilf

以我的经验,~/.bash_logout/etc/bash.bash_logout不能正常工作。:(
Paddy Landau

AFAIK别名/etc/profile.d/不会被子进程权被拾起?因此,仅当Bash是登录外壳程序时,别名才可用吗?
富兰克林于

我认为其中一些仅在将TTY用作唯一环境时才起作用,而在台式机终端中可能不使用其中一些。我必须对此进行测试以验证这一点,但目前无法!
Wilf

7

/etc/bash.bashrc

vim /etc/bash.bashrc

然后在那做你的别名

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

别名abc =“ whatever”

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

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

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

cd /etc/profile.d/

vim别名

并在此处添加别名。

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


3

此处已经有一个可接受的答案,但是您可以考虑使用某种形式的环境模块来处理用户环境的系统范围配置,而不是弄乱/etc/profile.d等中的文件。如果您要跨多个shell在一个地方管理此控件。Lmod(正在积极开发中),C / TCL模块(经典解决方案)或Cmod(轻量级)。


1

不知道我是否做对了,但是对我来说,保留/ home / ComunAtodos目录(大写且西班牙语,以表明它不是Linux本机目录),并放置.bashrc,.nanorc和.bash_aliases在那里。然后,我在/etc/skel/.profile中引用它们,以便新用户指向它们,并且仅向需要它们的特定用户添加其他内容。


-1

我真的是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文件的所有用户创建了一个文件定制。


3
第一行(您在其中读取/ etc / passwd)似乎无济于事。您的第4行似乎是一个错误(我不明白您为什么要抑制错误)。“出口0”是多余的。这要求所有用户都已经有一个.bashrc文件,并且无论行是否已经在他们的文件中,都始终追加。
thomasrutter

我当时在想获得该行所有用户的名字?但是,现在我考虑了一下,我就可以剪切</ etc / passwd,该列表上有很多名称,所以我不希望IT为那些没有bashrcfilé的人带来错误。仅当该行不存在时,您才能追加吗?您能为这种类型的rask编写正确的脚本吗,请问我仅使用Linux一周。
Farhad Rahimi

看来您已经掌握了一些技能,但是本教程非常适合shell脚本编写:shellscript.sh。对于其他问题,例如仅在行不存在时如何追加行,请随时在此处作为新问题提问。
thomasrutter
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.