5

I'm using apache mina sshd to produce a ssh server so that users could be able to access a specific system remotely.

here is a part of my code:

    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(830);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
    sshd.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", "-i", "-l" }));
    sshd.setCommandFactory(new ScpCommandFactory());
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
        public boolean authenticate(String username, String password, ServerSession session) {
            return true;
            // just to make sure everything is right
            // this will implement later
        }
    });

    sshd.start();

when running this code on windows, this code looks ok, but on linux (Ubuntu 18.04), this error is gotten:

Exception in thread "main" java.net.SocketException: Permission denied
at java.base/sun.nio.ch.Net.bind0(Native Method)
at java.base/sun.nio.ch.Net.bind(Net.java:455)
at java.base/sun.nio.ch.Net.bind(Net.java:447)
at java.base/sun.nio.ch.AsynchronousServerSocketChannelImpl.bind(AsynchronousServerSocketChannelImpl.java:164)
at org.apache.sshd.common.io.nio2.Nio2Acceptor.bind(Nio2Acceptor.java:59)
at org.apache.sshd.SshServer.start(SshServer.java:318)

I know running the program using a port number above 1024 might fix this but the standard port of the kind of application I'm developing is 830. how can I fix this problem? does it need any configuration in my firewall or superuser settings?

Majid Roustaei
  • 1,556
  • 1
  • 20
  • 39
  • 11
    problem solved with this command: `sudo /sbin/sysctl -w net.ipv4.ip_unprivileged_port_start=0` – Majid Roustaei Jun 01 '20 at 15:09
  • Thank you so much! resolved my problem too. [Majid Roustaei](https://stackoverflow.com/users/7940437/majid-roustaei) – zohreh Feb 22 '21 at 08:42
  • @MajidRoustaei, can you explain this command? It indeed helped! – Ostap Gonchar Jan 25 '22 at 09:33
  • `ip_unprivileged_port_start` is a per-namespace `sysctl`. It defines the first ***unprivileged port*** in the network namespace. Privileged ports require **root** or **CAP_NET_BIND_SERVICE** in order to bind to them. To disable all privileged ports, set this to 0. source: https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt – Majid Roustaei Jan 25 '22 at 20:18

0 Answers0