0

I think I made a mistake by editing /etc/hosts within sublime text, because immediately afterward I was unable to run "sudo xxx". It would ask for a password, and then never resolve once I'd typed it in.

I tried to edit /etc/hosts, but it always required sudo, which never resolved, so that didn't work. In the end I just deleted the file, but I'm stuck now with not being able to recreate it without access to sudo. Any ideas?

migrated from serverfault.com Apr 11 '17 at 11:12

This question came from our site for system and network administrators.

  • 1
    What do you mean by "resolving a password"? - Hosts file are used for name resolution - i.e. converting host names to IP-addresses. You don't resolve password. The easiest way to fix your problem is to reboot the Mac into single-user mode by holding down Cmd-s during boot. From single user mode you can recreate the hosts file. You could also take out the drive and attach to a different Mac and recreate the file from there. – jksoegaard Apr 11 '17 at 11:34
  • Easiest way is restore from backup - or in this case reinstall the OS – Mark Apr 11 '17 at 11:40
  • @jksoegaard "resolving a password" was just referencing that when I type in a password and hit submit, nothing happens. It just hangs indefinitely. I'll try single-user mode suggestion, thanks! – Jeremy Smith Apr 11 '17 at 11:51
  • You could boot into target disk mode by holding T during boot. Once in target disk mode you can connect your Mac to another computer using Thunderbolt, Firewire, or USB-C, and edit files on the hard drive. – audiomason Apr 11 '17 at 13:58
1

The file /etc/hosts is in no way related to the ability to run sudo. You probably hosed your sudoers file in a previous step.

To solve your problem boot to Recovery Mode and open Terminal in the menubar > Utilities.

  • Run df to get the name of your main system volume.
  • Change your working directory to /Volumes//etc:

    cd /Volumes/Macintosh\ HD/etc #"Macintosh HD" is just an example name here
    
  • Copy the spare files hosts~orig and sudoers~orig (or with similar names which may exist as "backup" in /etc by default - check this with ls!):

    cp hosts~orig hosts
    cp -i sudoers~orig sudoers #enter yes here
    

    If they don't exist create the hosts file and and check your sudoers file:

    /Volumes/<name_of_main_volume>/usr/bin/nano /Volumes/<name_of_main_volume>/etc/hosts
    

    The content of the default hosts file is.

    ##
    # Host Database
    #
    # localhost is used to configure the loopback interface
    # when the system is booting.  Do not change this entry.
    ##
    127.0.0.1   localhost
    255.255.255.255 broadcasthost
    ::1             localhost 
    

    The POSIX permissions are root:wheel 644. Modify this with chown and chmod if necessary.

    /Volumes//usr/bin/nano /Volumes//etc/sudoers

    A minimal (to type less!) working sudoers file looks like this:

    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"
    
    root        ALL = (ALL) ALL
    %admin      ALL = (ALL) ALL
    

    The POSIX permissions are root:wheel 440. Modify this with chown and chmod if necessary.

  • Reboot your Mac by entering:

    reboot
    

This can also be done in single-user-mode after mounting the root volume rw.


After regaining the ability to run sudo, either extract it from the macOS installer app with Pacifist or from a backup or modify it yourself with visuso. The default content is:

#
# 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
  • You said, "Copy the spare files hosts~orig and shudders~orig:", where are these "spare files" coming from? They don't exist in my 10.12.3 build. – user3439894 Apr 11 '17 at 14:54
  • They don't exist in my macOS 10.12.3 /etc directory. – user3439894 Apr 11 '17 at 15:01
  • @user3439894 You are right. Obviously a strange coincidence that both files exist in my install – klanomath Apr 11 '17 at 15:24
  • @klanomath On my current clean install, there are a lot of ~orig and ~previous files. – Max Ried Apr 11 '17 at 16:04
0

While you are booted normally to your regular account, do the following in Terminal:

touch sudoers.ori hosts.ori
open -e sudoers.ori hosts.ori
  • Note: This assumes you're in your Home directory when executing these commands.

In the hosts.ori file, copy and paste the following:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost

Then save and close the hosts.ori file.

In the sudoers.ori file, copy and paste the following:

#
# 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

Then save and close the sudoers.ori file.

Now from the Recovery HD boot in Terminal, do the following:

cd /Volumes/Macintosh\ HD/etc
cp /Volumes/Macintosh\ HD/Users/*/hosts.ori hosts
cp /Volumes/Macintosh\ HD/Users/*/sudoers.ori sudoers
chown root:wheel hosts
chown root:wheel sudoers
chmod 0644 hosts
chmod 440 sudoers
reboot
  • Note: Macintosh HD is the default name, however if you've changed it for whatever reason, change it appropriately in the command line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .