FedoraForum.org - Fedora Support Forums and Community
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 17

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    4,600

    Is there a way to add alias's to sudo ?

    Fedora's default list of alias's for /root are
    alias cp='cp -i'
    alias egrep='egrep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias grep='grep --color=auto'
    alias l.='ls -d .* --color=auto'
    alias ll='ls -l --color=auto'
    alias ls='ls --color=auto'
    alias mc='. /usr/libexec/mc/mc-wrapper.sh'
    alias mv='mv -i'
    alias rm='rm -i'
    alias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot'
    alias xzegrep='xzegrep --color=auto'
    alias xzfgrep='xzfgrep --color=auto'
    alias xzgrep='xzgrep --color=auto'
    alias zegrep='zegrep --color=auto'
    alias zfgrep='zfgrep --color=auto'
    alias zgrep='zgrep --color=auto'

    I added the above to my ~./bashrc and verified that they were enforced. I did that as I thought they were useful

    However, when I run sudo cp a b
    and then redo sudo cp a b

    the alias's are not inspected or evaluated. In otherwords, the protection for accidental over-write are not enforced.

    Is there a way to enforce same with sudo?
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  2. #2
    Join Date
    Oct 2011
    Posts
    1,630

    Re: Is there a way to add alias's to sudo ?

    The trick is to add more alias!
    Code:
    alias sudo='sudo '
    It works, no joke

    Code:
    $ alias sgh="echo 'I am alias'"
    $ sgh
    I am alias
    $ sudo sgh
    I am alias
    https://askubuntu.com/a/22043

  3. #3
    Join Date
    Nov 2008
    Posts
    343

    Re: Is there a way to add alias's to sudo ?

    sudo -i

    will open an interactive (login) root shell, in which the aliases are available.

  4. #4
    Join Date
    Sep 2009
    Posts
    2,127

    Re: Is there a way to add alias's to sudo ?

    Quote Originally Posted by dswaner
    sudo -i

    will open an interactive (login) root shell, in which the aliases are available.
    This doesn't work for me, even after changing "Defaults env_reset" to "Defaults !env_reset" with visudo. However, the previous post worked, defining the alias sudo='sudo ' in .bashrc.

    dd_wizard

  5. #5
    Join Date
    Nov 2008
    Posts
    343

    Re: Is there a way to add alias's to sudo ?

    Originally posted by dd_wizard

    ... This doesn't work for me ...
    Sounds like you don't have any aliases defined for root.

    What does your "/root/.bashrc" look like?

    Or, same thing: when logged in as root, what does "alias" show?

  6. #6
    Join Date
    Sep 2009
    Posts
    2,127

    Re: Is there a way to add alias's to sudo ?

    They're set in /root/.bashrc:
    Code:
    [root@Mobile-PC ~]# alias                                                                      
    alias ..='cd ..'
    alias dir='dir -h --color=auto'
    alias dl='dir -hl --color=auto'
    alias egrep='egrep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias grep='grep --color=auto'
    alias l.='ls -d .* --color=auto'
    alias ll='ls -hl --color=auto'
    alias lld='ls -dhl'
    alias ls='ls --color=auto'
    $ sudo -i ll
    -bash: ll: command not found

    So I'm not sure what's up here. As I said, I toggled env_reset with visudo, and nothing changed.

    dd_wizard

  7. #7
    Join Date
    Nov 2008
    Posts
    343

    Re: Is there a way to add alias's to sudo ?

    For some reason, you are missing the usual root aliases for:

    alias cp='cp -i'
    alias mv='mv -i'
    alias rm='rm -i'

    The missing cp alias is causing the failure for lsatenstein's test case, after doing a
    Code:
    $ sudo -i

  8. #8
    Join Date
    Sep 2009
    Posts
    2,127

    Re: Is there a way to add alias's to sudo ?

    This is odd! I added those, and nothing changed as I expected. However, this indicates "sudo -i ll" should work for me.
    Code:
    $ sudo alias
    No aliases are listed, output is empty.
    
    $ sudo -i alias
    alias ..='cd ..'                                                                               
    alias cp='cp -i'
    alias dir='dir -h --color=auto'
    alias dl='dir -hl --color=auto'
    alias egrep='egrep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias grep='grep --color=auto'
    alias l.='ls -d .* --color=auto'
    alias ll='ls -hl --color=auto'
    alias lld='ls -dhl'
    alias ls='ls --color=auto'
    alias mv='mv -i'
    alias rm='rm -i'
    
    But:
    $ sudo -i ll
    -bash: ll: command not found
    dd_wizard

  9. #9
    Join Date
    Nov 2008
    Posts
    343

    Re: Is there a way to add alias's to sudo ?

    What works for me is:
    Code:
    $ sudo -i
    without specifying any command, such as "ll".
    That takes you into a root login session, with a "#" prompt.
    Then if you do the "cp /tmp/a /tmp/b" twice, the second time it will prompt
    cp: overwrite '/tmp/b'?
    However, looking at the sudo man page, what you are doing (sudo -i ll) should also work, but it does
    not work for me either. That looks like a bug in sudo to me.

    1478651
    Last edited by dswaner; 5th August 2017 at 05:24 PM. Reason: Add link to bug report

  10. #10
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    4,600

    Re: Is there a way to add alias's to sudo ?

    what works is
    a) edit ~/.bashrc
    add
    alias sudo='sudo '

    sudo with one space in the alias command does it.
    Google for the write up about it or do man sudo.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  11. #11
    Join Date
    Nov 2008
    Posts
    343

    Re: Is there a way to add alias's to sudo ?

    Are there any security implications for this kluge?

  12. #12
    Join Date
    Sep 2009
    Posts
    2,127

    Re: Is there a way to add alias's to sudo ?

    It's documented in the man page for alias. All that happens is the command following sudo is expanded if it's one of your aliases before sudo is executed. So, if you trust your own aliases, sudo should be able to.

    dd_wizard

  13. #13
    Join Date
    Nov 2008
    Posts
    343

    Re: Is there a way to add alias's to sudo ?

    Got a definitive answer from the upstream sudo folks:

    From the bash manual:

    Aliases are not expanded when the shell is not interactive, unless the
    expand_aliases shell option is set using shopt (see the description of
    shopt under SHELL BUILTIN COMMANDS below).

    When you run a command via "sudo -i" the shell is not in interactive
    mode so aliases are not applied. If you use a shell function instead
    of an alias this is not an issue. Adding "shopt -s expand_aliases" to
    the top of .bash_profile should give you the behavior you want.

  14. #14
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    4,600

    Re: Is there a way to add alias's to sudo ?

    What do you get with
    [leslie@laptop ~]$ alias sudo='sudo '
    [leslie@laptop ~]$ sudo su
    [root@laptop leslie]# alias
    alias cp='cp -i'
    alias egrep='egrep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias grep='grep --color=auto'
    alias l.='ls -d .* --color=auto'
    alias ll='ls -l --color=auto'
    alias ls='ls --color=auto'
    alias mc='. /usr/libexec/mc/mc-wrapper.sh'
    alias mv='mv -i'
    alias rm='rm -i'
    alias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot'
    alias xzegrep='xzegrep --color=auto'
    alias xzfgrep='xzfgrep --color=auto'
    alias xzgrep='xzgrep --color=auto'
    alias zegrep='zegrep --color=auto'
    alias zfgrep='zfgrep --color=auto'
    alias zgrep='zgrep --color=auto'
    [root@laptop leslie]#

    In a shell script without #!/bin/bash are the alias's available to the script
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  15. #15
    Join Date
    Nov 2008
    Posts
    343

    Re: Is there a way to add alias's to sudo ?

    Sorry, but the "definitive solution" that I posted doesn't work. The bash manual does indeed indicate that the
    "expand_aliases" shopt should do the trick. However, it does not work on Fedora 26. I put "shopt -s
    expand_aliases" in both .bash_profile and .bashrc - logged out and in, and "sudo -i ll" still gives "bash: ll:
    command not found" I will re-open Bug 1478651 on Red Hat bugzilla, and comment on bug 806 at
    bugzilla.sudo.ws.

Page 1 of 2 1 2 LastLast

Similar Threads

  1. [SOLVED] sudo NOPASSWD to .bashrc alias?
    By Bazu135 in forum Security and Privacy
    Replies: 6
    Last Post: 3rd September 2011, 09:31 AM
  2. Cannot open /var/db/sudo after sudo package upgrade
    By Replicant10000 in forum Security and Privacy
    Replies: 1
    Last Post: 16th September 2010, 01:25 PM
  3. How to Change Firefox Alias or any Alias?
    By FergatROn in forum Using Fedora
    Replies: 1
    Last Post: 3rd June 2008, 09:18 PM
  4. alias help
    By madjac in forum Using Fedora
    Replies: 0
    Last Post: 5th May 2007, 09:44 PM
  5. sudo question (host alias)
    By arc2v in forum Using Fedora
    Replies: 2
    Last Post: 23rd June 2005, 01:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •