1

I have an alias set up

alias nano='nano -B'

in order to always make backups on change of files.

I did this in both /home/<USER>/.bashrc and in /root/.bashrc.

It works if I use

nano someFile

either as <USER> or root.

But when I use

sudo nano someFile

as <USER> backups are not made. (I'ld expect to find someFiles~ in the same folder)

Is there an additional .bashrc or something for sudo where I have to add this alias?

derHugo
  • 3,306
  • 5
  • 30
  • 49
  • 2
    Possible duplicate of [rm -i alias not working with sudo as root](https://askubuntu.com/questions/84366/rm-i-alias-not-working-with-sudo-as-root) – pLumo Nov 09 '17 at 12:51
  • 1
    https://askubuntu.com/questions/22037/aliases-not-available-when-using-sudo – pLumo Nov 09 '17 at 12:53
  • Your user ID's aliases are not used via sudo ; You may or may not want to use the trick in the previous comments. There might be reasons to have no aliases or other aliases for `root`. – sudodus Nov 09 '17 at 13:00
  • 1
    Yes but I thought those other aliases would be defined in `/root/.bashrc` – derHugo Nov 09 '17 at 13:02
  • Yes, and they are used if you run `sudo -i` (and get the root prompt). – sudodus Nov 09 '17 at 13:40
  • @sudodus I usually used `sudo -i` pressed `Enter` and did my stuff (e.g. `nano ./.configFile`) as root. Now that you mention it I tried to do just `sudo -i nano /root/.configFile` which works fine , too. Is it a good Idea than to use an alias as `alias sudo='sudo -i'` or speaks anything against this? – derHugo Nov 09 '17 at 16:27

1 Answers1

1

Your user ID's aliases are not used via sudo

  • You may or may not want to use the trick linked to in the comments.

  • There might be reasons to have no aliases or other aliases for root.

    These aliases can be stored in /root/.bashrc as you already know. They can be used when you run interactively at the root prompt # after

      sudo -i  # activates root's aliases
    

but they are not activated when followed by the alias on the command line

    sudo -i <specific alias>  # does not activate root's aliases
    sudo -H <specific alias>  # does not activate root's aliases

Examples:

$ LANG=C sudo -i

root@xenial32:~# alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

root@xenial32:~# grep -e ^alias -e \ alias /root/.bashrc
# enable color support of ls and also add handy aliases
    alias ls='ls --color=auto'
    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

Using one of root's aliases

root@xenial32:~# l
bin@  extractor.log  logfile.tar  mkusb.log

root@xenial32:~# exit
logout

sudodus@xenial32 ~ $ LANG=C sudo -i l
-bash: l: command not found

[127] sudodus@xenial32 ~ $ LANG=C sudo -H l
sudo: l: command not found

[1] sudodus@xenial32 ~ $ LANG=C sudo -i alias
sudodus@xenial32 ~ $ 

I would not use alias sudo='sudo -i' because

  • there is a risk, that you forget that you have superuser privileges, and may do things that you should only do with regular privileges. In other words, I would say that it defeats the purpose of sudo to always go to root prompt

  • an alias does not work anyway on the command line with sudo -i <specific alias>

  • it is often efficient to use sudo with the standard settings for text mode commands

  • for GUI commands I would recommend sudo -H or gksudo

sudodus
  • 45,126
  • 5
  • 87
  • 151