Linux下如何针对特定TTYn永久设置不同的登录默认Shell?
Absolutely, this is totally feasible! Here are two reliable methods to achieve this, depending on whether you want a user-specific or system-wide solution:
User-Specific Solution (No Root Required)
This method sets bash as your default login shell, then switches to csh automatically only when you log into tty7.
Set bash as your default shell:
Run this command and enter your password when prompted:chsh -s /bin/bash(Replace
/bin/bashwith the actual path to bash on your system—you can find it withwhich bash.)Add a tty check to your bash login script:
Bash reads either~/.bash_profile(preferred for login shells) or~/.profileif.bash_profiledoesn't exist. Open the appropriate file in an editor:nano ~/.bash_profile(Or use
~/.profileif needed.)Add the following lines at the top of the file:
# Switch to csh if logging into tty7 if [ "$(tty)" = "/dev/tty7" ]; then exec /bin/csh -l fi- The
execcommand replaces the bash process with csh, so exiting csh will log you out directly (no returning to bash). - The
-lflag tells csh to run as a login shell, so it loads its configuration files like~/.cshrcand~/.login.
- The
Test it:
Log out, then log into tty7—you should land in csh. Log into any other tty or via SSH, and you'll be in bash.
System-Wide Solution (Requires Root Access)
If you want to apply this to all users, you can modify the system's tty configuration. Note that this depends on your init system:
For systems using systemd:
Create a custom service file for tty7:
sudo mkdir -p /etc/systemd/system/getty@tty7.service.d sudo nano /etc/systemd/system/getty@tty7.service.d/override.confAdd the following content to set csh as the login shell for tty7:
[Service] ExecStart= ExecStart=-/sbin/agetty --login-program /bin/csh -o '-p -f %n' --noclear %I $TERMReload systemd and restart the tty7 service:
sudo systemctl daemon-reload sudo systemctl restart getty@tty7.service
For older systems using SysV init:
Edit /etc/inittab and find the line for tty7 (it looks like 7:2345:respawn:/sbin/getty 38400 tty7). Modify it to specify csh as the login shell:
7:2345:respawn:/sbin/getty -l /bin/csh 38400 tty7
Then run sudo init q to apply the changes.
Notes
- If tty7 runs a graphical display manager (like GDM or LightDM), the text login method above won't affect your graphical session's terminal shells. For those, set the default shell in your terminal emulator's preferences.
- Make sure
/bin/cshis installed on your system—if not, install it using your package manager (e.g.,sudo apt install cshon Debian/Ubuntu).
内容的提问来源于stack exchange,提问作者Anonymous

