在 Ubuntu 中,要让 alias 永久生效,需要把它写入 shell 的配置文件。常见做法如下(以 bash 为例,zsh 会单独说明)。
echo $SHELL
nano ~/.bashrc
例如:
alias ll='ls -alF'
alias gs='git status'
alias update='sudo apt update && sudo apt upgrade'
Ctrl + O → 回车Ctrl + X然后执行:
source ~/.bashrc
✅ 以后每次打开终端都会自动生效。
如果你希望 alias 在 登录 shell 中生效(如 SSH 登录):
nano ~/.bash_profile
或
nano ~/.profile
添加:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
nano ~/.zshrc
添加 alias:
alias ll='ls -alF'
生效:
source ~/.zshrc
sudo nano /etc/bash.bashrc
⚠️ 不建议普通用户使用,修改错误可能影响系统。
alias
仅在当前终端有效:
alias ll='ls -alF'
关闭终端即失效 ✅
| 场景 | 配置文件 |
|---|---|
| bash(推荐) | ~/.bashrc |
| zsh | ~/.zshrc |
| 登录 shell | ~/.bash_profile / ~/.profile |
| 所有用户 | /etc/bash.bashrc |
如果你是 Ubuntu 桌面 / 服务器 / WSL,我也可以帮你精确到具体环境。