during my work I need to constantly add alias commands to bashrc, most of those commands needs to be runed by other users. is there any way I could add alias commands to a bashrc from external source?
5 Answers
Many config files in users home directory just override/add to ones in the /etc
- for example the settings for GIMP in the users home are in ~/.gimp-2.*
, which adds to the system-wide config /etc/gimp/2.0
.
So for ~/.bashrc
, you could edit the system wide config files /etc/bash.bashrc
(for functions/aliases) or /etc/profile
(for environment stuff) - you can the full list from 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
This warning is given for a few Linux systems in the files:
# 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.
So you could edit those files, you may want to back them up first (cp /etc/bash.bashrc /etc/bash.bashrc-backup
for example), or create a shell script in /etc/profile.d
- for example you can create one with these commands (with sudo/as root):
touch /etc/profile.d/custom.sh
chmod +x /etc/profile.d/custom.sh
Then open it with nano /etc/profile.d/custom.sh
#!/bin/sh
alias ls='ls -lah'
And check whether it works by seeing if it appears in the output of alias
- Note that you may need to logout/login or reboot to see any changes (if you don't want to, running source /etc/profile
after any changes might work)
-
7Plenty of good suggestions there. I think there is one other option, which would be worth mentioning as well. One can edit
/etc/skel/.bashrc
to change what the contents of~/.bashrc
will look like for newly created users.– kasperdJul 27, 2014 at 15:08 -
-
In my experience,
~/.bash_logout
and/etc/bash.bash_logout
do not work. :( Jul 29, 2014 at 13:56 -
1AFAIK alias in
/etc/profile.d/
will not be picked up by sub-process right? So the alias is only available when Bash is the log-in shell? Jun 12, 2018 at 22:11 -
I think some of them only work when its used in say a TTY as the only environment, in a desktop terminal some of them may not be used. I'd have to test this to verify this though and can't at the moment!– WilfJun 14, 2018 at 12:53
Go to /etc/bash.bashrc
vim /etc/bash.bashrc
and make your alias there.
add your alias in last line.
alias abc="whatever"
That alias will become global for all users.
but for security reasons we dont recommend you that.
there is profile.d
directory which contains user-environment files
go to
cd /etc/profile.d/
vim aliases
and add your aliases here.
without effecting your system files. It is safe and right way to work with your environment files.
There's already an accepted answer here, but you might consider using some form of environment modules to handle system-wide configuration of user environments rather than messing with the files in /etc/profile.d, etc. This is especially true if you want to manage this control in one place across lots of shells. Lmod (under very active development), C/TCL modules (the classic solution), or Cmod (lightweight).
Don't know if I'm doing it right, but it works for me to keep a /home/ComunAtodos directory (capitalized and in spanish to make evident that it is not a linux native directory) and put .bashrc, .nanorc and .bash_aliases there. Then I reference those in /etc/skel/.profile so new users point to them, and only add extras to specific users that need them.
I'm really new to Linux operativ system, but i did a sketch of a bash script that works to modify all the users .bashrc file and not the system file /etc/.bashrc file.
#!/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
Okay so i know that that script works, but i don't if it's fault free :) Also u can modify it so it doesn't involves all the users, maybe u make a file for all the users that need their .bashrc file customized.
-
3First line (where you read /etc/passwd) seems to do nothing. Your 4th line seems to be a mistake (and I don't see why you'd want to suppress errors). "exit 0" is redundant. This requires all users to already have a
.bashrc
file, and always appends regardless of whether the line is already in their file. Dec 3, 2017 at 23:20 -
I was thinking to get the names of all users with that line? But now that i think about it i could just do cut < /etc/passwd, there was alot of names on that list so i didn't want IT to make errrors for those who doesn't have bashrc filé. How can u append only if the line doesn't exist ? And could u Write the proper script for this type of rask please i have only been using Linux for a week. Dec 4, 2017 at 11:11
-
Looks like you already have some skills but this tutorial is a good one for shell scripting: shellscript.sh . For other questions such as how to append a line only if it doesn't already exist, feel free to ask them as new questions on here. Dec 4, 2017 at 23:21