Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

So let's say one typoed something in their .bashrc that prevents him (or her) from logging in via ssh (i.e. the ssh login exits because of the error in the file). Is there any way that person could login without executing it (or .bashrc since the one runs the other), or otherwise delete/rename/invalidate the file?

Suppose you don't have physical access to the machine, and this is the only user account with the ability to ssh in.

For Reference: .bash_profile includes .bashrc:

[[ -f ~/.bashrc ]] && . ~/.bashrc

Edit: Things I have tried:

ssh user@host "rm ~/.bashrc"

scp nothing user@host:/RAID/home/tom/.bashrc

ssh user@host "/bin/bash --norc"

All give the error:

/RAID/home/tom/.bashrc: line 16: /usr/local/bin/file: No such file or directory
/RAID/home/tom/.bashrc: line 16: exec: /usr/local/bin/file: cannot execute: No such file or directory
share|improve this question

13 Answers 13

up vote 9 down vote accepted

I think your only options are:

  • ssh in as another user and su to your account;

  • use something like ftp or smbclient, if the relevant services are enabled on the host;

  • find an open vulnerability in an open network service and exploit it :).

  • get an admin to fix the problem.

share|improve this answer
"Suppose you don't have physical access to the machine, and this is the only user account with the ability to ssh in." –  Dennis Williamson Dec 15 '09 at 16:05
I am going down this route. I will post the solution after I find it. Fortunetly, I have a few avenues of attack. –  Tom Ritter Dec 15 '09 at 21:07
used Filezilla to SFTP into my server and it fixed me. Thanks soooo much, you saved me. I had a "exit 1" condition in my .bash_profile by accident and it hosed me. –  djangofan Oct 19 '12 at 23:46

ssh -t username@hostname /bin/sh works for me.

share|improve this answer
This works well - simple and provides a shell you can use to fix the problem. –  MT. Oct 2 '11 at 16:55
You just saved my life. Thanks. –  Leo Nov 18 '13 at 22:05
Trying to do this with the Secure Shell app in Google Chrome (chrome.google.com/webstore/detail/secure-shell/…) but can't figure out how/where to put the command line arguments. Any tips? –  Benj Feb 19 '14 at 14:19
This doesn't work for me (though it should according to the man page). The local host runs ubuntu 14.04.1 and it's ubuntu 12.04.5 on the remote host. I'm unable to rsync because my login shell is tcsh and on the remote host it prints garbage (tcsh: No such file or directory tcsh: Trying to start from "/u/levy" -- which I'm told it's because the directory is NSF mounted). I thought I'd solve the problem bypassing the login shell but that doesn't work. –  Silvio Levy Mar 28 at 3:00

I've had the same problem, and somehow was able to solve it. I used ssh to access the system, and pressed and held Ctrl+c as soon as I logged into the system. Then, ~/.bashrc was not read, and I was able to modify it.

share|improve this answer

I used a published CVE to execute a command as root through a web interface in a network monitoring software I had installed. "rm /RAID/home/tom/.bashrc"

Then I could login and svn revert the changes I made.

share|improve this answer
That is both awesome and failriffic at the same time. –  MikeyB Feb 18 '11 at 22:18

You're out of luck.

All ssh commands run your login shell. ssh $COMMAND runs $SHELL -c $COMMAND, scp runs $SHELL -c /path/to/sftp-server, plain ssh just runs your shell.

share|improve this answer
This is also true for sftp - even SSH subsystems are apparently executed from within a shell. –  lxgr Jun 9 '14 at 23:17
In my experience you are correct, even though the ssh man page says "If command is specified, it is executed on the remote host instead of a login shell." Yet the poster was able to solve the problem using the highest-rated answer (ssh hostname /bin/sh). What gives? –  Silvio Levy Mar 26 at 21:15
@Silvio, OP's answer is that he used an exploit not related to SSH. –  Tobu Mar 27 at 23:48
Right. Someone reported success with the highest-rated answer and I hastily assumed it was the OP. I wish it did work -- I have a similar situation, though it's merely annoying not crippling. (See my comment under the highest-rated answer above.) –  Silvio Levy Mar 28 at 3:02

You need to a) start bash without source'ing either bashrc or bash_profile and b) since such a bash wouldn't be a full login shell / have no tty attached, force ssh to attach a tty:

ssh -t user@host bash --norc --noprofile
share|improve this answer

You can try to overwrite the .bash_profile with an empty file using the scp command. From what I have googled, scp use a non-interactive login that does not read .bash_profile.

share|improve this answer
Unless I'm (hypothetically) remembering the directory structure wrong, and it's not giving me an error on that, this does not work - scp also runs the file(s). –  Tom Ritter Dec 15 '09 at 12:30
The thing that people are not realizing is that scp isn't anything special; it's just another command run over ssh -- which means just about everything that happens for an interactive ssh session happpens with scp, also. –  larsks Dec 15 '09 at 14:24
Thanks for the precision. –  Laurent Etiemble Dec 15 '09 at 15:45

You can also just delete the bashrc file:

ssh <hostname> rm ~/.bashrc
share|improve this answer
The problem is in the .bash_profile file, not in the bashrc. –  Laurent Etiemble Dec 15 '09 at 12:31
This doesn't work - it tries for a shell first and fails. –  Tom Ritter Dec 15 '09 at 12:32

Something like:

ssh host "/bin/bash --norc"

which seems to work, but note that PS1 is not set so you'll be typing commands without a prompt.

This has the advantage of being non-destructive.

share|improve this answer
This doesn't work, it tried for a successful login first, but can't login, and therefore can't run bash --norc –  Tom Ritter Dec 15 '09 at 12:27

If you system is setup normally, .bash_profile won't be run for a non-interactive shell (such as running a command).

Since you state the problem is in the .bash_profile file, try moving it out of the way:

ssh user@host "mv ~/.bash_profile ~/.bash_profile_broken"
share|improve this answer
I didn't think which way the files were linked mattered - .bash_profile includes .bashrc =( I added more info. –  Tom Ritter Dec 15 '09 at 12:37
But what Lockie is suggesting -- which is confirmed by the bash man page -- is that a non-interactive shell will not run either .bashrc or .bash_profile. However, even when you pass commands on the ssh command line (like in this example), bash is still started as an "interactive" shell, which means it will read your .bashrc file. So a good idea, but unfortunately ssh won't cooperate. –  larsks Dec 15 '09 at 14:32

UH='user@host'; ssh $UH 'mv ~/.bashrc ~/letmein'; ssh $UH

Please don't cut and run, change user and host, then edit letmein and save as .bashrc

share|improve this answer


echo ^C | ssh <hostname> ' rm .bashrc'

^C there is control-v then c

share|improve this answer

From the suggestions and responses given above, I'd say it's not the .bashrc or .bash_profile files. Also ssh manpage says that if you specify a command to be executed then your profile files won't be read.

I'd suggest try executing a different login shell (ksh? csh? sh?) from the absolute path; also, beware that it might be a totally different problem (quota? execute and read permission on your home directory?), so a side approach would be better. Can you ask another user to do a ls -la $YOUR_HOME_DIR and mail you the result?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.