70

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
  • 3
    Your scp command will not work because scp will also read .bashrc when connecting. To avoid the problem In the future, you may add something like [ -z "$PS1" ] && return at the beginning of ./bashrc. This way scp will stop parsing .bashrc after the first line, and you will be able to overwrite it in case of emergency. – dalloliogm Aug 26 '16 at 9:27

17 Answers 17

19

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.

  • 4
    "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
  • 1
    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
107

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

  • This works well - simple and provides a shell you can use to fix the problem. – MT. Oct 2 '11 at 16:55
  • 1
    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
  • 1
    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 '15 at 3:00
  • 2
    If your default shell is csh/tcsh it will always source your .cshrc/.tcshrc even for non-interactive shells. – Brian Vandenberg Jul 17 '15 at 21:37
  • I'm sitting here laughing my ass off at my own stupidity to have gotten to this point. Your solution worked great. Thank you so much for helping this tinkerer get some work done – ijustlovemath Jan 28 '17 at 4:54
31

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.

  • 9
    I was skeptic, but this actually works... – rmobis May 19 '16 at 19:42
  • This is genius! – user1747134 Nov 11 '16 at 11:14
  • Actually works...thanks! The other methods never worked for me. – Zzzach... Oct 30 '17 at 0:25
  • on mac with only a .bash_profile, this worked for me :) – AnneTheAgile Jan 13 '18 at 14:51
  • OMG, I thought I have searched the whole internet, then I decided to try this, and this is the only way that works for me! Thanks! – Jiahao Cai Feb 3 '18 at 22:03
21

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.

  • 11
    That is both awesome and failriffic at the same time. – MikeyB Feb 18 '11 at 22:18
  • 1
    @TomRitter : Please provide that ᴄᴠᴇ reference. – user2284570 Oct 16 '15 at 13:24
  • 1
    It was in an oooold version of cacti, so unless you're running 8-year old software I don't think it will help you. And if you are running 8 year old software, I can't help you. ;) – Tom Ritter Oct 16 '15 at 20:32
14

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

ssh -t user@host bash --norc --noprofile
  • 3
    This works, but note that you won't have your usual prompt, just an empty screen. try doing an ls to convince yourself that you're in :). Also note this will use a dumb term, so for me nano doesn't work – Ciprian Tomoiagă Sep 4 '18 at 8:22
6

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.

  • 3
    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 '15 at 21:15
  • @Silvio, OP's answer is that he used an exploit not related to SSH. – Tobu Mar 27 '15 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 '15 at 3:02
5

None of the above answers can bypass the login shell of ssh. You can pass a full command line, and so it runs the remote shell to process the command and set the working environment for the command. That's what shells are for and it's the Unix way. You would have all sorts of compatibility problems if you tried to run something without a shell. Likewise, trying a control-C should do the same as calling exit which is the behavior you are trying to avoid. If bash continues its a bug. Why people keep saying that the man page says something different need to quote it because it says nothing of the sort in my man page.

Additionally, on most linux systems, specifying /bin/sh does NOTHING since this is just a symlink to bash!

Wanna test? Add "echo" statements to you .bashrc and .profile and see which is run. I did. Here's the results.

ssh user@host will execute .bash_profile ssh user@host /bin/bash will execute .bashrc, but it thinks its non-interactive (no prompt). ssh -t user@host /bin/bash executes .bashrc twice ... once at login, once for the passed command, so specifying ANY shell will always run the first. ssh -T user@host is the same as not specifying -T or -t at all.

Now, if you notice, MY system isn't running both files, only one or the other. But the original poster has a line in .bash_profile running .bashrc, so .bashrc will always get run no matter what. Shouldn't have put that line there! If that line didn't exist, you wouldn't have had a problem.

You'll need to find another way in or find an admin. This is what admins are for.

  • Some good points; no executable passed as a command can help, as you explain, but on a somewhat moot point: if /bin/sh did get to execute first, it would help, because Bash doesn't load ~/.bashrc when invoked as sh. This answer is a superset of your other answer, so please delete the other one. – mklement Mar 29 '17 at 21:23
3

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.

  • 1
    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
2
ssh -t user@host "bash --norc --noprofile -c '/bin/rm .bashrc'"
  • This worked for me in a very restricted environment when nothing would. I had to specify the full path to .bashrc. – zbeekman May 23 '18 at 15:28
1

try

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

^C there is control-v then c

  • It's ctrl-v and ctrl-c on my system. Thanks anyway! :) – mzuther Feb 14 '16 at 13:24
0

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.

  • 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
  • what about sftp, afaik its a ssh subsystem. – allo Mar 16 '17 at 23:42
0

You can also just delete the bashrc file:

ssh <hostname> rm ~/.bashrc
  • 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
0

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"
  • 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
0

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

0

Kudos to user60069, it worked for me, but I use the shell-specific startup file .bashrc, so logging in with /bin/sh worked for me.

However, if you are in the "no such luck" situation, I offer this solution, based on user60069's and Dennis W's solutions:

ssh -t you@host  /bin/bash --noprofile  --norc

Dennis W offered the --norc option, which someone said did not work for them.

Run "man bash" or "man (your shell)" for options to disable the start up files. You only need to use an abhorrent shell for the time it takes to fix the problem.

0

Other way to login to server is without profile, please find below command
ssh -t user@host bash --noprofile

For AWS using pem file and no other user
ssh -ti "YOUR-PEM-FILE-NAME.pem" ec2-user@YOUR-IP-ADDRESS bash --noprofile

Hope this helps!

-1

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?

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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