Multiple ports for OpenSSH not working


I am trying to open a second port for my openSSH.
For this I followed

Doing this and restarting sshd via service sshd reload/restart however results in

sh: 222: unknown operand

where 222 seems to be the port. Any idea about what could be going on?

Can you post here the sshd_config?

#	$OpenBSD: sshd_config,v 1.102 2018/02/16 02:32:40 djm Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

Port 22
Port 222
#AddressFamily any
#ListenAddress ::

HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile	.ssh/authorized_keys

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
#UsePAM no

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# enable DSCP QoS values (per RFC-4594)
#IPQoS AF21 AF11

# override default of no subsystems
Subsystem	sftp	/usr/lib/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#	X11Forwarding no
#	AllowTcpForwarding no
#	PermitTTY no
#	ForceCommand cvs server

Did you edit the file though SSH in the Vi or some other editor, OR did you use scp to copy the edited file from/to the router?

I edited it via nano through SSH. Does that make a difference?

Yes, special characters are not the same among all OSes.
Then it could be a limitation of the OpenSSH implementation for Openwrt.
Dropbear works fine with multiple ports, I have SSH listening on multiple ports on my routers.

Copied it to my laptop and rewrote it there, turned out the same. Might be a bug then?
Background is that I am splitting my SSH port with an iptables output chain away from my VPN - I can now reach my router from the outside while still in VPN but I need to connect to my public IP for this now.
I'd like to have a second port open so that I can have one for 'intranet' and one for 'internet'. I know that dropbear can do this and that is currently the fallback but yeah.. would feel like a cleaner solution. And curiosity drives the case of course.

sed -i -e "
\$a ListenAddress
\$a ListenAddress
" /etc/ssh/sshd_config
service sshd restart
1 Like

What does that do?
Whatever it was supposed to do, it didn't work :slight_smile:

EDIT: I mean I understand the sed command but what does the ListenAddress part do?

Adds the ports 222 and 2222 on all IPv4 interfaces.
You should have something like this in the config:

#Port 22
#Port 222
#AddressFamily any
#ListenAddress ::

No, that didn't work.
I still can only connect over port 22 with the setting that @trendy proposed.

With Dropbear you mean?
If you utterly need OpenSSH on one port, let OpenSSH listen to that port and for the others use Dropbear. I know it is not as neat as you'd like, but there seems to be some limitation here.

Yes, that will then be it for now... Only worried about SFTP though. But for SSH, I'll have the 'double setup' for now. Maybe it clears up in the future.

With Dropbear you'll have to use scp.

1 Like

Try again:

# opkg update
# opkg install openssh-server
# service dropbear disable
# service dropbear stop
# cat << EOF >> /etc/ssh/sshd_config
# service sshd restart
# netstat -l -n -p | grep -e sshd
tcp        0      0    *               LISTEN      28524/sshd
tcp        0      0   *               LISTEN      28524/sshd
1 Like

So the problem is at another point - I posted an issue in the OWRT Github repo and the init script for openssh is 'insufficient' for this purpose. I'm honestly not too sure whether this script is really good at all as it manually takes the port from sshd_config and opens a zero-config port:

I'm not sure why that's actually necessary. I put the ports manually into the config file as mentioned above and then restarted sshdwithout the init-script - no problem, both ports are accessible.

Why not just have something in the line of

start_service() {
	for type in rsa ecdsa ed25519
		# check for keys
		[ ! -f $key ] && {
			# generate missing keys
			[ -x /usr/bin/ssh-keygen ] && {
				/usr/bin/ssh-keygen -N '' -t $type -f $key 2>&- >&-

	procd_set_param command $PROG -f /etc/ssh/sshd_config

so simply launching sshd with the config file explicitly passed? That seemed to matter as a normal start wouldn't use my config file.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.