Elevate to root using the sudo command.
sudo <somecommand>
sudo suThe above is sometimes frowned upon if you want every elevated command logged for strict auditing.
Configure which users and groups can sudo and to which commands by editing the /etc/sudoers file
(or sometimes /etc/sudoers.d/... include files).
Use the visudo command because it validates the changes before it allows saving them.
This command drop you into your $EDITOR if it's set (see IntelliJ page), or if not set then it'll open
/etc/sudoers in the classic vi editor.
sudo visudoIf you want to add to another file under /etc/sudoers.d/ then:
sudo visudo -f /etc/sudoers.d/hariReplace hari with your username.
The line you need to add is:
hari ALL = (ALL) ALL
Ensure if you're create a new file /etc/sudoers.d/hari that you set correct permissions:
sudo chmod 440 /etc/sudoers.d/hariTo allow your user to use sudo without having to enter their password every 5 minutes, set the line to:
hari ALL = (ALL) NOPASSWD: ALL
Ensure the above line comes after the following line found by default on macOS:
%admin ALL = (ALL) ALL
as this %admin line requires a password for all members of the admin group which you will be in on your macOS.
Test sudo permission by first invalidating your sudo cached credential:
sudo -kand then retrying a basic command which should return successfully without a password prompt:
sudo -n echo success