0
$ which sudo emacs
  /usr/bin/sudo
  /home/user1/local_build/bin/emacs
$ sudo emacs /etc/apache2/sites-available/000-default.conf 
  sudo: emacs: command not found
$ emacs /etc/apache2/sites-available/000-default.conf # Works fine
$ which emacs
  /home/user1/local_build/bin/emacs
$

Edit:

I have appended PATH for root user, just now

$ whoami
 user1
$ which emacs
 /home/user1/local_build/bin/emacs
$ sudo tail -n 2 -F /root/.bashrc
 #fi
 export PATH=/home/user1/local_build/bin:$PATH
$ 
$ sudo emacs /etc/apache2/sites-available/000-default.conf
 sudo: emacs: command not found
$ 

Edit:

# whoami
 root
# command -v emacs
 /home/user1/local_build/bin/emacs
# exit
 exit
$ whoami
 user1
$ command -v emacs
 alias emacs='emacs -q --load ~/.emacs.d/init.el'
$

Edit

$ whoami
 user1
$ tail -n 2 -F .bashrc
 alias emacs='emacs -q --load ~/.emacs.d/init.el'

^C
$

Edit:

After an answer from Tigger, problem still exists,

$ tail -n 2 -F /home/user1/.bashrc
# alias emacs='emacs -q --load ~/.emacs.d/init.el'

^C
$ whoami # After logout/login bash
 user1
$ sudo emacs /etc/apache2/sites-available/000-default.conf
 sudo: emacs: command not found
$

Edit:

After 7_R3X answer,

# whoami
 root
# ls -ltr /etc/sudoers #Unable to modify this file
 -r--r----- 1 root root 755 Oct 14  2016 /etc/sudoers
# 

BTW, sudo gedit <etc's filename> works fine.

Question:

Why emacs does not get launch using sudo?

  • 1
    Because /home/user1/local_build/bin is not on root's PATH obviously! – cylgalad Jun 25 '17 at 7:06
  • 1
    Try sudo /home/user1/local_build/bin/emacs /etc/apache2/sites-available/000-default.conf instead of sudo emacs /etc/apache2/sites-available/000-default.conf and comment below the results. – 7_R3X Jun 25 '17 at 7:06
  • @7_R3X Uodated query – overexchange Jun 25 '17 at 7:24
  • @overexchange: I just updated my answer. Please check below. Thanks to steeldriver for sharing his knowledge. – 7_R3X Jun 25 '17 at 7:29
  • 1
    Isn't customization what .emacs is for? Did you want a more recent version than the one installed on the machine? – Faheem Mitha Jun 25 '17 at 8:06
4

cylgalad has mentioned the reason for your error in the comment that the path /home/user1/local_build/bin is not present in root's system variable PATH.

The reason of your problem is that the directory /home/user1/local_build/bin is not present in variable secure_path. To add this directory in secure_path, you need to edit /etc/sudoers file and change the content in it's Defaults content.

OR alternatively,

To solve the problem, you could either run emacs by explicitly mentioning it's full path in the command:

sudo /home/user1/local_build/bin/emacs /etc/apache2/sites-available/000-default.conf

or alternatively

you could add the path /home/user1/local_build/bin/ to root's PATH. Just put the folliwing line in your .bashrc file located at ~/root/ directory.

PATH=$PATH:~/home/user1/local_build/bin

Adding directory to root's PATH won't work for the reasons mentioned by steeldriver in the comment.

  • sudo is usually configured to use its own secure_path rather that root's PATH - see for example Why are PATH variables different when running via sudo and su? – steeldriver Jun 25 '17 at 7:19
  • @steeldriver : Oops! I didn't know that. I'll edit my answer. – 7_R3X Jun 25 '17 at 7:23
  • 1) your first line is contradicting with last line in the answer. 2) How to set sudo's secure_path? – overexchange Jun 25 '17 at 7:33
  • @overexchange: Forgot to edit it. Hold on a second. – 7_R3X Jun 25 '17 at 7:35
  • Sorry, why sudo has its own /etc/sudoers's secure_path instead of relying on /root/.bashrc's PATH variable? What is the reason to maintain secure_path in addition to root's PATH variable? Is sudo's version of executing command(emacs) does not mean to seek root privilege access to execute the command as mentioned here? BTW, am yet to verify your suggestion – overexchange Jun 25 '17 at 21:14
3

Classic example of why to not use which.

At the core of the issue is which. If you check what type emacs or command -v emacs says then your problem would be solved.

Edit: If you are trying to run as the root user, what happens when you try:

su
<password>
command -v emacs

Edit 2: That alias is the issue. You need to check ~/.bashrc or ~/.profile for that user. Either amended it or create a new alias would be my suggestion.

Edit 3: You are doing non-standard stuff, that is installing emacs in a users folder, then expecting to be able to execute it as root. This means you need a non-standard solution.

Option 1

Install emacs to /usr/local/bin - the correct location. Do not use a users folder to store applications you expect to be accessible to root.

Option 2

Use the full path when running emacs - as pointed out in the answer by 7_R3X. For example:

sudo /home/user1/local_build/bin/emacs

Option 3

Add the following alias:

alias emacs='/home/user1/local_build/bin/emacs'

This alias should be added to both /home/user1/.bashrc and /root/.bashrc. If /root/.bashrc does not exist, just create it.

Option 3 is not recommended (in my opinion) as it it starting to create a real mess of your system. It is also possible that root does not use the bash shell. I don't know why you have emacs in the users folder, so I have included this option.

Another option is to change the alias for Option 3 to something more custom like:

alias emacsuser1='/home/user1/local_build/bin/emacs'

or even

alias localemacs='/home/user1/local_build/bin/emacs'

This way you know you are running a non-standard install or custom version.

  • Query updated, after this answer – overexchange Jun 25 '17 at 8:14
  • alias coming from /home/user1/.bashrc – overexchange Jun 25 '17 at 8:27
  • After commenting it out, I get the path for emacs – overexchange Jun 25 '17 at 8:47
  • Edited query, problem still exists. Why commenting alias suppose to work? – overexchange Jun 25 '17 at 21:01
  • @overexchange See edit 3 for details. – Tigger Jun 26 '17 at 4:59
0

I think you may be able to get an idea of what's going on by stracing the execution of Emacs (sudo). Here is a nice and friendly explanation on how to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.