38

I have tried putting my aliases in ~/.bash_profile, ~/.bashrc, /etc/profile, and /etc/bashrc.

I am still unable to execute the following:

alias zf2="php public/index.php"

and then execute:

sudo zf2 orm:info

The issue seems to be that I am unable to specify an alias as a command using visudo-- which causes a syntax error.

So I am unable to call:

sudo zf2 orm:info

However, I was able to create a script at /usr/share/scripts/zf2 which contains:

#!/bin/bash

alias zf2="php public/index.php"
zf2 $1

and add this script as the command in visudo. When this script is in the end user's PATH I am able to execute

zf2 orm:info

I have different aliases like zf2 that I need to expose to the end user. I would prefer to maintain alias instead of a collection of scripts.

  • 1
    Unfortunately I want to use aliases in visudo and I am unable. – Michael Niño Jun 21 '17 at 2:57
  • 2
    That is not possible. Aliases are handled by the shell (in your case bash) and expanded before being sent to the application. sudo itself knows nothing about aliases. – Steven Jun 21 '17 at 3:49
  • 2
    Because @Steven is technically correct, you should clarify whether you simply want to expand aliases prior to sudo being passed the line, or whether you want the aliases expanded in the elevated environment. Pedantry isn't merely a way to elicit eye-rolls. (Wow, but that last sentence doesn't look kosher at all.) – can-ned_food Jun 21 '17 at 6:44
  • 3
    Possible duplicate of How can I alias a command for sudo? – Gilles Jun 21 '17 at 9:26
  • 1
    Try it with sudo -E – Denis de Bernardy Jun 21 '17 at 10:33
66

Ironically, the solution is to call sudo from an alias.

alias sd="sudo "

Note: While not recommended, you could name the alias sudo: alias sudo="sudo "

Bash Reference Manual (Aliases)

If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.

  • 7
    I can maybe see reasons why someone would and would not want this done as the default for any system. The convenience and expectation as asked, versus being more careful with elevated privileges. – Pysis Jun 20 '17 at 2:13
  • 5
    alias sd="sudo " maychance? – The Nate Jun 20 '17 at 3:00
  • After reviewing my aliases I noticed that I did not have alias sudo="sudo " in the end users environment. This is the answer – Michael Niño Jun 21 '17 at 22:55

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.