'macOS terminal asking for password every time I run copy command

I'm running a bash command on mac that moves a file to private/etc/app_name/.

sudo cp my_file.cpp private/etc/app_name/

Every time the I want to run the bash file, the OS asks for my system password.

> ./run_copy.sh
Password: *******

Is there a way to by-pass this or configure in such way that I only have to enter the password once.



Solution 1:[1]

Apparently, on my Macbook, I see /etc directory having symlinks with the /private/etc directory which is owned by the wheel group & root is part of that group. So, you would need to use sudo to copy to that directory.

With that said on a Linux machine, you can work around this by adding your group to a new file in the /etc/sudoers.d/<group-name> path.

<grp-name> ALL=(ALL) NOPASSWD:ALL

I've just tried this on my mac, I could copy files onto /private/etc directory without entering the sudo password prompt.

Unfortunately, this comes up with some risks as users of your group get privileged access without entering any password prompt. You might accidentally delete important system files etc.,

A more niche approach could be to allow selectively like <group> ALL = (ALL) NOPASSWD: /usr/local/bin/copy-script. This way, they can't run all scripts/commands with sudo privileges.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1