# restart sshd in a ssh session? [RESOLVED]

## hongqn

after i upgraded openssh on my server, i was told to run 

```
/etc/init.d/sshd restart
```

.  but it couldn't be run:

```
$ sudo /etc/init.d/sshd restart

 * Stopping sshd ...                                                      [ !! ]
```

i think it is because i am running it in a ssh session.  then how could i restart sshd?  reboot isn't an option because some critical tasks are running on the server.

----------

## tarpman

I've never had trouble restarting sshd while logged in through ssh.  Try

```
/etc/init.d/sshd --verbose restart
```

and see if it tells you anything more interesting.  Also check /var/log/messages afterward.

----------

## hongqn

still no message    :Sad: 

```
$ sudo /etc/init.d/sshd --verbose restart

 * Stopping sshd ...                                                      [ !! ]

```

and there is no message in /var/log/everything/current (i am using metalog) after the line about my sudo command.

----------

## tarpman

Check the output of 

```
cat /var/run/sshd.pid
```

and

```
pidof sshd
```

/var/run/sshd.pid is the file the init script checks for the PID of the sshd service it started.  If the two numbers are different, then something else restarted the daemon and didn't let the init script know about it.  You may be able to fix it with

```
pidof sshd > /var/run/sshd.pid
```

but I wouldn't attempt this on a running production server - it would be better to wait until it is safe to reboot.  Not restarting the ssh service isn't likely to cause problems.  Do, however, make sure you can log in before you log out of your current connection!

----------

## hongqn

 *tarpman wrote:*   

> Check the output of 
> 
> ```
> cat /var/run/sshd.pid
> ```
> ...

 

```
$ cat /var/run/sshd.pid 

4383

$ pidof sshd

4325 4317 19351 19305 10996 10941 4383
```

the pid in /var/run/sshd.pid is also in `pidof sshd`, but as there are multiple pids in `pidof sshd`, i am not sure if the command 

```
pidof sshd > /var/run/sshd.pid
```

 is safe.  can a pid file hold multiple values?

i checked the pids other than 4383, they are all about my ssh logins:

```
$ ps aux | grep 4325

hongqn    4325  0.0  0.0   6788  1408 ?        S    13:15   0:00 sshd: hongqn@pts/0

hongqn    4964  0.0  0.0   2700   720 pts/2   R+   13:19   0:00 grep 4325
```

 *Quote:*   

> but I wouldn't attempt this on a running production server - it would be better to wait until it is safe to reboot.  Not restarting the ssh service isn't likely to cause problems.  Do, however, make sure you can log in before you log out of your current connection!

 

i ran 

```
glsa-check -f all
```

 today and noticed the openssh upgrade message, so i believe there must be a security hole in the running sshd.  so it is better to replace the sshd processes asap.  i have a backup server identical to the main server, so i can try restarting sshd on the backup server first, and if success, do the same command sequence on the main server then.

----------

## tarpman

 *hongqn wrote:*   

> 
> 
> ```
> $ cat /var/run/sshd.pid 
> 
> ...

 

Ah... I forgot about those.  Hm.  No, a pid file should only hold a single value - but you said the value in the file is one of the running sshd's.

Since you have a backup ( :Wink: ), try this:

```
kill `cat /var/run/sshd.pid`

/etc/init.d/sshd zap

/etc/init.d/sshd start
```

Killing the sshd server won't affect any already-running logins, since it spawns a separate process to deal with each of them - it just means you won't be able to make any new logins until you have the fresh daemon running.

Rebooting still seems to be the best option, if you can spare the server even for a couple of minutes.

----------

## hongqn

thank you, tarpman, i restarted sshd on both my backup and main server using your method  :Smile: 

although i'm still curious why restarting initscript fails, after all, i've resolved my problem  :Wink: 

----------

