1

I'm using a server where I'm a common user (non-sudo).

I access the server through ssh.

Here's the output of some commands run on the server:

[username@machinename: ~]$ ps -p $$
    PID TTY          TIME CMD
1332818 pts/55   00:00:00 bash
[username@machinename: ~]$ echo $$SHELL
1332818SHELL
[username@machinename: ~]$ echo $-
himBHs
[username@machinename: ~]$ uname
Linux
[username@machinename: ~]$ uname -v
#1 SMP Thu May 11 07:38:47 EDT 2023
[username@machinename: ~]$ uname -r
4.18.0-372.57.1.el8_6.x86_64
[username@machinename: ~]$ cat /etc/os-release
NAME="Red Hat Enterprise Linux"
VERSION="8.6 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.6"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.6 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8::baseos"
HOME_URL="https://www.redhat.com/"
DOCUMENTATION_URL="https://access.redhat.com/documentation/red_hat_enterprise_linux/8/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"

REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_BUGZILLA_PRODUCT_VERSION=8.6
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.6"

Username and machine name are hided.

Here's the operating system information:

LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: RedHatEnterprise
Description:    Red Hat Enterprise Linux release 8.6 (Ootpa)
Release:    8.6
Codename:   Ootpa

Normally to add aliases on Ubuntu/Linux, I just need to edit the ~/.bashrc file. But this file doesn't exit the first time I enter the system, so I created it on my own in my home directory and fill the aliases:

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/tien/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/tien/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/tien/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/tien/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

export PATH="/usr/local/cuda-12.1/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-12.1/lib64:$LD_LIBRARY_PATH"

alias c='clear'
alias gpu='watch -n 0.5 nvidia-smi'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
alias h='history'
alias j='jobs -l'
alias gst='git status'

But when I log out and re-login, the alias doesn't work.

So how can I debug this?

Here are some of my info:

[username@machinename: ~]$ which alias
/usr/bin/alias
[username@machinename: ~]$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias vi='vim'
alias xzegrep='xzegrep --color=auto'
alias xzfgrep='xzfgrep --color=auto'
alias xzgrep='xzgrep --color=auto'
alias zegrep='zegrep --color=auto'
alias zfgrep='zfgrep --color=auto'
alias zgrep='zgrep --color=auto'
6
  • 1
    Could you be running another shell than bash ?
    – MC68020
    Commented Aug 7, 2023 at 16:42
  • ... or perhaps it's a bash login shell, that's reading ~/.bash_profile or ~/.profile instead of ~/.bashrc Commented Aug 7, 2023 at 17:38
  • Please edit your question and tell us i) exactly how you are accessing this server (over ssh? Are you logging in graphically? Some other way?), ii) the output of ps -p $$ and echo $SHELL run on the server, iii) the output of echo $- on the server.
    – terdon
    Commented Aug 7, 2023 at 17:41
  • @terdon The question is edited following your instructions.
    – pvti
    Commented Aug 8, 2023 at 14:32
  • @MC68020 I guess nope, I access through ssh and use the default command window opened.
    – pvti
    Commented Aug 8, 2023 at 14:33

3 Answers 3

1

I include this in ~/.profile

# if running bash
# include .bashrc if it exists
[ -n "$BASH_VERSION" ] && [ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"
1
  • There was no file `.profile' in ~/ so I created it with the aforementioned content. Then I log out and log in. Now it works. Thank you very much @glenn jackman
    – pvti
    Commented Aug 8, 2023 at 14:38
0

you said on Redhat server...

look at /etc/profile.d/ contents and you see a bunch of .sh and .csh files. In /etc/passwd if your account login is /bin/bash then it will reference the .sh files, if /bin/csh or /bin/tcsh then the .csh files.

create an /etc/profile.d/my_aliases.sh; it can be named anything just has to have the .sh ending (or .csh) in regards to your account shell.

simply dump your alias commands and whatever else into one or more .sh files under /etc/profile.d/ and give it 664 permissions.

you can throw an

echo "hi from /etc/profile.d/myaliases.sh"

in there to verify the file is getting run, you should see that printed in any terminal window that gets opened.

-1

Other commentators have suggested adding this to ~/.profile or /etc/profile. But the profile is different from bashrc. The former is only run for login/interactive shells. It's usually more appropriate to use the rc file for aliases.

I don't have a Fedora box handy, but on a nearby Ubuntu box, I have both a personal ~/.bashrc and a system-wide /etc/bash/bashrc

If ~/.bashrc does not work for you and there is a system wide file in /etc, I suggest adding [ -f "${HOME}/.bashrc" ] && source "${HOME}/.bashrc" rather than adding your aliases inline in the file.

3
  • None of the rc family of files are read by login shells. Debian and its derivatives include a . "$HOME/.bashrc" command to bring in rc settings to the profile files which are read by login shells, but on other systems, adding it to either /etc/bash.bashrc or to ~/.bashrc will both fail since neither file is read by login shells. See Difference between Login Shell and Non-Login Shell?
    – terdon
    Commented Aug 8, 2023 at 15:18
  • RTFM: bashrc is invoked for both interactive and non-interactive shells UNLESS the shell is invoked with $0="sh" (the man page is a bit big; look for "--norc") linux.die.net/man/1/bash
    – symcbean
    Commented Aug 8, 2023 at 16:22
  • Woah there. First of all, that language and tone are not acceptable here, please never use it again. Second, please read my comment again. The relevant difference here isn't interactive vs non-interactive, but login vs non-login. The OP is running a login shell and those do not read the rc files. This is explained in the very section you so rudely pointed me to, two paragraphs above where you read (emphasis mine): "When bash is invoked as an interactive login shell [...] it [...] reads [...] the file /etc/profile [...] ~/.bash_profile, ~/.bash_login, and ~/.profile".
    – terdon
    Commented Aug 8, 2023 at 16:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .