add alias to root

26,108

Solution 1

You should add that alias command line to .bashrc file in /root folder and not in /home/user/.bashrc(this .bashrc file belongs to user account not root).

By doing so .bashrc file in /root is run at root login, your alias command will be automatically created every time.

So that you can use your alias command directly in shell when logged in as root.

Solution 2

You can get that done in the same way as you would do it for other users:

  1. By adding the new alias to the user's ~/.bashrc file (for root user the absolute path is /root/.bashrc);
  2. (Preferred method as you can have all custom aliases and functions under one file.) By creating, if it is not there, and adding the new alias to the user's ~/.bash_aliases file (for root you'd create /root/.bash_aliases).

(Note: You can also add/append new function definations in the same way as mentioned for aliases above.)

For an example you can check this link.

Share:
26,108
Akisame
Author by

Akisame

Updated on September 18, 2022

Comments

  • Akisame
    Akisame over 1 year

    I need to execute a command for my system to search for new sd cards and to be able to execute it I need to execute it as root. Simply using sudo with the command does not work. This is not a question of how to execute an alias with sudo rights!
    I want to know how to add an alias to the root user so I can execute it after using sudo -i. I tried logging in to the root user and just adding the alias (which works fine). The only problem is that the alias disappears after I log out from the terminal.