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
link|improve this question

67% accept rate
feedback

11 Answers

up vote 4 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.

link|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
feedback

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

link|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
feedback

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.

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

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.

link|improve this answer
feedback

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.

link|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
1  
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
feedback

You can also just delete the bashrc file:

ssh <hostname> rm ~/.bashrc
link|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
feedback

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.

link|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
feedback

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"
link|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
1  
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
feedback

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.

link|improve this answer
feedback

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

link|improve this answer
feedback

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?

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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