LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Added 'exit 0' to the bottom of .bashrc, now what? (https://www.linuxquestions.org/questions/linux-newbie-8/added-exit-0-to-the-bottom-of-bashrc-now-what-4175536213/)

szboardstretcher 03-09-2015 02:39 PM

Added 'exit 0' to the bottom of .bashrc, now what?
 
Centos 6.5

I ran into an interesting problem (on reddit) that I figured I could solve, but I have not been able to. Its simple,.. I added 'exit 0' to /root/.bashrc, and now I am trying to log in via ssh.

Everytime I do, it immediately exits when it runs the .bash_profile, which sources .bashrc, (which is immediate upon 'logging in')

I've tried:

Code:

ssh root@192.168.1.50 -t vim
vim scp://192.168.1.50/.bashrc
vim scp://192.168.1.50/root/.bashrc
ssh root@192.168.1.50 bash --norc
ssh root@192.168.1.50 /bin/bash --norc --noprofile
ssh -T root@192.168.1.50 "mv /root/.bashrc /root/.bashRC"
scp .bashrc root@192.168.1.50:/root/
ssh root@192.168.1.50 /bin/bash --norc --noprofile -vvvvvvvvvv
ssh -vvvvvv root@192.168.1.50 /bin/bash --norc --noprofile
ssh -vvvvvv root@192.168.1.50 /bin/mv /root/.bashrc /root/.bashRC
ssh -t -t root@192.168.1.50 << EOF
mv /root/.bashrc /root/.bashRC
EOF
ssh -t -t root@192.168.1.50 --norc << EOF
echo HELLO > /root/.bashrc
EOF
ssh -tv root@192.168.1.50 rm .bashrc

So,.. I am unable to get back into the system (as root, no other users exist) after adding 'exit 0' to .bashrc

Anyone feel like explaining why all of these failed (aside from saying SSH interactive logins run the .bash_profile/.bashrc files) or, offering a suggestion that works? Seems like if you have the root password, you should be allowed to modify the login process... since... you know... you are root.

AlucardZero 03-09-2015 03:01 PM

If all of those failed to let you edit .bashrc or remove it, then you have a different problem than just an exit.
Also, hi from Reddit (though that guy says he fixed it).

szboardstretcher 03-09-2015 03:05 PM

Correct. Tried all the solutions, none work. I have access to the machine, it works fine until I put exit 0 at the bottom of .bashrc

After that, no combination of --norc --noprofile, scp, sftp, /bin/bash, commands etc etc have any affect at all. Its literally a base install of centos built for this testing only. Everything is default except the 'exit 0' at the bottom of .bashrc

AlucardZero 03-09-2015 03:26 PM

Boot the machine from live CD at the console and edit /root/.bashrc from there.

jpollard 03-09-2015 03:45 PM

Ummm... don't do that.

bashrc is run in the same context as the users login shell - thus an exit logs the user out.

And in the list of things of "don't put exit in":
.login
.profile
.bashrc
.cshrc
.xinit
.xsession

Exits can be used in any script that is not run as part of the users login shell - all it does then is terminate the process. It can be used in other places - but you have to expect to be logged out.

szboardstretcher 03-10-2015 09:58 AM

Quote:

Originally Posted by AlucardZero (Post 5329456)
Boot the machine from live CD at the console and edit /root/.bashrc from there.

No. I believe you are missing the point. If you put 'exit 0' in your .bashrc,.. is it ever possible to 'remotely' access the machine again?

Its a virtual machine built for testing this. Of course I can boot into single user mode and edit it, but that isnt the point.

szboardstretcher 03-10-2015 10:00 AM

Quote:

Originally Posted by jpollard (Post 5329465)
Ummm... don't do that.

bashrc is run in the same context as the users login shell - thus an exit logs the user out.

And in the list of things of "don't put exit in":
.login
.profile
.bashrc
.cshrc
.xinit
.xsession

Exits can be used in any script that is not run as part of the users login shell - all it does then is terminate the process. It can be used in other places - but you have to expect to be logged out.

Agreed. But,. if you were to accidentally do that,. how could you bypass the .bashrc to remotely log in? Some people and distros are reporting that the --norc option works. Why does it work on their distro but not Centos 6.5 default install? What is different? That is the real discussion here, not whether its possible, but why Centos 6.5 root user is unable to diable bashrc remotely when logging in...

grail 03-10-2015 10:13 AM

Well I am no ssh guru, but I found one idea and had a left of centre one:

Found:
Code:

~/.ssh/rc
              Commands in this file are executed by ssh when the user logs in, just before the user's shell (or command) is started.

Idea: By default the nobody user is often part of the core system. Could you login as that user??

suicidaleggroll 03-10-2015 10:17 AM

What if you were to use sshfs to remotely mount root's home directory on another machine and edit it there?

jpollard 03-10-2015 10:25 AM

Quote:

Originally Posted by szboardstretcher (Post 5329886)
Agreed. But,. if you were to accidentally do that,. how could you bypass the .bashrc to remotely log in? Some people and distros are reporting that the --norc option works. Why does it work on their distro but not Centos 6.5 default install? What is different? That is the real discussion here, not whether its possible, but why Centos 6.5 root user is unable to diable bashrc remotely when logging in...

you could try with the kernel parameter "init=/bin/bash --noprofile". But I admit to being unfamiliar with passing parameters to init. I think it has to be the last entry of the boot command.

TenTenths 03-10-2015 10:55 AM

Brain isn't working well today but how about using rsync or scp to copy over an "empty" .bashrc file?

jpollard 03-10-2015 11:07 AM

Quote:

Originally Posted by TenTenths (Post 5329908)
Brain isn't working well today but how about using rsync or scp to copy over an "empty" .bashrc file?

I believe sshd runs the bashrc scripts to setup the shell used to finish running the local scp (which runs very much like "cat > file"... Now an external rsync server might not.

Anything that terminate the shell will also terminate the scp.

TenTenths 03-10-2015 11:09 AM

Quote:

Originally Posted by jpollard (Post 5329911)
I believe sshd runs the bashrc scripts to setup the shell used to finish running the local scp (which runs very much like "cat > file"... Now an external rsync server might not.

Anything that terminate the shell will also terminate the scp.

Makes sense, like I said, bad brain day! Although I'm now fascinated and may muck about with a VM tomorrow to try it! :)

sgrlscz 03-10-2015 05:18 PM

Quote:

Originally Posted by szboardstretcher (Post 5329886)
Agreed. But,. if you were to accidentally do that,. how could you bypass the .bashrc to remotely log in? Some people and distros are reporting that the --norc option works. Why does it work on their distro but not Centos 6.5 default install? What is different? That is the real discussion here, not whether its possible, but why Centos 6.5 root user is unable to diable bashrc remotely when logging in...

They fail because the sshd process is invoking the root's shell to call the command, i.e. sshd isn't running:

Code:

/bin/bash --norc --noprofile
It's actually running:

Code:

bash -c /bin/bash --norc --noprofile
Since it's invoking the shell in order to run the command, it's exiting before it even gets to the command being passed, so it never runs the '/bin/bash --norc --noprofile'.

I haven't looked at the sshd code to see if maybe there are different options to give different behaviors or if there is something in the default configs, which might explain the distribution differences.

With the way Centos and RHEL are working, I don't see a way to get it to work because I can't see a way to either stop the processing of the .bashrc or override the shell.

jpollard 03-10-2015 05:39 PM

That was why I suggested using the kernel parameter "init=/bin/bash --noprofile". This should bypass the system startup and allow you to use the initrd to do things (like mount root and fix things).


All times are GMT -5. The time now is 04:00 PM.