在 Ubuntu 里,aliases 不保存通常是因为你只在当前 shell 里定义了 alias,没有写进配置文件,或者写错文件了。下面按最常见原因给你说明。
alias ll='ls -l'
这个 只对当前终端会话有效,关掉终端就没了。
✅ 解决:写进配置文件(见下面)
不同 shell 用的文件不一样:
| Shell | 配置文件 |
|---|---|
| bash | ~/.bashrc |
| zsh | ~/.zshrc |
| sh | ~/.profile |
如果你用的是 bash(Ubuntu 默认),但写到了 .bash_profile 或 .profile,可能不生效。
✅ 正确做法:
nano ~/.bashrc
在最后加:
alias ll='ls -l'
然后:
source ~/.bashrc
有些情况(如 scp、脚本、某些 IDE 终端)不会加载 .bashrc。
✅ 确保终端是交互式登录 shell
有些系统会在登录时覆盖 .bashrc。
✅ 检查是否被覆盖:
alias ll
如果没有,说明配置没加载。
sudo ll
❌ alias 不会继承到 sudo
✅ 用:
sudo ls -l
~/.bashrcecho "alias ll='ls -l'" >> ~/.bashrc
source ~/.bashrc
type ll
sudo nano /etc/bash.bashrc
加入 alias(不推荐新手改)
echo $SHELL)source 吗?如果你愿意,可以把你具体的操作命令 + 用的 Ubuntu 版本 + 终端类型发出来,我可以精确告诉你哪里错了。