Attach to server tmux session in a single command
The first step I always run when connecting to a server is to create a tmux session. The necessary steps to do this can be reduced thanks to the ssh config file. Here is how.
The config files is usually found in: ~/.ssh/config
Here is how it would look like if there was only one server:
Host *
UserKnownHostsFile ~/.ssh/known_hosts
Host staging-server
HostName <insert-ip-here>
User <username>
IdentityFile ~/.ssh/staging-server-id_ed25519
RequestTTY yes
RemoteCommand tmux attach-session -t dev-user || tmux new-session -s dev-user
UserKnownHostsFile points to the desired known hosts file, on my case this path is the default on my system.
The RequestTTY argument is sued to always request a text-only terminal, needed to then run the remote command:
tmux attach-session -t dev-user || tmux new-session -s dev-user
This command will either attach to a tmux session named dev-user or create and attach if no session with the given name exists.
Now with a simple ssh command I can connect to a tmux session on the staging-server:
ssh staging-server
The ssh config file documentation can be found here.