If you've come across the error "sorry, you must have a tty to run sudo" in the logs when trying to run a command from a script using sudo, know this: by default sudo wants to be run only by logged-in users (i.e. users who have an open TTY terminal) - for security reasons.
This means sudo won't want to work from a script. But we can get around this.
Open the sudoers file for editing and find this line:
Default requiretty
and comment it out:
#Default requiretty
Location of the sudoers file depending on the OS:
- FreeBSD: /usr/local/etc/sudoers
- Debian/ubuntu: /etc/sudoers
- CentOS/RedHat: /etc/sudoers
Comments