Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have really bad problem. Here is the background:

I have a vps and I tried to use svn, but it fauls badly. I found in one site that the problem disappears if you reinstall ssh client and I did it. And while I was on the server the problem has been fixed.

The problem: it turns out that now I cannot login anymore on that server. It says ssh:

 connect to host port 22: Connection refused

I suspect that when I reinstalled the ssh client the sshd server was uninstalled and the result is - no login.

What I have is FTP as well as PHP and I know root password. Is there any way I can install the openssh-server agan?

I ve tried following:

echo "MYPASSWORD" | /usr/bin/sudo -u root -S apt-get install -q openssh-server

and if I execute it in my local machine terminal it's working and it try to install sshd, but when I try to run it with php with:

exec('echo "MYPASSWORD" | /usr/bin/sudo -u root -S apt-get install -q openssh-server', $lines);
echo $lines;

it return nothing. I've tried with ls -la but the result is the same - on terminal it's ok, but from PHP it doesn't work.

Could you give me any advice how to solve that problem. It look like I need to turn into a hacker :)

share|improve this question
The VPS provider isn't providing console access to the VPS? – andol Aug 10 '11 at 11:51
Lesson learned I hope: after mucking about with ssh/sshd/libraries/pam over an ssh session, make sure you can login via ssh before closing your root shell. – MikeyB Aug 10 '11 at 13:43
This sounds a bit like a you have FTP access to upload PHP scripts and you want to know how to use that to get shell access. That is a bit dodgy. Your VPS supplier should be able to help you out here. – dunxd Aug 10 '11 at 13:47
up vote 3 down vote accepted

If you have access as root over ftp you could do the following:

  1. Put package(s) required for sshd re-installation into some directory, e.g. /root/sshd_reloaded.
  2. Prepare a script, which installs that package and starts the sshd demon.
  3. FTP the packages to the proper location and the script to /etc/cron.hourly (or equivalent on your VPS system) and chmod it 0755 to make it executable. The general idea is to make cron pick it up and do the work for you. If you don't want to wait, you could probably put a file into /var/spool/cron/ to make cron execute a script which installs and starts sshd.
  4. Wait for the cron to pick up the new file and execute the job.
  5. Log into your VPS, change root password (it went in cleartext through FTP) and sin no more ;).

I'd recommend trying it first on the local machine -- it will make debugging easier. Only when you are sure all elements work, do it on the remote host.

The whole trick hinges on ability of being able to FTP remotely as root to a non-chrooted environment (which is not the way I'd recommend to configure FTP server).

share|improve this answer
That is downright cunning; you have my respect, sir! Though I agree about any ftp server allowing this being misconfigured to the point of perversity. – MadHatter Aug 10 '11 at 13:49
Thanks, I will try, but probably is better to get support from the hosting company. – Nik Aug 10 '11 at 15:55

I don't think you have much option but to try re-starting your VPS.

If that doesn't fix the problem then boot your system into recovery mode (if you can) and take a look at your logs and the sshd configuration file to see if they can help narrow down the problem.

Failing that you will have to get your vps provider to help.

share|improve this answer
Thanks for the idea, actually it's not a bad to try to restart the server. – Nik Aug 10 '11 at 15:58

Unless your Apache user has sudo permissions or you are running Apache as root, sorry, you cannot install ssh like that. Also reinstalling stuff on Linux rarely helps; if anything, you should need to restart the sshd process and if that does not help, see if there's something wrong with its configuration file.

One possible way MIGHT be to modify /etc/sudoers file at your localhost to contain sudo privileges for your Apache user, then upload that file to your server (if you can connect there as root), and try run your script again. But then again, I suspect this does not help even if you succeed in this, since problem might be in your configuration files.

You can also try to modify your script to be sudo -u root -S /etc/init.d/ssh restart after you grant the sudo privileges for Apache user.

Oh! One more thing. If you can connect there as root, you may also download the log files and see if they reveal why your sshd does not start or why it has fallen down.

share|improve this answer

There could be ways to log in to your server, but these are methods that are not discussed here I think ( containing php shells , connectback shell etc...).

I agree with the above answer that you would need some sort of remote console on your vps. A lot of providers support these methods ( rebooting into resque mode, giving a web kvm, ...).

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.