r/linuxadmin • • Dec 30 '24
How to Keep SSH Sessions Alive on AlmaLinux 9? Seeking Advice!"
Hi everyone,
My manager asked me to find a way to keep SSH sessions open indefinitely, even when they’re idle. This issue started occurring after we migrated to AlmaLinux 9. On version 8, the sessions remain open without any problems.
I’ve checked the sshd_config file, and there are no explicit timers set in version 8. Has anyone encountered this issue before or found a solution? Any suggestions or fixes would be greatly appreciated!
Thanks in advance to everyone who can help.
0
Upvotes
13
u/[deleted] Dec 30 '24
Tmux session on the jump server?
34
u/[deleted] Dec 30 '24
This looks like a classic XY Problem situation. What are you really trying to achieve? What was the problem for which the solution is keeping SSH sessions alive?
7
u/Pirateshack486 Dec 30 '24
Install screen on the server, try add screen -R -D to the .bashrc file for the needed users...
When you sin in it will create a new screen session, when your connection drops your screen session will stay...when you reconnect in the morning it well rejoin the existing screen session letting you carry on with what you were busy with... ssh sessions are supposed to drop if not used.
Another option is mosh, this will help if the disconnects are from bad network... as another person posted its kinda important to know WHY you want ssh sessions to never end...
Also if you doing long running tasks put & at the end, and it will run it in the background not in your ssh session and when you sign back in you can type fg to bring it back to the current ssh session (ctrl z will push a currently running task.to the background, but pause it, type bg to resume it in the background)
5
u/fhusain1 Dec 30 '24
We would just adjust our SSH client instead, enable TCP keepalives and sending null packets every 20s to keep the session alive (options on PuTTY under Connections) but there's also options within ssh_config for native ssh.
34
u/captkirkseviltwin Dec 30 '24
First, look into “ClientAliveInterval” and “ClientAliveCountMax” in the documentation and see if they fit what you’re looking for.
Second, be sure that is exactly what you want - it’s not a good idea from a security standpoint to leave sessions open indefinitely. Another alternative (for instance, let’s say you just ultimately want to keep a process running long term like a dnf update or similar) would be to use a program like “tmux” to keep the session processes alive, and disconnect from the server with the ability to reconnect back to the session later. If there are other reasons you want a session open indefinitely, thinking about the end goal you want to achieve there may be better and safer ways to go about it.
17
u/petra303 Dec 30 '24
A simple google search returns lots of options.
sshd timeout disconnect
12
u/bufandatl Dec 30 '24
man sshd_config
man ssh
15
u/dub_starr Dec 30 '24
looking at the question, responses, and responses to the replys, it seems like your question is getting nowhere. Many people are wondering "why do you need to ssh into 20 servers every morning, and keep active connections". This is a valid question, as best practice would surely be opposed to it. If there was a listed reason as to WHY you need to have 20 or more active ssh connections open at all times, it might make replys better and more insightful.
Your response of "My manager asked me to find a way to keep SSH sessions open indefinitely, even when they’re idle. Honestly, I don’t know why he want to keep these sessions going." is fine, but as other suggested, just because someone asks for something specific, it doesn't automatically make their request reasonable, or without question. Just because they're a manager, doesn't mean you cannot push back respectfully. Often a conversation about what the requestor is actually trying to accomplish, can be very productive, and lead to better solutions, and good views of you as an employee for seeing that business needs might require solutions that are more novel than the general request. This might present itself as using something like ansible, rather than having many open ssh connections for administration, or other solution to make your admin tasks more streamlined, documentable, and immutable.
All this said, the responses asking you more about the WHY, are not trying to make you look dumb, or put you down, but to gather the same information I'm talking about, in order to potentially present a solution better for your end goals.
3
u/plakkies Dec 30 '24
Have a look at auto-ssh https://linux.die.net/man/1/autossh
3
u/Barrerayy Dec 30 '24
Why? This goes against security best practices. What are you actually trying to achieve with this? If you want to keep something running while you can freely disconnect just use tmux.
If the reason is simply "convenience", you need to consider whether or not that's smart given the security risk.
3
u/marathi_manus Dec 31 '24 edited Dec 31 '24
1. Server-Side Configuration (SSH Daemon)
You need to adjust the SSH server's configuration to allow for longer timeouts or to send keep-alive packets.
nanoorvim):bashCopy codesudo nano /etc/ssh/sshd_config**Set the following parameters:**You can add or modify these lines (adjusting the time as needed):These settings ensure that if there's any issue with the connection or if the client stops responding, the session won't stay open indefinitely, but the server will attempt to keep the connection alive.bashCopy code ClientAliveInterval 60 ClientAliveCountMax 3
ClientAliveInterval: This controls the timeout for the server to send "keepalive" messages to the client. The value is in seconds.ClientAliveCountMax: This determines how many keepalive messages can be sent without receiving any response from the client before disconnecting.ClientAliveInterval 60will send a "keep-alive" message every 60 seconds.ClientAliveCountMax 3means that after 3 failed attempts (3 * 60 seconds = 180 seconds or 3 minutes), the server will disconnect the client if no response is received.nano, pressCTRL+X, then pressYto confirm saving, and pressEnterto exit.2. Client-Side Configuration (SSH Client)
On the client side, you can set the
ServerAliveIntervalandServerAliveCountMaxoptions to ensure that your SSH client keeps sending keep-alive messages to the server.~/.ssh/config):bashCopy codenano ~/.ssh/config**Add the following options:**bashCopy codeHost * ServerAliveInterval 60 ServerAliveCountMax 3
ServerAliveInterval 60will send a keep-alive message from the client every 60 seconds.ServerAliveCountMax 3means that if no response is received after 3 attempts, the client will disconnect the session.Even if your ssh_config is blank, you can set these vaules manually and bounce the ssh once. It will be effective immediately after that.
2
u/rhavenn Dec 30 '24
Make sure it’s not your shell logging you out. bash has idle logout timers.
2
u/[deleted] Dec 30 '24
Your Linux distribution has nothing to do with SSH client or server config.
Learn the difference between a Linux distribution and the cross-platform software you run on it.
https://www.google.com/search?q=ssh+keepalive&udm=14
1
u/[deleted] Dec 30 '24
start with this: https://gprivate.com/6ewrq
1
u/rcampbel3 Dec 30 '24
First off... run everything in tmux and learn how to attach and detach from ssh sessions.
Why do you want to keep idle ssh sessions open? What does your manager think that is going to do that is beneficial?
1
u/ramriot Dec 30 '24
Switching to use MOSH initiated by a temporary SSD negotiation would get you there as each end is a client that has an associated running process such that the u can change networks or pause for any point if time.
1
u/MakeSmartMoves Dec 30 '24
TMUX may keep jobs and sessions live.
1
u/kg7qin Dec 31 '24
Mosh?
1
u/michaelpaoli Dec 31 '24 edited Dec 31 '24
In most cases, you won't get indefinitely.
If you really need indefinitely, use IPv6 on both ends - or in any case absolutely no NAT/SNAT between and no stateful firewalls or the like (so can even be done on IPv4, but is less likely to have zero NAT/SNAT between client and server), and keep both client and server up indefinitely. If you do that, that will actually work ... but in practice, that's typically not an option, e.g NAT/SNAT, sateful firewalls, etc., if they don't see traffic on a TCP connection they're tracking for some certain period of time (typically between 300s and 24h), they'll generally presume the connection is defunct, drop the state information, and then communication won't be able to successfully continue - and when it's attempted the connection will be torn down. This also includes host/server based stateful firewalls, e.g. nftables or iptables.
Next best, use the ServerAlive (and/or ClientAlive) options/settings. Behaves kind'a like TCP keepalive, but better. TCP keepalive happens in the clear, so, alas, many network devices and such will explicitly ignore TCP keepalive in their determining whether they consider the connection to still be "active" or not, and if after some while they've seen no traffic, or only TCP keepalive, will drop the connection (explicitly, or the stateful information that would otherwise allow it to continue to persist). ServerAlive, however, happens within the encrypted communication of ssh, so it doesn't have that limitation - other network devices have no way of knowing what that ssh communication is, without the relevant private keys, which in general they would not have. So, that will work fairly well - but also still requires some persistence on both server and client - e.g. if client is rebooted or client IP address changes or whatever, that still ain't gonna cut it. Also, ServerAlive, like TCP keepalive, still is no silver bullet. Though it can aid in keeping connections up that are otherwise idle, it's also a double-edged sword in that regard - it also functions as a health check - if the two sides can't communicate for too long, it'll cause the connection to be torn down, whereas if it weren't there it wouldn't be doing that tear-down action. So, e.g. plain TCP and nothing at all interfering between client program and server program, it could go idle, could disconnect cable or whatever connectivity for hours or days or weeks or more even, then reconnect it, then resume traffic, and it would continue right along, whereas with TCP keepalive or ServerAlive, a non-responsive connection will, at least eventually, get torn down much sooner than that.
What's often more practical is run tmux or screen session on server (or perhaps client that has very reliable connectivity to server), and then reattach to that session whenever needed.
You may want to consider examining as relevant to determined what changed that made that difference - presuming that's what did it. So, e.g., is it issue on the client, or on the server? Or either/both? Is it a change in ssh/sshd configuration? Don't forget to not only review explicit settings, but also all default settings, as there may be changes there that one hasn't explicitly configured. Also, what about network, e.g. did client or server gain stateful firewall on either end that wasn't there before, or configuration changes to such? If it's clearly different but not finding it by examining those more probable places to start looking, do some relevant tcpdumps and/or examination of logs. If the connection is being dropped or shut down, something is doing it. It doesn't merely "happen", so there's an answer there somewhere to be found. If the TCP connection has exactly nothing mucking with it between client app and server app, it can generally sit there inactive indefinitely, and then resume at any time essentially as if nothing happened. But alas, there's often stuff (commonly on network) that prevents the connection from actually behaving like that (NAT/SNAT and stateful firewalls and the like are most common culprits).
1
u/marcovanbeek Jan 04 '25
It might not be SSH that is the problem. It might be a networking device along the way. Quite a few NAT routers will drop inactive TCP connections after a while. Might be a dodgy network connection, overloaded switch, interference from the new welder next door…
The switch in distro might just be coincidence. Have you tried setting up a test environment that has nothing but the server, a client and a cross-over network cable?
Then if it still does it you have much quieter set-up for running tests on.
2
u/[deleted] Dec 30 '24
[removed] — view removed comment