sudo command corrupted

Why my sudo command is running in this way? I think this command is corrupted. How to fix this?

MacBook Pro 13″, macOS 11.6

Posted on Oct 16, 2021 12:29 AM

Reply
18 replies

Oct 16, 2021 3:53 AM in response to ramim1207

What do you want to use sudo for?


sudo is not supposed to be used this way.


You should type


sudo <command>


where <command> is whatever you need to execute with elevated privileges.


For example you might do


sudo tmutil deletelocalsnapshots /


to delete Time Machine snapshots.

You don't just type "sudo" on its own.


So: what do you need sudo for?


Careful: if you are not sure, just don't. You risk making your mac unbootable.

Oct 20, 2021 5:28 AM in response to ramim1207

No. But is writeable can create any problem?

Yes. Someone can modify the sudoer files to add elevated privileges for processes and users.


If you successfully ran the chmod command, it should no longer be writeable.

Since you had to use the sudo command to run the change mode command, it will report the status of sudoers.d directory when it runs. If you run sudo now, do you still get errors?

If you just run sudo at the prompt with nothing else, you should get the usage instructions and no errors.

Oct 16, 2021 5:21 AM in response to ramim1207

You certainly do have something wrong with your sudoers file, which may be the cause of the other errors, but I don't know.

Something has made the file writeable, which it should not be. That is why it cannot find any valid sources.


Perhaps editing it with visudo will make it unwritable, but I would remove everything in the file and copy into it valid sudoers file contents.


Do you know of anything you've installed that would attempt to alter the sudoers file?

Oct 16, 2021 1:01 PM in response to ramim1207

ramim1207 wrote:

I want to uninstall java from my macbook. For that I have to run some sudo <command>. Can you just help me to reset sudo command?

I can post what is in my sudoers file and you can use visudo to create a new file, but why would you trust anybody on the internet?


#
# Sample /etc/sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.

##
# Override built-in defaults
##
Defaults	env_reset
Defaults	env_keep += "BLOCKSIZE"
Defaults	env_keep += "COLORFGBG COLORTERM"
Defaults	env_keep += "__CF_USER_TEXT_ENCODING"
Defaults	env_keep += "CHARSET LANG LANGUAGE LC_ALL LC_COLLATE LC_CTYPE"
Defaults	env_keep += "LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME"
Defaults	env_keep += "LINES COLUMNS"
Defaults	env_keep += "LSCOLORS"
Defaults	env_keep += "SSH_AUTH_SOCK"
Defaults	env_keep += "TZ"
Defaults	env_keep += "DISPLAY XAUTHORIZATION XAUTHORITY"
Defaults	env_keep += "EDITOR VISUAL"
Defaults	env_keep += "HOME MAIL"

Defaults	lecture_file = "/etc/sudo_lecture"

##
# User alias specification
##
# User_Alias	FULLTIMERS = millert, mikef, dowdy

##
# Runas alias specification
##
# Runas_Alias	OP = root, operator

##
# Host alias specification
##
# Host_Alias	CUNETS = 128.138.0.0/255.255.0.0
# Host_Alias	CSNETS = 128.138.243.0, 128.138.204.0/24, 128.138.242.0
# Host_Alias	SERVERS = master, mail, www, ns
# Host_Alias	CDROM = orion, perseus, hercules

##
# Cmnd alias specification
##
# Cmnd_Alias	PAGERS = /usr/bin/more, /usr/bin/pg, /usr/bin/less

##
# User specification
##

# root and users in group wheel can run anything on any machine as any user
root		ALL = (ALL) ALL
%admin		ALL = (ALL) ALL

## Read drop-in files from /private/etc/sudoers.d
## (the '#' here does not indicate a comment)
#includedir /private/etc/sudoers.d

I have no idea if replacing your sudoers file will solve the rest of the issues.

Oct 16, 2021 4:30 PM in response to ramim1207

Hi ramim1207,


To uninstall Java, use Barney-15E's suggestion (delete the relevant folders specified).


To reset your sudoers file back to good condition, try these steps. It is imperative that they are followed exactly as written.


  1. Back up your Mac with Time Machine, in case something goes wrong later on. Do not skip this step.
  2. Create a new empty plain-text file in TextEdit. (In TextEdit, go to the top of the screen and select Format -> Make Plain Text.
  3. Paste the entire contents of the default sudoers file posted by Barney-15E. (I verified it as well to be safe.)
  4. Save the file as "sudoers" (without the quotes) on your desktop. Be sure to uncheck "If no extension is provided, use .txt".
  5. Enable the root user account via these steps: How to enable the root user on your Mac or change your root password - Apple Support
  6. Log out of your user account.
  7. On the login screen, if you normally see one or more account pictures, there should be an "Other" icon. Click on it, then enter the following credentials:
    1. Username: root
    2. Password: (the root password you set earlier in step 5)
  8. When the desktop appears, you have successfully signed into the root user account. You should select a distinctive desktop background from System Preferences -> Desktop & Screen Saver to remind you that you're not on your usual account (and in fact, using the most dangerous account on the system).
  9. Open a new Finder window. At the top of the screen, select Go -> Go to Folder...
  10. Type this path and hit Enter (Return): /Users
  11. Open the folder for your regular user account, then open the Desktop folder.
  12. Right-click (or hold down Control as you click on) the sudoers file you made earlier, then select Copy.
  13. At the top of the screen, select Go -> Go to Folder...
  14. Type this path and hit Enter (Return): /private/etc
  15. Right-click (or hold down Control as you click on) the Finder window background, then select Paste.
  16. If you're warned that a previous file already exists, select Replace.
  17. Go to the Applications folder, open the Utilities folder, then open the Terminal app.
  18. Paste each of the below commands into Terminal, one at a time. Press Enter (Return) after each command:
    1. chown root /etc/sudoers
    2. chmod 440 /etc/sudoers
  19. Quit Terminal and log out of the root user. The sudoers file should now be repaired.
  20. Log in to your normal user account.
  21. Disable the root user account.
  22. Verify that sudo works as expected, by running a safe command (e.g. "sudo diskutil list").


EDIT: Once the sudo command is repaired, don't use it unless you know exactly what you're doing with it. Using sudo allows you to run any command as the root user, which is the most powerful (and dangerous) user on your Mac. Even though the root user may be "disabled" on your Mac, it can always be accessed through sudo.


The default sudo policy only allows administrators to use it. If you have multiple users on your Mac, most of them should be made standard users so that they cannot abuse sudo.

Oct 17, 2021 6:28 AM in response to ramim1207

Is it ok now?

Copy and paste in this command:

ls -dl /private/etc/sudoers.d

If the returned line does not start with drwxr-xr-x, run this command:

sudo chmod 755 /private/etc/sudoers.d


Did you ever do anything to change the permissions on the startup drive? Based on both of those items being "world writeable," it appears you have have changed permissions and used "Apply to Enclosed" in the gear menu or used the chmod command to change permissions on the entire drive. If you did, you will likely have lots of other random/strange issues besides this one.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

sudo command corrupted

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.