Fedora Linux Support Community & Resources Center
  #1  
Old 3rd August 2017, 07:48 PM
lsatenstein Offline
Registered User
 
Join Date: Jun 2005
Location: Montreal, Que, Canada
Posts: 4,354
linuxfedorafirefox
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
Reply With Quote
  #2  
Old 4th August 2017, 08:57 AM
srakitnican Online
Registered User
 
Join Date: Oct 2011
Posts: 1,584
linuxchrome
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
Reply With Quote
  #3  
Old 4th August 2017, 11:37 AM
dswaner Offline
Registered User
 
Join Date: Nov 2008
Posts: 308
linuxfedorafirefox
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.
Reply With Quote
  #4  
Old 4th August 2017, 07:08 PM
dd_wizard's Avatar
dd_wizard Offline
Registered User
 
Join Date: Sep 2009
Posts: 2,113
linuxfedorafirefox
Re: Is there a way to add alias's to sudo ?

Quote:
Originally Posted by dswaner View Post
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
Reply With Quote
  #5  
Old 4th August 2017, 09:02 PM
dswaner Offline
Registered User
 
Join Date: Nov 2008
Posts: 308
linuxfedorafirefox
Re: Is there a way to add alias's to sudo ?

Quote:
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?
Reply With Quote
  #6  
Old 4th August 2017, 09:16 PM
dd_wizard's Avatar
dd_wizard Offline
Registered User
 
Join Date: Sep 2009
Posts: 2,113
linuxfedorafirefox
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
Reply With Quote
  #7  
Old 4th August 2017, 09:47 PM
dswaner Offline
Registered User
 
Join Date: Nov 2008
Posts: 308
linuxfedorafirefox
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
Reply With Quote
  #8  
Old 4th August 2017, 10:04 PM
dd_wizard's Avatar
dd_wizard Offline
Registered User
 
Join Date: Sep 2009
Posts: 2,113
linuxfedorafirefox
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
Reply With Quote
  #9  
Old 4th August 2017, 10:33 PM
dswaner Offline
Registered User
 
Join Date: Nov 2008
Posts: 308
linuxfedorafirefox
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
Quote:
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
Reply With Quote
  #10  
Old 5th August 2017, 03:55 AM
lsatenstein Offline
Registered User
 
Join Date: Jun 2005
Location: Montreal, Que, Canada
Posts: 4,354
linuxfedorafirefox
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
Reply With Quote
  #11  
Old 5th August 2017, 01:37 PM
dswaner Offline
Registered User
 
Join Date: Nov 2008
Posts: 308
linuxfedorafirefox
Re: Is there a way to add alias's to sudo ?

Are there any security implications for this kluge?
Reply With Quote
  #12  
Old 5th August 2017, 10:20 PM
dd_wizard's Avatar
dd_wizard Offline
Registered User
 
Join Date: Sep 2009
Posts: 2,113
linuxfedorafirefox
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
Reply With Quote
  #13  
Old 12th October 2017, 05:09 PM
dswaner Offline
Registered User
 
Join Date: Nov 2008
Posts: 308
linuxfedorafirefox
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.
Reply With Quote
  #14  
Old 12th October 2017, 09:04 PM
lsatenstein Offline
Registered User
 
Join Date: Jun 2005
Location: Montreal, Que, Canada
Posts: 4,354
linuxfedorafirefox
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
Reply With Quote
  #15  
Old 12th October 2017, 09:44 PM
dswaner Offline
Registered User
 
Join Date: Nov 2008
Posts: 308
linuxfedorafirefox
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.
Reply With Quote
Reply

Tags
add , alias , sudo

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


Current GMT-time: 15:40 (Sunday, 22-10-2017)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat