如何在 sudo 提权后使用别名命令

公众号关注 「奇妙的 Linux 世界」

设为「星标」,每天带你玩转 Linux !

8f419a3569fcbd5cd8d078d1ae2d935f.png

一般来说,sudo会忽略通过.bashrc文件、.bash_aliases文件或者alias命令设置的别名命令(aliased commands)。

比如,我们经常将ll用作ls -lh命令的别名。然后,我们输入ll,终端将会返回一个关于当前目录的长列表。但是,当我们输入sudo ll时,终端将会返回:

$ sudo ll
=>  sudo: ll: command not found

解决方案

我们给shutdown命令创建一个别名,当rotorrent运行的时候尝试输入这个别名去关机,我们可以看到系统不会关机。想要运行/sbin/shutdown需要root权限,然而sudo会完全忽略shutdown的这个别名。解决办法是,我们需要添加另一个别名:

alias sudo='sudo '

sudo后面的那个空格将会告诉bash,去检查跟在空格后面的命令是否也是一个别名。bash手册(通过man bash查看)上面是这么描述的:

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. 如果别名值的最后一个字符是空格,将会检查”跟在别名后的下一个命令”是否也是别名扩展。

下面是我机器.bash_aliases文件中的一些别名设置(因为系统才安装,可能以后会加更多别名设置):

# Shortcuts
alias ll='ls -lh'
alias la='ls -lhA'
alias l='ls'
alias c='clear'
alias x='exit'
alias q='exit'

# Don't run shutdown if rtorrent is running - as long as there's a screen with "tor" in its name, shutdown won't run (unless you call /sbin/shutdown, or unalias it)
alias shutdown='/home/james/scripts/safe.shutdown.sh'

# When using sudo, use alias expansion (otherwise sudo ignores your aliases)
alias sudo='sudo '

参考文档

  1. Using sudo with an alias

本文转载自:「 Legolas' Blogs 」,原文:https://url.hi-linux.com/lAqFF/ ,版权归原作者所有。欢迎投稿,投稿邮箱: editor@hi-linux.com。

1378dd6a2c87ba0c384c44a9aae736c4.gif

最近,我们建立了一个技术交流微信群。目前群里已加入了不少行业内的大神,有兴趣的同学可以加入和我们一起交流技术,在 「奇妙的 Linux 世界」 公众号直接回复 「加群」 邀请你入群。

dc246a42549c1f28a6976ec97becf642.png

你可能还喜欢

点击下方图片即可阅读

2493f94ab2b9c1a814ff9eac53ab531a.png

SQLite 的文艺复兴

f74685c94edc06f65e3f2bc2d0fe923a.png
点击上方图片,『美团|饿了么』外卖红包天天免费领

b14eed8cec9c940ad209da5c4726d31e.png

更多有趣的互联网新鲜事,关注「奇妙的互联网」视频号全了解!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值