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?

overexchange
  • 1,536
  • 1
    Because /home/user1/local_build/bin is not on root's PATH obviously! – cylgalad Jun 25 '17 at 07: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 07:06
  • @7_R3X Uodated query – overexchange Jun 25 '17 at 07:24
  • @overexchange: I just updated my answer. Please check below. Thanks to steeldriver for sharing his knowledge. – 7_R3X Jun 25 '17 at 07:29
  • Why do you have a local install of emacs? And what distribution? – Faheem Mitha Jun 25 '17 at 08:02
  • @FaheemMitha This approach is needed when you work on build server, where multiple users sitting on that build server can have their own emacs customisation. This hold good for any tool that you use on build server. I would not touch /usr/bin/emacs version – overexchange Jun 25 '17 at 08:03
  • 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 08:06
  • @FaheemMitha Some customisations in .emacs need newer version of emacs – overexchange Jun 25 '17 at 08:07

3 Answers3

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.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
7_R3X
  • 1,174
  • @overexchange: Forgot to edit it. Hold on a second. – 7_R3X Jun 25 '17 at 07: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
  • Query edited, after following your suggestion – overexchange Jun 25 '17 at 21:25