在 CentOS(以及大多数 Linux 发行版)里,alias(别名) 是否生效,取决于在哪定义以及在什么 shell 环境中使用。下面按“常见位置 + 生效范围”来说明。
alias ll='ls -l'
✅ 只在当前终端会话生效
❌ 关闭终端后失效
❌ 对其他用户、其他终端无效
编辑:
~/.bashrc
添加:
alias ll='ls -l'
让立即生效:
source ~/.bashrc
✅ 只对当前用户生效
✅ 打开新的终端自动加载
❌ 对非交互 shell(如 cron)不一定生效
~/.bash_profile
~/.profile
适合登录时一次性执行的内容。
/etc/bashrc
或
/etc/profile.d/alias.sh
推荐方式(清晰、好管理):
sudo vim /etc/profile.d/alias.sh
内容:
alias ll='ls -l'
赋权:
sudo chmod +x /etc/profile.d/alias.sh
✅ 所有用户生效
✅ 新开终端自动加载
比如:
ssh user@host command
默认不加载 .bashrc
解决:
alias ll='ls -l'
放进 ~/.bash_profile 或显式:
bash -i -c 'll'
CentOS 7 默认是 bash
CentOS 8+ 有些环境可能用 zsh
检查:
echo $SHELL
如果是 zsh:
~/.zshrc
脚本里默认 不展开 alias
需要:
shopt -s expand_aliases
source ~/.bashrc
type ll
alias
用户 alias → ~/.bashrc
全局 alias → /etc/profile.d/*
临时 alias → 当前终端
如果你愿意,可以告诉我:
我可以直接给你最合适的配置方式。