Skip to content

Instantly share code, notes, and snippets.

@copley
Created August 25, 2024 11:27
Show Gist options
  • Save copley/6c87f34acca0848d84850067dc4d4650 to your computer and use it in GitHub Desktop.
Save copley/6c87f34acca0848d84850067dc4d4650 to your computer and use it in GitHub Desktop.
etc/hosts2
To allow all privileges for Account B on your macOS laptop while preventing Account B from modifying the /etc/hosts file, you need to modify the file permissions specifically for the /etc/hosts file. This way, Account B will have administrative privileges on the system, except for modifying this specific file.
Here's how you can do this:
Step-by-Step Guide
Log in as Admin Account A: Make sure you are logged in as Admin Account A, which will be the primary administrator.
Change Ownership of the /etc/hosts File: You need to change the ownership and permissions of the /etc/hosts file so that only Admin Account A (and not Admin Account B) can modify it. Open Terminal and run the following commands:
bash
Copy code
sudo chown root:wheel /etc/hosts
sudo chmod 644 /etc/hosts
chown root:wheel /etc/hosts: This command sets the owner to root and the group to wheel (default for system files).
chmod 644 /etc/hosts: This command sets the permissions to read/write for the owner (root) and read-only for everyone else.
Remove Write Permissions for All Users Except Root:
Since both admin accounts are likely part of the wheel group, removing write access to the /etc/hosts file for the group and others will prevent Account B from modifying it.
bash
Copy code
sudo chmod 644 /etc/hosts
This command ensures:
Owner (root) has read and write permissions (rw-).
Group (wheel) and others have read-only permissions (r--).
Verify Permissions: You can verify that the permissions are set correctly by running:
bash
Copy code
ls -l /etc/hosts
The output should look like this:
bash
Copy code
-rw-r--r-- 1 root wheel /etc/hosts
This indicates that only the root user has write permissions, and all other users, including admin users like Account B, have read-only access.
Prevent Account B from Using sudo to Edit /etc/hosts: To ensure that Account B cannot use sudo to regain write access to /etc/hosts, you need to modify the sudoers file.
Note: This step is optional and involves editing the sudoers file, which should be done with caution to avoid locking yourself out of administrative capabilities.
Open the sudoers file using the visudo command (which is the safest way to edit it):
bash
Copy code
sudo visudo
Add the following line to the end of the file to prevent Account B from modifying /etc/hosts:
bash
Copy code
B ALL=(ALL) ALL, !/bin/nano /etc/hosts, !/bin/vim /etc/hosts, !/usr/bin/vi /etc/hosts
This line allows Account B to use sudo for all commands except editing the /etc/hosts file with nano, vim, or vi.
Save and exit the visudo editor (usually by pressing Ctrl + X, then Y, and Enter).
Summary
By following these steps, you will:
Allow Account B to have administrative privileges for everything else on the system.
Prevent Account B from modifying the /etc/hosts file directly or through sudo.
These changes will ensure that only Admin Account A or the root user can modify the /etc/hosts file, securing it from unauthorized changes by Admin Account B.
You said:
for the command that does this "This line allows Account B to use sudo for all commands except editing the /etc/hosts file with nano, vim, or vi." How would this command look if account B was named, book1
ChatGPT said:
ChatGPT
If Account B is named book1, the line you would add to the sudoers file to prevent book1 from using sudo to edit the /etc/hosts file with nano, vim, or vi would look like this:
bash
Copy code
book1 ALL=(ALL) ALL, !/bin/nano /etc/hosts, !/bin/vim /etc/hosts, !/usr/bin/vi /etc/hosts
Explanation:
book1: The username of the account.
ALL=(ALL) ALL: This part allows book1 to run all commands as any user on any host.
!, followed by the command paths: The ! character is used to negate the following commands, meaning book1 is not allowed to use sudo to run these specific commands.
Full Steps to Implement:
Open the sudoers File Safely:
Use the visudo command to edit the sudoers file. This command checks for syntax errors before saving, which helps prevent misconfigurations that could lock you out of sudo access.
bash
Copy code
sudo visudo
Add the Restriction for User book1:
Scroll to the end of the file and add the following line:
bash
Copy code
book1 ALL=(ALL) ALL, !/bin/nano /etc/hosts, !/bin/vim /etc/hosts, !/usr/bin/vi /etc/hosts
Save and Exit:
If you're using visudo with the default nano editor: Press Ctrl + X, then Y to confirm saving the changes, and Enter to exit.
If you're using vim: Press Esc, then type :wq and press Enter to write the changes and quit.
By adding this line to the sudoers file, book1 will be able to use sudo for all commands except editing the /etc/hosts file with nano, vim, or vi. This configuration helps maintain the integrity of the /etc/hosts file while still granting administrative privileges for other tasks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment