时间:2023-02-16 11:29
人气:
作者:admin
一般来说,sudo会忽略通过.bashrc文件、.bash_aliases文件或者alias命令设置的别名命令(aliased commands)。
比如,我们经常将ll用作ls -lh命令的别名。然后,我们输入ll,终端将会返回一个关于当前目录的长列表。但是,当我们输入sudo ll时,终端将会返回:
$sudoll
=>sudo:ll:commandnotfound
我们给shutdown命令创建一个别名,当普通用户运行的时候尝试输入这个别名去关机,我们可以看到系统不会关机。想要运行/sbin/shutdown需要root权限,然而sudo会完全忽略shutdown的这个别名。解决办法是,我们需要添加另一个别名:
aliassudo='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
aliasll='ls-lh'
aliasla='ls-lhA'
aliasl='ls'
aliasc='clear'
aliasx='exit'
aliasq='exit'
#Don'trunshutdownifrtorrentisrunning-aslongasthere'sascreenwith"tor"initsname,shutdownwon'trun(unlessyoucall/sbin/shutdown,orunaliasit)
aliasshutdown='/home/james/scripts/safe.shutdown.sh'
#Whenusingsudo,usealiasexpansion(otherwisesudoignoresyouraliases)
aliassudo='sudo'
审核编辑 :李倩
上一篇:嵌入式C语言开发基础
下一篇:最全的Nginx日志分割教程