1.8. Creating a custom unit file by using the second instance of the sshd service
If you need to configure and run multiple instances of a service, you can create copies of the original service configuration files and modify certain parameters to avoid conflicts with the primary instance of the service.
Procedure
To create a second instance of the sshd service:
Create a copy of the
sshd_configfile that the second daemon will use:# cp /etc/ssh/sshd{,-second}_configEdit the
sshd-second_configfile created in the previous step to assign a different port number and PID file to the second daemon:Port 22220 PidFile /var/run/sshd-second.pidSee the
sshd_config(5) manual page for more information aboutPortandPidFileoptions. Make sure the port you choose is not in use by any other service. The PID file does not have to exist before running the service, it is generated automatically on service start.Create a copy of the
systemdunit file for thesshdservice:# cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/sshd-second.serviceAlter the created
sshd-second.service:Modify the
Descriptionoption:Description=OpenSSH server second instance daemonAdd
sshd.serviceto services specified in theAfteroption, so that the second instance starts only after the first one has already started:After=syslog.target network.target auditd.service sshd.service-
Remove the
ExecStartPre=/usr/sbin/sshd-keygenline, the first instance ofsshdincludes key generation. Add the
-f /etc/ssh/sshd-second_configparameter to thesshdcommand, so that the alternative configuration file is used:ExecStart=/usr/sbin/sshd -D -f /etc/ssh/sshd-second_config $OPTIONSAfter the modifications, the
sshd-second.serviceunit file contains the following settings:[Unit] Description=OpenSSH server second instance daemon After=syslog.target network.target auditd.service sshd.service [Service] EnvironmentFile=/etc/sysconfig/sshd ExecStart=/usr/sbin/sshd -D -f /etc/ssh/sshd-second_config $OPTIONS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
If using SELinux, add the port for the second instance of
sshdto SSH ports, otherwise the second instance ofsshdwill be rejected to bind to the port:# semanage port -a -t ssh_port_t -p tcp 22220Enable
sshd-second.serviceto start automatically on boot:# systemctl enable sshd-second.service-
Verify if the
sshd-second.serviceis running by using thesystemctl statuscommand. Verify if the port is enabled correctly by connecting to the service:
$ ssh -p 22220 user@serverMake sure you configure the firewall to allow connections to the second instance of
sshd.