Run SSH SOCKS server as a daemon in CentOS

This post relies on the previous tutorial on how to quickly setup a SOCKS server.

Again, this is all one in the client side.

First you need to make sure you can autologin to the server acting as the SOCKS server. This is achieved by setting. Make sure you have your own public see so the server can know who you are.

[root@Client ~]# ls .ssh/|grep id
id_rsa
id_rsa.pub

If you don’t have any of these files just create the using the following command. Just press enter on every promtp.

[root@Client ~]# ssh-keygen -t rsa

Then add the autologin to your remote server using this command by changing the server ip to adjust to your scenario.

[root@Client ~]# cat ~/.ssh/id_rsa.pub | ssh user@yourserver ‘cat >> .ssh/authorized_keys’

Now test it by logging in. It should not prompt for any password and go straight to your server console.

[root@Client ~]# ssh user@yourserver
[user@yourserver ~]#

Now that you have the autologin setup, you can set your SOCKS server access by creating an upstart job.
Create a file under /etc/init/socks.conf and paste the following source. Remember to change your user and yourserver ip address to match your current scenario. In this case I’ll setup the SOCKS server in port 443.


# socks - SOCKS server access daemon
#
# This service setups a SOCKS server connection to a specified server
#

start on runlevel [0123456]

stop on runlevel [!$RUNLEVEL]

respwan
respawn limit 15 5
expect fork
exec ssh -v -N -f -D 443 user@yourserver >/tmp/log.log 2>&1

Then just run the upstart command to start your service.

[user@Client ~]# start socks

To make sure the proxy service is running you can use netstat.

[user@Client ~]# start socks
socks start/running, process 1151
[root@socksserver ~]# netstat -tlnp|grep 1151
tcp 0 0 127.0.0.1:443 0.0.0.0:* LISTEN 1151/ssh
tcp 0 0 ::1:443 :::* LISTEN 1151/ssh

And that is all. Whenever your server reboots the service will start automaticly. If the SSH daemon goes down, upstart will restart it,

Leave a comment