0

I am running

Linux 4.4.0-116-generic Ubuntu + KDE ( x86_64 GNU/Linux )

I want to run an alias named ksession that kills a particular session. At the same time I want that only I can run this alias and nobody else. So, I found .bashrc file under /root directory and added an alias by the name ksession in it, then tried running source command with no success and following errors :

amit@C0deDaedalus:~$ source /root/.bashrc
bash: /root/.bashrc: Permission denied

amit@C0deDaedalus:~$ sudo source /root/.bashrc
sudo: source: command not found
  1. Is there any way to source that file being a non-root user ?

  2. I want to run the alias being a non-root user. Can I run it with sudo like this ?

    $ sudo ksession
    

    I tried that and failed with error :

    amit@c0deDaedalus:~$ sudo ksession
    sudo: ksession: command not found
    
  3. If not, Is there any workaround for running root aliases from a non-root user ?

2 Answers 2

4

Root shouldn't need aliases. If you want something private, then don't start changing things in other user's accounts (root's in this case).

Just write ksession as a shell script and use sudo in the script to when needed. Put the script in a private directory, for example $HOME/bin, and make sure that this directory is part of your $PATH and that the script is executable.

Alternatively, write it as a shell function and put it in your ~/.bashrc file. The function may use sudo when needed.

Alternatively, write it as an alias and put it in your ~/.bashrc file. The alias may use sudo when needed.


Your sudo source ... command does not work since source is a bash built-in command and sudo expects an external command.

Even if it had worked, the alias would not have been activated in the calling shell as the command that sudo invokes is running in its own sub-shell.

0

You can create a script in /etc/profile.d/ to make aliases for all users:

Create a file called 00-aliases.sh (or any other fancy name) in /etc/profile.d:

nano /etc/profile.d/00-aliases.sh Put you aliases in this file. Example:

alias foo='bar --baz' alias baz='foo --bar' Save the file

Restart any open terminals to apply the changes. Enjoy

1
  • 1
    Welcome to the community. OP specifically asked how to run aliases created for root user when logged in as a regular user. You explain how to create aliases for every user on the system. I'm sure you agree these are two entirely different things :-) Since you're new, I'd suggest taking the Tour and checking Asking and Answering in the Help to learn how these sites work, how to write a good answer etc. It's very likely this answer will be shortly deleted. Feb 11 at 19:13

You must log in to answer this question.

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