温馨提示×

linux aliases如何持久化

小樊
43
2026-09-26 09:27:19
栏目: 智能运维

在 Linux 中,alias 默认只在当前 shell 会话有效,要“持久化”需要写入 shell 的配置文件。下面按常见 shell 说明。


1. 确定你使用的 shell

echo $SHELL

常见结果:

  • /bin/bash → Bash
  • /bin/zsh → Zsh
  • /bin/fish → Fish

2. Bash 持久化 alias

编辑用户级配置文件(推荐):

nano ~/.bashrc

在文件末尾添加:

alias ll='ls -lh'
alias gs='git status'

保存后生效:

source ~/.bashrc

如果是登录 shell(如 SSH 登录),也可以写入:

~/.bash_profile

(很多发行版会在 .bash_profile 里 source .bashrc)


3. Zsh 持久化 alias

nano ~/.zshrc

添加:

alias ll='ls -lh'

生效:

source ~/.zshrc

4. Fish 持久化 alias

Fish 不叫 alias,叫 abbr 或 alias 函数:

alias ll 'ls -lh'

Fish 会自动写入:

~/.config/fish/config.fish

或者直接用:

abbr ll 'ls -lh'

5. 系统级(所有用户)

不推荐,但可用:

  • Bash: /etc/bash.bashrc
  • Zsh: /etc/zsh/zshrc

6. 验证

type ll

或

alias

7. 常见坑

  • ❌ 只在终端里敲 alias xxx='...'(重启失效)
  • ❌ 写错配置文件(bash 写了 zshrc)
  • ❌ 没有 source

如果你告诉我具体 shell + 发行版,我可以给你最精确的命令。

0 踩