大网数据广告活动
服务器运维问答

SSH login: automatically goto last directory visited?

· 52CE 我爱测

During my multitasking days, sometimes I will be booted off of an SSH session for one reason or another (idle timeouts, etc), while I am working on another task. When I return to the terminal, I will find myself back at localhost…

问题原文

原帖链接 · Jake Wilson · CC BY-SA 3.0

During my multitasking days, sometimes I will be booted off of an SSH session for one reason or another (idle timeouts, etc), while I am working on another task. When I return to the terminal, I will find myself back at localhost, needing to SSH back to the server. Once back at the server, I'm always in my ~ home directory by default and I need to navigate back to the directory I was at last (if I can remember it) in order to continue working.

Is it possible to automatically return to the last directory I was working in when I login?

I was thinking a homebrew solution would be aliasing cd to change directories but also save that directory into some environment variable, and then changing to that directory in my .bash_profile.

But is there any sort of functionality like this already in Linux?

社区高分解答

原帖链接 · 200_success · CC BY-SA 3.0

Instead of solving the cd persistence problem, you should consider solutions to resume your shell session.

  • tmux
    • Inside SSH, run tmux to start a new session
    • If disconnected, SSH in again, and run tmux attach to resume the session
    • Inside a tmux session, you can press CtrlB, then D to detach
  • screen
    • Inside SSH, run screen to start a new session
    • If disconnected, SSH in again, and run screen -r to resume the session
    • Inside a screen session, you can press CtrlA, then D to detach
  • mosh
    • mosh is an SSH replacement that runs over UDP and is designed to be resistant to network disruptions

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2013-09-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。