There should be no need to install sudo. If you want to run the occ command as an elevated user, simply do the following to run the command as the www-data user.

Example using su and www-data user

su -c "php /var/www/html/occ maintenance:mode --off" -s /bin/sh www-data

Should I be using the www-data user?

To confirm that the www-data user is the owner of the configuration you can do the look at the file properties of config.php

ls -al /var/www/html/config/config.php

Which, for me, results in

-rw-r----- 1 www-data root 1501 Jun 28 14:13 /var/www/html/config/config.php


Finally, if you’re running this in a Docker container, as I am, you can either run an interactive terminal, then issue commands, or do a one-liner from the host.

Docker Interactive

# Start an interactive terminal
sudo docker exec -it docker-nextcloud /bin/bash
# Run your command from within the docker container
su -c "php /var/www/html/occ maintenance:mode --off" -s /bin/sh www-data
# Exit the interactive session

Docker from host

# one-liner from host (may need to add sudo if your logged in user is not part of the docker group)
docker exec -it my-nextcloud-container su -c "php /var/www/html/occ maintenance:mode --off" -s /bin/sh www-data
# Alternative syntax
docker exec -u www-data my-nextcloud-container php occ maintenance:mode --off