【发布时间】:2011-06-16 13:35:54
【问题描述】:
~/.bashrc、~/.bash_login、~/.bash_logout、~/.bash_profile、~/.profile、/etc/profile、/etc/bash.bashrc、/etc/ssh/ssh_config有什么区别和 sshd_config,它们是什么时候加载的,它们的目的是什么?
【问题讨论】:
~/.bashrc、~/.bash_login、~/.bash_logout、~/.bash_profile、~/.profile、/etc/profile、/etc/bash.bashrc、/etc/ssh/ssh_config有什么区别和 sshd_config,它们是什么时候加载的,它们的目的是什么?
【问题讨论】:
bash 的手册页说 bash shell 有以下初始化文件:
/etc/profile
The systemwide initialization file, executed for login shells
/etc/bash.bashrc
The systemwide per-interactive-shell startup file
/etc/bash.bash.logout
The systemwide login shell cleanup file, executed when a login shell exits
~/.bash_profile
The personal initialization file, executed for login shells
~/.bashrc
The individual per-interactive-shell startup file
~/.bash_logout
The individual login shell cleanup file, executed when a login shell exits
~/.inputrc
Individual readline initialization file
显然,不同的 shell(bash、zsh、csh 等)似乎有不同的配置文件。似乎有与不同的 linux 和 unix 版本一样多的 shell:csh、ksh、bash、zsh,... Bash 有一个.bashrc,Zsh 有一个.zshrc,等等。人们还可以区分登录 shell 和非登录 shell -login shell 以及系统范围的默认值和用户特定的默认值之间。
区分 login 和 non-login shell 是有意义的,因为有些命令应该只在登录时处理,而另一些命令应该在每次打开新的时候运行终端窗口。这就是.bash_profile and .bashrc 之间的区别。对于 bash,每次启动新的 bash 副本时都会重新加载 .bashrc,即当您启动新的 bash 但未登录时。 .bash_profile 或 .profile 仅在您登录时加载。 bashrc 中的缩写 rc 代表“运行命令”或“运行控制”,是旧 Unix 系统采用的约定。
系统范围的默认值..
/etc/profile ..login shells,用于登录的交互式 shell/etc/bashrc ..non-login Bash shell 主目录中用户特定的默认值 ~ for..
~/.profile ..login shell,登录后调用~/.bashrc ..non-login shells,如果已经登录了~/.bash_profile ..login shell,登录后调用(优先级较低)主目录中用于登录和注销的用户特定默认值
~/.bash_login ..login shell(登录时调用)~/.bash_logout ..login shell(注销时调用)以下链接很有帮助:.bashrc vs .bashprofile 和 .bash_profile vs .bashrc、bash manual page(man bash)和 Zsh/Bash startup files loading order (.bashrc, .zshrc etc.)。
【讨论】:
我碰巧对这些文件很好奇,自己做了一些实验。结果与文档中的内容略有不同。
我知道交互式和非交互式或登录和非登录之间的区别。
我在两台计算机上进行了尝试,我的 macbook pro 的 OS 10.9 和一个 ubuntu 服务器 13.10 的服务器。我将以下命令添加到 /etc/profile 中:
echo "Loading /etc/profile"
和类似的命令进入 /etc/bash.bashrc、/etc/bashrc、/etc/bash.bashrc、~/.profile、~/.bash_profile、~/.bashrc、~/.bash_login 并确保这些文件在自身内部不会相互获取源。
(OS 10.9, GNU bash, 版本 3.2.51(1)-release (x86_64-apple-darwin13)) 在 mac 上,使用交互式登录 bash,我有:
Loading /etc/profile
Loading ~/.bash_profile
也就是说直接加载的文件只有/etc/profile和~/.bash_profile。
使用交互式非登录 bash,我有:
Loading ~/.bashrc
表示直接加载的文件是~/.bashrc。
(ubuntu 服务器 13.10 GNU bash,版本 4.2.45(1)-release (x86_64-pc-linux-gnu)) 在 ubuntu 上,使用交互式登录 bash,我有:
Loading /etc/profile
Loading ~/.bash_profile
也就是说直接加载的文件只有/etc/profile和~/.bash_profile。
使用交互式非登录 bash,我有:
Loading /etc/bash.bashrc
Loading ~/.bashrc
表示直接加载的文件是/etc/bash.bashrc和~/.bashrc。
不知道为什么~
【讨论】: