SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How can I get aliases working as root?
Here is my Slackware 13.1's /etc/profile file followed by my *Slackware 11's* /etc/profile file [as a comparison]. My Slackware 11's root aliases work in addition to my username ones *without* needing a separate .bashrc in my home dir.
In S13.1 I have created a .bashrc for my username and it works fine, but I want those same aliases operating when I'm root, and cannot for the life of me figure how to achieve it. Why???
S13.1 /etc/profile:
# /etc/profile: This file contains system-wide defaults used by
# all Bourne (and related) shells.
# If the user doesn't have a .inputrc, use the one in /etc.
if [ ! -r "$HOME/.inputrc" ]; then
export INPUTRC=/etc/inputrc
fi
# Set the default system $PATH:
PATH="/usr/local/bin:/usr/bin:/bin:/usr/games:/usr/sbin:/usr/local/sbin:/sbin"
# Let's try some aliases!!!!
alias l='ls -la|more'
alias cd='cd /home/pargoo'
alias start='/etc/rc.d/rc.inet1 restart'
alias stop='/etc/rc.d/rc.inet1 stop'
# For root users, ensure that /usr/local/sbin, /usr/sbin, and /sbin are in
# the $PATH. Some means of connection don't add these by default (sshd comes
# to mind).
if [ "`id -u`" = "0" ]; then
echo $PATH | grep /usr/local/sbin 1> /dev/null 2> /dev/null
if [ ! $? = 0 ]; then
PATH=/usr/local/sbin:/usr/sbin:/sbin:$PATH
fi
fi
# I had problems with the backspace key using 'eval tset' instead of 'TERM=',
# but you might want to try it anyway instead of the section below it. I
# think with the right /etc/termcap it would work.
# eval `tset -sQ "$TERM"`
# Set TERM to linux for unknown type or unset variable:
if [ "$TERM" = "" -o "$TERM" = "unknown" ]; then
TERM=linux
fi
# Set ksh93 visual editing mode:
if [ "$SHELL" = "/bin/ksh" ]; then
VISUAL=emacs
# VISUAL=gmacs
# VISUAL=vi
fi
# Set a default shell prompt:
PS1='`hostname`:`pwd`# '
if [ "$SHELL" = "/bin/pdksh" ]; then
PS1='! $ '
elif [ "$SHELL" = "/bin/ksh" ]; then
PS1='! ${PWD/#$HOME/~}$ '
elif [ "$SHELL" = "/bin/zsh" ]; then
PS1='%n@%m:%~%# '
elif [ "$SHELL" = "/bin/ash" ]; then
PS1='$ '
else
PS1='\u@\h:\w\$ '
fi
PS2='> '
export PATH DISPLAY LESS TERM PS1 PS2
# Default umask. A umask of 022 prevents new files from being created group
# and world writable.
umask 022
# Notify user of incoming mail. This can be overridden in the user's
# local startup file (~/.bash.login or whatever, depending on the shell)
if [ -x /usr/bin/biff ]; then
biff y 2> /dev/null
fi
# Append any additional sh scripts found in /etc/profile.d/:
for profile_script in /etc/profile.d/*.sh ; do
if [ -x $profile_script ]; then
. $profile_script
fi
done
unset profile_script
# For non-root users, add the current directory to the search path:
if [ ! "`id -u`" = "0" ]; then
PATH="$PATH:."
fi
...And my Slackware 11 /etc/profile:
# /etc/profile: This file contains system-wide defaults used by
# all Bourne (and related) shells.
# Set the values for some environment variables:
export MINICOM="-c on"
export MANPATH=/usr/local/man:/usr/man:/usr/X11R6/man
export HOSTNAME="`cat /etc/HOSTNAME`"
export LESSOPEN="|lesspipe.sh %s"
export LESS="-M"
# Set a few aliases (hopefully!!)
alias l='ls -la|more'
alias p='cd /home/pargoo'
alias start='/etc/rc.d/rc.inet1 restart'
alias stop='/etc/rc.d/rc.inet1 stop'
# If the user doesn't have a .inputrc, use the one in /etc.
if [ ! -r "$HOME/.inputrc" ]; then
export INPUTRC=/etc/inputrc
fi
# Set the default system $PATH:
PATH="/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/usr/games"
# For root users, ensure that /usr/local/sbin, /usr/sbin, and /sbin are in
# the $PATH. Some means of connection don't add these by default (sshd comes
# to mind).
if [ "`id -u`" = "0" ]; then
echo $PATH | grep /usr/local/sbin 1> /dev/null 2> /dev/null
if [ ! $? = 0 ]; then
PATH=/usr/local/sbin:/usr/sbin:/sbin:$PATH
fi
fi
# I had problems with the backspace key using 'eval tset' instead of 'TERM=',
# but you might want to try it anyway instead of the section below it. I
# think with the right /etc/termcap it would work.
# eval `tset -sQ "$TERM"`
# Set TERM to linux for unknown type or unset variable:
if [ "$TERM" = "" -o "$TERM" = "unknown" ]; then
TERM=linux
fi
# Set ksh93 visual editing mode:
if [ "$SHELL" = "/bin/ksh" ]; then
VISUAL=emacs
# VISUAL=gmacs
# VISUAL=vi
fi
# Set a default shell prompt:
#PS1='`hostname`:`pwd`# '
if [ "$SHELL" = "/bin/pdksh" ]; then
PS1='! $ '
elif [ "$SHELL" = "/bin/ksh" ]; then
PS1='! ${PWD/#$HOME/~}$ '
elif [ "$SHELL" = "/bin/zsh" ]; then
PS1='%n@%m:%~%# '
elif [ "$SHELL" = "/bin/ash" ]; then
PS1='$ '
else
PS1='\u@\h:\w\$ '
fi
PS2='> '
export PATH DISPLAY LESS TERM PS1 PS2
# Default umask. A umask of 022 prevents new files from being created group
# and world writable.
umask 022
# Notify user of incoming mail. This can be overridden in the user's
# local startup file (~/.bash.login or whatever, depending on the shell)
if [ -x /usr/bin/biff ]; then
biff y 2> /dev/null
fi
# Append any additional sh scripts found in /etc/profile.d/:
for profile_script in /etc/profile.d/*.sh ; do
if [ -x $profile_script ]; then
. $profile_script
fi
done
unset profile_script
# For non-root users, add the current directory to the search path:
if [ ! "`id -u`" = "0" ]; then
PATH="$PATH:."
fi
I have searched for days for a solution and it is extremely annoying to be baulked by such a small glitch. All help appreciated!
Click here to see the post LQ members have rated as the most helpful post in this thread.
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,153
Rep:
Either copy your .bashrc to roots home folder or make a symlink a symlink would be best as then any changes made in your .bashrc would be reflected in roots
<takes deep breath>
Okay, here's the state of play. Before the last time I shut my machine down I created a .bashrc file in my home dir with just a few simple aliases like l='ls -la|more' and - lo and behold, they worked as long as I was in a user-terminal. Thinking the battle was half-won, I commented out all the identical aliases in /etc/profile. Imagine my surprise when I reopened a fresh konsole as user to find 'l' now got a 'command not found' error.
What?????
I then renamed my ~/.bashrc to ~/notworking.bashrc and returned as root to my /etc/profile file and un-commented aliases one by one. Every time I opened a fresh terminal as user the particular alias I had just uncommented now worked thus proving to my satisfaction that IT was setting my user-specific aliases, NOT my ~/.bashrc file [which, as you may recall, I had renamed].
I then created a .bashrc file with those same simple aliases and put it in /etc/profile.d. Guess what: my user aliases were still okay but *still* no aliases at all for root, which is what this is all about. I also renamed it /etc/profile.d/bashrc to see if the '.' made any difference. It didn't. I see by a scrap of paper I scribbled notes down on that /etc/profile.d/bash_profile didn't work either.
Perhaps the difference is whether or not the particular shell (xterm, VT, konsole, whatever) is a login shell or not. Bash reads different files when it is invoked as a login shell or not. Check the man-page for details. Note that by default, virtual terminals are login shells already - but your X terminals may or may not be.
Meanwhile, try in a xterm starting bash (as either user or root) like:
Code:
bash -l
and see if aliases and things which do not work previously, do work now?
FWIW, I made wrapper scripts to start my x-terminals, and made them always start bash with -l thereby saving me this hassle.
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,153
Rep:
Quote:
Originally Posted by GrapefruiTgirl
Perhaps the difference is whether or not the particular shell (xterm, VT, konsole, whatever) is a login shell or not...
Bash reads different startup files like this:
Login shell (ie the virtual consoles ) 1st looks for /etc/profile (NOT /etc/profile.d/profile) if that file can't be found then looks for ~/.bash_profile, ~/.bash_login, and ~/.profile as per the man page.
For non login interactive shells ie most x terminals looks for ~/.bashrc it doesn't look in /etc/ or /etc/profile.d automatically if you want to put a bashrc file in etc for use globally you must specifically include it from your ~/.bashrc ie:
Code:
if [ -f /etc/bashrc ] ; then
. /etc/bashrc
fi
I admit it's really confusing where bash reads its startup files from but if in doubt try the man pages
May be something like thius could be made a sticky as I seem to recall a number of posts on similar subjects?
Last edited by Keith Hedger; 08-23-2010 at 10:06 AM.
I agree, a sticky *might* be a good idea - or at least, a good tutorial/article on the subject somewhere that we could point to.
A while back, for diagnostic purposes, I put a line at the top of all my .profile and .*bash* files which would print what file it was when it was read. This was very helpful in figuring out why I had been having similar issues as the OP.
Bash reads different startup files like this:
Login shell (ie the virtual consoles ) 1st looks for /etc/profile (NOT /etc/profile.d/profile) if that file can't be found then looks for ~/.bash_profile, ~/.bash_login, and ~/.profile as per the man page.
If "ie" was used to mean "that is" (as opposed to "for example") then that is not the whole story; login shells can be started from other places apart from the virtual terminals such as logging on graphically, over a network, at a serial line terminal and when forced by the -l option.
Regards "if that file can't be found", here's from the GNU Bash Reference Manual (my bolding): "it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable".
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,153
Rep:
Sorry I meant eg not ie yes you are right I misread the man the other profile files seem to be for adding extra per user stuff, it's been a very bad day today I'm going to go away now and be very quiet, apologies
Have been away for three days, hence the slow reply.
I tried "#bash -l", and yes, my aliases start working as root. Close the window and open another one and they don't work again - I have my shortcut in KDE ticked as 'run as different user' - root, obviously, if this means anything important(?) If I type "exit" as root it says "logout" and I'm back to aliases not working even though I'm still root.
At least I have a short-term fix!
P.S: I also notice that after typing 'bash -l' my prompt now lists the active dir the way I want it to: root@pargoo:/etc/X11#
Never knowing where I am and having to enter 'pwd' all the time is a PITB.
Sorry I meant eg not ie yes you are right I misread the man the other profile files seem to be for adding extra per user stuff, it's been a very bad day today I'm going to go away now and be very quiet, apologies
No worries, no problems and don't be a stranger
We all make mistakes (I make lots -- as a search of my post for "Oops" shows) and anyone who never risks making a mistake doesn't get far. That's one of the great things about this community -- if someone makes a mistake, mostly someone corrects it soon afterwards and greater knowledge is gained.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.