在 CentOS 中,想让 alias(别名)永久生效,需要把它写入 shell 的配置文件 中。常见做法如下:
vi ~/.bashrc
例如:
alias ll='ls -lh'
alias cls='clear'
source ~/.bashrc
✅ 只对当前用户生效
✅ 重启或重新登录仍然有效
/etc/bashrc(推荐)vi /etc/bashrc
在末尾添加:
alias ll='ls -lh'
alias rm='rm -i'
使其生效:
source /etc/bashrc
✅ 对所有用户生效
✅ 仅对 bash shell 有效
/etc/profile.d/ 脚本(规范做法 ✅)vi /etc/profile.d/alias.sh
内容:
#!/bin/bash
alias ll='ls -lh'
alias grep='grep --color=auto'
赋予执行权限:
chmod +x /etc/profile.d/alias.sh
✅ 系统级
✅ 管理规范、易维护
| Shell | 配置文件 |
|---|---|
| bash | ~/.bashrc / /etc/bashrc |
| zsh | ~/.zshrc |
| fish | ~/.config/fish/config.fish |
alias ll='ls -lh'
❌ 退出终端即失效
alias
| 场景 | 推荐方式 |
|---|---|
| 个人使用 | ~/.bashrc |
| 服务器统一配置 | /etc/profile.d/alias.sh |
| 临时测试 | 直接 alias |
如果你用的是 CentOS 7 / 8 / Stream 或 zsh,可以告诉我,我可以给你更精确的配置方式。