Take the 2-minute tour ×
Server Fault is a question and answer site for professional 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
add comment

12 Answers

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
add comment

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 at 14:19
add comment

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
3  
That is both awesome and failriffic at the same time. –  MikeyB Feb 18 '11 at 22:18
add comment

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
add comment

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 at 23:17
add comment

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
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
add comment

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
add comment

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
add comment

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
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
add comment

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
add comment

try

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

^C there is control-v then c

share|improve this answer
add comment

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
add comment

Your Answer

 
discard

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.