在 CentOS 中永久设置 alias(别名),关键是把 alias 写入对应用户的 shell 配置文件。下面按最常见场景详细说明。
CentOS 默认使用 bash,配置文件是:
~/.bashrc
使用任意编辑器打开:
vi ~/.bashrc
或:
nano ~/.bashrc
例如:
alias ll='ls -lh --color=auto'
alias gs='git status'
alias update='sudo yum update -y'
source ~/.bashrc
或直接:
. ~/.bashrc
✅ 之后每次登录都会自动生效。
有些情况下(如 SSH 登录).bashrc 可能不会被加载,建议同时加到:
~/.bash_profile
或:
~/.profile
示例:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
⚠️ 需要 root 权限
/etc/bashrc(推荐)sudo vi /etc/bashrc
添加:
alias ll='ls -lh --color=auto'
对所有用户生效。
sudo vi /etc/profile.d/myalias.sh
内容:
#!/bin/bash
alias ll='ls -lh --color=auto'
alias gs='git status'
赋予执行权限:
sudo chmod +x /etc/profile.d/myalias.sh
✅ 登录时自动加载,适合服务器环境。
alias
查看某个:
alias ll
unalias ll
从对应配置文件中删除 alias 行,然后:
source ~/.bashrc
zsh,要改 ~/.zshrcalias rm='rm -rf' 很危险✅ 个人用户:
~/.bashrc
✅ 服务器统一配置:
/etc/profile.d/xxx.sh
如果你愿意,我可以帮你: