spring-bootmavenubuntuwindows-subsystem-for-linuxsudo

Unable to run 'sudo mvn' in WSL


When trying to run sudo with mvn e.g. 'sudo mvn spring-boot:run', in WSL (Ubuntu 24), I get the following error: 'sudo: mvn: command not found'. Obviously, maven is installed and runs perfectly without sudo. Also, running sudo with other commands works fine as well e.g. sudo apt-get update etc.

Just to preclude any questions why I am trying to run 'sudo mvn spring-boot:run' is that when I ran it without sudo, my SpringBoot app crashes with 'java.net.BindException: Permission denied', probably on port 80, although the error stacktrace doesn't say so (in application.preperties, server.port=80, and the port is not in use i.e. sudo lsof -i:80 shows nothing).

The aforementioned Permission denied error happens on both my WSL and my AWS EC2 instance (which also uses Ubuntu 24), but the way I overcome it on EC2 is by using sudo, but I can't do the same on WSL.

Would be grateful for any suggestions.


Solution

  • If you have in /etc/sudoers :

    Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
    

    Try to add maven directory :

    Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/mnt/c/system/apache-maven-3.9.6/bin"