Configuring Listening Interfaces

When we created our first SshServer we passed a port number through in the constructor.

SshServer server = new SshServer(2222); 

This will start up a listening interface on the port you provide on all interfaces. But you may want to control this more tightly. There is an option using the default constructor of SshServer to create it without defining any interfaces. 

If you use the default constructor or want to add additional interfaces at any time after we have created our SshServer we can use:

server.addInterface("", 10022);

Or with an InetAddress directly

server.addInterface(InetAddress.getLoopbackAddress(), 10022);

Every connection on the SshServer must have a SshServerContext object associated with it. SshServerContext defines the configuration of the connection such as the host keys, authenticators and its behaviour and permissions through configuration policy objects like ForwardingPolicy and FileFactory. The SshServer class manages this for you, creating a context based on the configuration options available on SshServer, like addHostKey, addAuthenticator, etc. 

For more advanced users, you can provide a factory object to create a configuration context for each incoming connection yourself, overriding the managed contexts provided by SshServer. In order to do this, you will be required to set an instance of ProtocolContextFactory<SshServerContext> on an interface. 

server.addInterface(InetAddress.getLoopbackAddress(), 10022, new ProtocolContextFactory<SshServerContext>() {
    public SshServerContext createContext(SshEngineContext engineContext, SocketChannel socket) {
          SshServerContext sshContext = new SshServerContext(engineContext.getEngine());
          // Configure context adding host keys, authenticators, file factory, channel factory etc.
          return sshContext;

You can add as many interfaces as you need. You can also mix a combination of SshServer managed contexts or your own context factories.