How can I regain SSH access after creating a .bashrc loop?
Asked by
9 tabs remaining
I accidentally made a recursive loop in my `.bashrc` on a Debian server by calling `.bashrc` within itself. Now, every time I try to SSH into the server, the connection drops immediately without any error message. I don't have any alternative user accounts or console access; I can only connect via SSH. I've tried different commands like `ssh root@server "command"`, and even other methods such as `scp` and `rsync`, but they all fail because the interactive shell runs the faulty `.bashrc` and disconnects. Is there any way I can bypass the `.bashrc` or recover the server without physical access?
Answers
Post your output from the connection attempt; it might shed light on why none of the commands are working. Sometimes, it's just about looking at the details closely.
Reply to this answer
You could run an explicit command to the SSH daemon to skip loading the rc files by using `bash --norc --noprofile`. This instructs bash not to read the `.bashrc`. Try it out and see if it lets you connect!
Reply to this answer
Have you tried connecting using a different shell, like dash? You can run `ssh -t /bin/dash` instead of bash. This might allow you to access the server without triggering the recursive `.bashrc` issue. Just remember, that shouldn't have affected commands like `scp`, which runs in non-interactive mode, so it's odd that's failing too. After you sort this out, consider disabling root SSH access in `sshd_config` to improve security. It's usually a bad idea to log in as root anyway, so setting up a non-root account with sudo privileges would be safer.
cedar shingles: Yeah, I thought `scp` wouldn't trigger that either, but something may have gone wrong. Just make sure to check the logs for any clues.
hex_0A2F: I agree with that! Also, be sure to create a 'break glass' admin account for emergencies next time.
Reply to this answer
I managed to replicate your issue. However, I found that using `ssh -t [email protected] /bin/bash` works for me. When it prompts for a password and hangs, pressing Ctrl+C seems to help get the login working afterward. This might be worth a shot!
Reply to this answer
Add your answer
Answers are reviewed before publication.
turning_radius: I attempted that, but I still encountered "Connection closed". It seems like a tricky situation.