在 Linux 中,要让 alias 永久生效,需要把 alias 写入相应的配置文件中,具体取决于你使用的 shell 和用户范围。
echo $SHELL
常见结果:
/bin/bash → Bash/bin/zsh → Zsh编辑当前用户家目录下的配置文件:
vim ~/.bashrc
在文件末尾添加:
alias ll='ls -lah'
alias gs='git status'
保存后执行:
source ~/.bashrc
✅ 以后每次打开终端都会自动生效。
vim ~/.zshrc
添加 alias:
alias ll='ls -lah'
执行:
source ~/.zshrc
⚠️ 需要 root 权限,谨慎使用
sudo vim /etc/bash.bashrc
sudo vim /etc/zsh/zshrc
添加 alias 后保存即可。
如果你是通过 SSH 登录 或 非交互式 shell,可能还需要:
vim ~/.bash_profile
加入:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
alias
或
type ll
❌ 只在终端里直接敲 alias xxx='...'(重启失效)
✅ 必须写进配置文件
❌ 写在 ~/.profile 但用的是 Bash(不生效)
✅ Bash 用 ~/.bashrc
如果你告诉我:
我可以给你最精确的配置方式。