2018-03-02 22:47:46 +08:00
|
|
|
|
4 种绕过 Linux/Unix 命令别名的方法
|
2018-01-05 15:12:48 +08:00
|
|
|
|
======
|
2018-03-02 22:47:46 +08:00
|
|
|
|
|
2018-01-25 09:02:59 +08:00
|
|
|
|
我在我的 Linux 系统上定义了如下 mount 别名:
|
2018-03-02 22:47:46 +08:00
|
|
|
|
|
2018-01-05 15:12:48 +08:00
|
|
|
|
```
|
|
|
|
|
alias mount='mount | column -t'
|
|
|
|
|
```
|
|
|
|
|
|
2018-03-02 22:47:46 +08:00
|
|
|
|
但是我需要在挂载文件系统和其他用途时绕过这个 bash 别名。我如何在 Linux、*BSD、macOS 或者类 Unix 系统上临时禁用或者绕过 bash shell 呢?
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
2018-03-02 22:47:46 +08:00
|
|
|
|
你可以使用 `alias` 命令定义或显示 bash shell 别名。一旦创建了 bash shell 别名,它们将优先于外部或内部命令。本文将展示如何暂时绕过 bash 别名,以便你可以运行实际的内部或外部命令。
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
2018-03-02 22:47:46 +08:00
|
|
|
|
[![Bash Bypass Alias Linux BSD macOS Unix Command][1]][1]
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
2018-03-02 22:47:46 +08:00
|
|
|
|
### 4 种绕过 bash 别名的方法
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
2018-01-25 09:02:59 +08:00
|
|
|
|
尝试以下任意一种方法来运行被 bash shell 别名绕过的命令。让我们[如下定义一个别名][2]:
|
2018-03-02 22:47:46 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
alias mount='mount | column -t'
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-25 09:02:59 +08:00
|
|
|
|
运行如下:
|
2018-03-02 22:47:46 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
mount
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-25 09:02:59 +08:00
|
|
|
|
示例输出:
|
2018-03-02 22:47:46 +08:00
|
|
|
|
|
2018-01-05 15:12:48 +08:00
|
|
|
|
```
|
|
|
|
|
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
|
|
|
|
|
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
|
|
|
|
|
udev on /dev type devtmpfs (rw,nosuid,relatime,size=8023572k,nr_inodes=2005893,mode=755)
|
|
|
|
|
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
|
|
|
|
|
tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=1610240k,mode=755)
|
|
|
|
|
/dev/mapper/ubuntu--vg-root on / type ext4 (rw,relatime,errors=remount-ro,data=ordered)
|
|
|
|
|
/dev/sda1 on /boot type ext4 (rw,relatime,data=ordered)
|
|
|
|
|
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,relatime)
|
|
|
|
|
lxcfs on /var/lib/lxcfs type fuse.lxcfs (rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other)
|
|
|
|
|
```
|
|
|
|
|
|
2018-03-02 22:47:46 +08:00
|
|
|
|
#### 方法 1 - 使用 `\command`
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
2018-03-02 22:47:46 +08:00
|
|
|
|
输入以下命令暂时绕过名为 `mount` 的 bash 别名:
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
2018-03-02 22:47:46 +08:00
|
|
|
|
```
|
|
|
|
|
\mount
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 方法 2 - 使用 `"command"` 或 `'command'`
|
|
|
|
|
|
|
|
|
|
如下引用 `mount` 命令调用实际的 `/bin/mount`:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
"mount"
|
|
|
|
|
```
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
2018-01-25 09:02:59 +08:00
|
|
|
|
或者
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
2018-03-02 22:47:46 +08:00
|
|
|
|
```
|
|
|
|
|
'mount'
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 方法 3 - 使用命令的完全路径
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
2018-03-02 22:47:46 +08:00
|
|
|
|
使用完整的二进制路径,如 `/bin/mount`:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
/bin/mount
|
|
|
|
|
/bin/mount /dev/sda1 /mnt/sda
|
|
|
|
|
```
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
2018-03-02 22:47:46 +08:00
|
|
|
|
#### 方法 4 - 使用内部命令 `command`
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
2018-01-25 09:02:59 +08:00
|
|
|
|
语法是:
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
2018-03-02 22:47:46 +08:00
|
|
|
|
```
|
|
|
|
|
command cmd
|
|
|
|
|
command cmd arg1 arg2
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
要覆盖 `.bash_aliases` 中设置的别名,例如 `mount`:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
command mount
|
|
|
|
|
command mount /dev/sdc /mnt/pendrive/
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
[“command” 直接运行命令或显示][3]关于命令的信息。它带参数运行命令会抑制 shell 函数查询或者别名,或者显示有关给定命令的信息。
|
|
|
|
|
|
|
|
|
|
### 关于 unalias 命令的说明
|
|
|
|
|
|
|
|
|
|
要从当前会话的已定义别名列表中移除别名,请使用 `unalias` 命令:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
unalias mount
|
|
|
|
|
```
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
2018-01-25 09:02:59 +08:00
|
|
|
|
要从当前 bash 会话中删除所有别名定义:
|
2018-03-02 22:47:46 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
unalias -a
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
确保你更新你的 `~/.bashrc` 或 `$HOME/.bash_aliases`。如果要永久删除定义的别名,则必须删除定义的别名:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
vi ~/.bashrc
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-25 09:02:59 +08:00
|
|
|
|
或者
|
2018-03-02 22:47:46 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
joe $HOME/.bash_aliases
|
|
|
|
|
```
|
|
|
|
|
|
2018-01-25 09:02:59 +08:00
|
|
|
|
想了解更多信息,参考[这里][4]的在线手册,或者输入下面的命令查看:
|
2018-03-02 22:47:46 +08:00
|
|
|
|
|
2018-01-05 15:12:48 +08:00
|
|
|
|
```
|
|
|
|
|
man bash
|
|
|
|
|
help command
|
|
|
|
|
help unalias
|
|
|
|
|
help alias
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
via: https://www.cyberciti.biz/faq/bash-bypass-alias-command-on-linux-macos-unix/
|
|
|
|
|
|
|
|
|
|
作者:[Vivek Gite][a]
|
2018-01-25 09:02:59 +08:00
|
|
|
|
译者:[geekpi](https://github.com/geekpi)
|
2018-03-02 22:47:46 +08:00
|
|
|
|
校对:[wxy](https://github.com/wxy)
|
2018-01-05 15:12:48 +08:00
|
|
|
|
|
|
|
|
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
|
|
|
|
|
|
|
|
[a]:https://www.cyberciti.biz
|
|
|
|
|
[1]:https://www.cyberciti.biz/media/new/faq/2017/10/Bash-Bypass-Alias-Linux-BSD-macOS-Unix-Command.jpg
|
|
|
|
|
[2]:https://bash.cyberciti.biz/guide/Create_and_use_aliases
|
|
|
|
|
[3]:https://bash.cyberciti.biz/guide/Command
|
|
|
|
|
[4]:https://www.gnu.org/software/bash/manual/bash.html
|