You are not logged in.

#1 2009-12-27 23:12:26

anonymous_user
Member
Registered: 2009-08-28
Posts: 3,059

enabling aliases for sudo [Solved]

How I can use sudo with my aliases? For example I have an alias ka='killall -9' but when I run it with sudo, "ka" is not recognized as a command.

Last edited by anonymous_user (2009-12-28 19:19:47)

Offline

#2 2009-12-27 23:29:53

Misfit138
Misfit Emeritus
From: USA
Registered: 2006-11-27
Posts: 4,189

Re: enabling aliases for sudo [Solved]

Export your aliases globally, or put them in /etc/profile, or in both root and non-root's .bashrc....

Offline

#3 2009-12-27 23:53:01

anonymous_user
Member
Registered: 2009-08-28
Posts: 3,059

Re: enabling aliases for sudo [Solved]

Misfit138 wrote:

Export your aliases globally

How do I do that?

Misfit138 wrote:

or in both root and non-root's .bashrc....

I already did that.

Offline

#4 2009-12-28 00:50:25

M177ER
Member
Registered: 2008-06-15
Posts: 148

Re: enabling aliases for sudo [Solved]

You could also try:

alias ka='sudo killall -9'

Offline

#5 2009-12-28 00:58:11

anonymous_user
Member
Registered: 2009-08-28
Posts: 3,059

Re: enabling aliases for sudo [Solved]

I don't need to sudo the command every time though.

Oh well, if there no solution I will just create a separate alias.

Offline

#6 2009-12-28 01:04:25

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: enabling aliases for sudo [Solved]

This may be a silly question, but have you either sourced the changed files or logged out and in since you made the changes?

Offline

#7 2009-12-28 01:25:55

chpln
Member
From: Australia
Registered: 2009-09-17
Posts: 361

Re: enabling aliases for sudo [Solved]

You could create a function to handle the privilege requirements for you.  Perhaps something like:

justkill () {
    pusers=$(ps -eo "%u%c" | grep -e " $1\$" | cut -d\  -f1)
    altuser=$(echo "$pusers" | grep -v "$USER")
    if [ ! -n "$pusers" ]; then
        echo "$1: no process found"
        exit 1
    fi
    if [ -n "$altuser" -a "$UID" -ne 0 ]; then
        sudo killall -9 "$1"
    else
        killall -9 "$1"
    fi
}

alias ka='justkill'

Last edited by chpln (2009-12-28 05:44:10)

Offline

#8 2009-12-28 01:26:09

kazuo
Member
From: São Paulo/Brazil
Registered: 2008-03-18
Posts: 413
Website

Re: enabling aliases for sudo [Solved]

set the follow alias (works on zsh, I think that works in bash too)

alias sudo='sudo '

And you can use you alias with sudo

Last edited by kazuo (2009-12-28 01:26:28)

Offline

#9 2009-12-28 05:38:17

JohannesSM64
Member
From: Norway
Registered: 2009-10-11
Posts: 623
Website

Re: enabling aliases for sudo [Solved]

But why does that solve it?

Offline

#10 2009-12-28 05:46:26

mikesd
Member
From: Australia
Registered: 2008-02-01
Posts: 788
Website

Re: enabling aliases for sudo [Solved]

kazuo wrote:

... I think that works in bash too

Confirmed on BASH 4.0. Works fine.

Offline

#11 2009-12-28 05:51:30

chpln
Member
From: Australia
Registered: 2009-09-17
Posts: 361

Re: enabling aliases for sudo [Solved]

JohannesSM64 wrote:

But why does that solve it?

Bash Info Manual - 6.6 Aliases wrote:

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

The fact that I come across nuances like this so regularly make me think it might be worth-while to sit and read the entire manual. wink

Clever solution, kazuo.

Last edited by chpln (2009-12-28 05:55:01)

Offline

#12 2009-12-28 19:19:34

anonymous_user
Member
Registered: 2009-08-28
Posts: 3,059

Re: enabling aliases for sudo [Solved]

Thank you kazuo.

Offline

Board footer

Powered by FluxBB