Unable to update host file error - have tried all solutions in forum, UAC prompt for cmd failing?

What issue or error are you experiencing?

After installation when creating a new site, I get the error “Unable to update the hosts file make sure it’s not locked by antivirus” no matter what I do. I have followed all directions and then some in other threads with the same or similar errors.

  • I do not have anti virus software other than basic Windows 10
  • Local is given all firewall permissions
  • My hosts file is NOT read only
  • I have repeatedly fresh installed both for just my user and for all users
  • I uninstalled my VPN
  • I checked if things were listening on the ports, there do not appear to be
  • I uninstalled xampp
  • I have run as administrator
  • I have installed and tried with no programs running, firewall turned off, and window’s real time virus thing off as well
  • Local appears to have all permissions needed
  • I tried installing on my partner’s computer and it worked as expected, so it’s definitely something about my particular laptop. The only difference I noticed is that during the creation of a new site on that computer there was a pop up asking for permission for command to make changes to the computer, which was granted. I am NOT given this pop up on the computer where installation isn’t working. I don’t know why this would happen or how to get the pop up to work on this laptop

I suspect this is the relevant line of the verbose logs file:

{"class":"HostsFileService","level":"debug","message":"Updating hosts with command","thread":"main","timestamp":"2024-02-28T00:55:25.558Z","updateCommand":"C:\\Users\\Ronan\\AppData\\Local\\Programs\\Local\\Local.exe","updateCommandArgs":["%%appPath%%\\main\\dns\\workers\\updateHostsFileWorker.js","test.local","www.test.local"]}
{"class":"HostsFileService","error":{},"exitCode":{},"level":"error","message":"Could not update hosts.","stderr":{},"stdout":{},"thread":"main","timestamp":"2024-02-28T00:55:25.633Z"}{"class":"HostsFileService","level":"debug","message":"Updating hosts with command","thread":"main","timestamp":"2024-02-28T00:55:25.558Z","updateCommand":"C:\\Users\\Ronan\\AppData\\Local\\Programs\\Local\\Local.exe","updateCommandArgs":["%%appPath%%\\main\\dns\\workers\\updateHostsFileWorker.js","test.local","www.test.local"]}
{"class":"HostsFileService","error":{},"exitCode":{},"level":"error","message":"Could not update hosts.","stderr":{},"stdout":{},"thread":"main","timestamp":"2024-02-28T00:55:25.633Z"}

The only thing I can think of that may be relevant that I’ve done lately is I recently installed a CLI tool that required I add a new path to the system paths. I also needed to add the path to allow the netstat command to work to check the ports.

Here’s a picture of what’s in my path variable. I compared it to the other computer and while they’re different it doesn’t seem like anything was glaringly absent from my computer
PATH

If there’s something that should be there and isn’t, let me know and I’ll add it and give it a try.

Aside from that I’m at a loss as to why this is happening. My end goal is to migrate a live WP site to my local computer so I can stop paying for hosting and extended PHP support and I’d very much like to use this program rather than trying to set up xampp. Any help getting it to work is greatly appreciated. Let me know if there’s any more screen shots or files I can upload to help.


What steps can be taken to replicate the issue? Feel free to include screenshots, videos, etc

Install Local, create new site, choose preferred, name site test, create test log in, continue, get error.


System Details

  • Local Version:
    8.2.1-windows

  • Operating System (OS) and OS version:
    Windows 10 os
    Build 19045.4046 if that’s relevant


Local Logs

Attach your Local Logs here (Help Doc - Retrieving Local’s Log)
local-lightning-verbose.log (12.1 KB)


Security Reminder
Local does a pretty good job of scrubbing private info from the logs and the errors it produces, however there’s always the possibility that something private can come through. Because these are public forums, always review the screenshots you are sharing to make sure there isn’t private info like passwords being displayed.

Hi @Graywaren

After doing everything else above did you try a full uninstall and resinstall?

@Nick-B Yes, as I listed, I tried that multiple times. I followed all instructions listed on the full uninstall guide each time.

I also get this notification, though no popup to grant privileges comes with it
Notification

I’ve gone through the entire process multiple times again. I’m still getting cannot update hosts file etc. From what I can tell from all this, and comparing it to the other computer, Local is failing to use CMD to edit the hosts file. The other computer generated the UAC prompt to grant cmd permission to make changes during website creation, my computer does not. Any idea how to fix this issue? Here’s more things I’ve tried:

  • I made sure the hosts file security matched this video
  • Tried with UAC on always notify, default, and never notify with restarts between each
  • I tried Method 3 from this, linked to in yet another thread about this error
  • Replaced the hosts file with a new copy
  • I found this ticket, which sounds nearly identical to the issue I’m having, but I don’t have an associated program with .bat files as far as I can tell from checking, so that didn’t work to fix it. I even tried resetting all default apps and that didn’t work either.
    bat

In case it’s relevant that windows version on the computer it worked on is:
Win 10 Pro Build 19045

I found a picture in this thread of the UAC prompt that does not happen on my laptop:
68f1c6da7a1e5c8587825393b6acf037550130d7_2_690x431

My account is an administrator and admins have full control for the hosts file.

@Nick-B Any help at all?

Hey @Graywaren – I’ve always found this part of Windows to be confusing. One thing to double check, when you see that notification show up in the lower right corner of the screen, what happens when you click it? Does that show the pop-up window with the prompt?

Nope, nothing happens, the notification just disappears if I click it. The error message pops up simultaneously with that notification as well, there’s no delay between the two.

I tried installing Local 7 something just to see if an older version would work, and it does the same thing. I can try an even older version if there’s any chance it might make a difference.

@ben.turner Any updates? Any help? Any recommendations for another product to try since yours doesn’t work? I’ve got to get my live site taken down, and I was really hoping to use local because your interface made it easy, rather than fiddling about with xampp, but it’s been 10 days…

Hey @Graywaren – this is definitely an odd situation. The error in the error logs doesn’t give us much to go by and we haven’t been able to replicate on our end.

As you say, the issue seems specific to this machine since things appear to work on other systems.

I don’t have many ideas, but a couple of questions:

  • Have you tried creating a new, regular Windows user and installing Local for that user? That might help in ruling out if it’s an issue with your current user or the machine as a whole.
  • Additionally, for your current user, are there other applications which successfully trigger the UAC prompt?

In terms of unblocking you – I don’t have many alternative recommendations for hosting Local on your own Windows machine. You could always lean in to Wamp or home-roll your own infrastructure using Docker. Those work do work, but they also require a bit more manual work from you to get things done.

@ben.turner

It’s definitely the machine as a whole, though judging by the forums, there do appear to be other machines having the same issue. The thread referenced above your first reply seems likely another case of it, then the one I linked to that was able to solve it by removing associations with .bat files and there’s enough other various issues with the hosts file where the regular solution doesn’t work that it seems likely. If I hadn’t had access to a separate computer to try to see that I was missing a UAC prompt I wouldn’t know it was supposed to be there.

As far as I can tell, all other UAC prompts work, including for Local. It has no issue bringing up the UAC prompt for installing for all users or uninstalling. It also doesn’t have an issue bringing up the windows security prompts, including asking for admin password for that as well. It seems only related to the UAC prompt specifically for using command and editing the hosts file. Do you know of any other program that would need to use command in a similar way and bring up a UAC prompt that I can test to see if another program can bring up UAC specifically for command?

I tried installing with a different user. First as a standard user and just for that user. It prompted me to put in the admin password for allowing it through the firewall on windows security just fine, but while I was inputting that password, it errored for the hosts file. It does it immediately, no delay, no attempt to even raise the UAC prompt that I can tell, doesn’t make the same windows sound that the security prompts do, just the error sound. I then tried installing for all users, asks for admin pw just fine, but immediately errors for hosts file. I then made that user an admin and the same thing happened. It just goes straight to the error no matter what.

I decided to try editing the hosts file manually to see if it was possible and I was able to edit the file manually running notepad as an administrator, so it doesn’t seem to be an issue with the file itself. Are you able to give me a command I can run to see if I can edit the hosts file with command directly the way local would?

I also noticed in the error logs that it was failing to retrieve the windows version, but I don’t know if that’s relevant.

That’s a really interesting aspect of this and something that I’ve never heard of happening before.

I can’t think of anything off the top of my head. I doubt that there are many applications out there, built with Electron, and which try to edit the Hosts file.

There isn’t really a way to run the same command outside of Local.

I’m not quite sure where to go with this, especially since you mention that other “sudo prompts” are working. Since it appears to only affect this action, and the action is only working on the hosts file, my guess would be that there is some kind of setting within the OS for this file which is preventing Local from interacting with it.

I’ve done a bit more to try to figure out the issue. I tried doing the entire process as the built in administrator via enabling that with
net user administrator /active:yes, but that had no changes.

However, after some more experimenting involving borrowing another laptop, I’m not sure the problem lies with the hosts file. The laptop I borrowed has the same windows build as mine and it successfully elevates the UAC prompt for windows command processor and can therefore modify the hosts file. When the UAC prompt for Windows Command Processor appeared I check out the “more details” and saw this:

I check out the locations of those files on my original laptop. Cmd.exe was in exactly the same place, so that’s not the issue. Then I checked the temp folder. To do this I watched the temp folder during site creation on both laptops. On the working laptop, I saw the temp folder that contains the execute.bat folder pop up when the UAC prompt is elevated, and it disappears afterwards. However, on my laptop, that folder is never created. The moment in the creation process in which it should be created and then run the windows command process, the “unable to edit hosts file” error appears.

To me, this seems to imply that something is going wrong before it’s even to the point of attempting to edit the hosts file and that for some reason, this temp file with the instructions to run the editing of the hosts file isn’t created and therefore the whole hosts file editing process is not happening. That would seem to explain why the error log is blank with no info about why it failed. I did try this with the most recent version of local as well. Any idea what would be causing this to happen?

That sounds like maybe there’s some sort of permissions issue on the either the Temp folder of it’s parents. If you open the folder in File Explorer, right-click on it and examine the permissions, does anything look odd?

I suppose it could be that somehow the perms on that folder have become corrupted. You might also try deleting the contents of the AppData\Local\Temp folder because in theory those files are “temporary” and should be re-created by whatever apps consume them. Note that the “Local” in that path isn’t the LocalWP app – I think that more refers to the “Local Windows system” and as such that Temp folder is the temp folder for all apps on your system.

LocalWP successfully creates other temp files in the temp folder just fine, such as the node and mailpit.db files, so I don’t think it can be a permissions issue. Other apps use it just fine as well, nothing seems odd about it. The process is identical as the working laptop up until it tries to create the [randomstring] folder that then contains the execute.bat file, where it fails to create it.

I moved all temp files out of the folder to “delete” them when I originally started the process of comparing between the two laptops, so that didn’t affect anything either.

I’m aware the ~AppData\Local folder is a windows folder and not a LocalWP folder. I personally think the app should make all folders generated by it in installation say LocalWP instead of Local for the exact reason that it’s needlessly confusing for users when there’s multiple Local folders where some are important windows folders and some are this software. Especially when the full uninstall process involves needing to manually delete the correct one. It’d be a lot easier to find the correct things if all the folder apps were LocalWP.

That aside, is there any information about what generates the process of creating the temp folder/execute.bat file that I can investigate to see if the issue lies with them or otherwise upstream from that?

Thank you for helping us with your thorough investigation of this, @Graywaren. I’m sorry that Local fails to write the execute.bat to prompt for permission to write to /etc/hosts. I’d be interested to learn why.

…is there any information about what generates the process of creating the temp folder/execute.bat file that I can investigate to see if the issue lies with them or otherwise upstream from that

sudo-prompt is the package Local uses that writes the execute.bat to your temp folder. (Local plans to move to another package because sudo-prompt is now archived, but this likely requires building our own alternative because there are no stand-out cross-platform alternatives.)

Known issues in the sudo-prompt repo suggest usernames with special characters can cause failure on Windows. I don’t think these apply given the screenshots in your original post, but sharing just in case:

If you have Node.js installed, you could download the simplified test case I made below to discover if sudo-prompt gives error info when running on your system.

sudo-prompt-test.zip (20.9 KB)

To use it:

  1. Unzip the file.
  2. Feel free to read the contents of index.js — it just tries to call sudo-prompt to echo output.
  3. cd to the unzipped directory.
  4. Run node index.js. (Do not run npm install first. I bundled node_modules with the zip and made a light edit to node_modules/sudo_prompt/index.js to log errors in the WindowsElevate function on lines 534-538.)

If sudo-prompt fails, you may see error output with more info. If sudo-prompt works, you’ll see the auth prompt followed by “stdout: hello” in console output, implying the issue lies not with sudo-prompt but elsewhere in Local.

1 Like

We did it! I’ve got local working! Your sudo prompt test lead me straight to the problem. I installed node and ran index, which threw an error saying that powershell.exe was not a recognized command. Apparently my computer was missing the PATH variable for it. It seems my system variables, as opposed to the user variables doesn’t contain anything but dotnet and node for some reason :flushed:, not sure what happened there, but that’s next on my list to fix. Once I added powershell’s path to the user PATH environmental variable both the sudo prompt test and Local’s request to edit the host file worked perfectly. In case it is of interest for a patch update or your building of what will replace sudo prompt, forum posts I read in my research on powershell not being recognized recommended to call it with full pathing to avoid this and similar issues.

I would add checking if the PATH environmental variable contains a path to Powershell as part of the troubleshooting check list for this error, at least when checking anti virus and permissions on the host file comes back normal. I’ve documented the process for adding it to the user path variable and will post it in a separate reply below so it can be linked/referenced easily and hopefully be helpful to anyone else having this issue persist. Let me know if it needs any changes or notations.

Thanks for your test program! Glad to finally have figured out the issue. So frustrating how tiny little things like this are such a long process to find!

2 Likes

If you are encountering the Cannot Edit Hosts File error and do not receive a prompt asking to allow “Windows Command Processor” to make changes to your device such as in the following image, before the error happens, here is a solution to try.
Command

Check If Powershell PATH Variable Exists For User & Add It If Not

  1. Press the windows key or click the start button and type env, then click “Edit the system environmental variables” or manually locate it in the Advanced System Settings.

  2. In the window that pops up, click “Environmental Variables”
    2-EnvironmentalVariables

  3. Select the PATH variable so it is highlighted, then click “Edit”

  4. In the window that pops up, check the list of variables for "C:\WINDOWS\System32\WindowsPowerShell\v1.0" or "%SYSTEMROOT%\System32\WindowsPowerShell\v1.0"
    EditPath

  5. If you do not have "C:\WINDOWS\System32\WindowsPowerShell\v1.0" in the path variables, click “New”. If you don’t have it but do have "%SYSTEMROOT%\System32\WindowsPowerShell\v1.0", you may still try this part to see if it solves the issue.
    NewPath

  6. Write or paste "C:\WINDOWS\System32\WindowsPowerShell\v1.0" into the text box, the click okay on all the open windows. Restart your computer and then see if Local successfully edits the host file.
    WritePath

Note: The above process checks and adds the path variable for the listed user. If another user account tries to use local they may still have the issue. They can either also add the path, or it can be added to the path variable in the “System Variables” section. Admin privileges are required to add to the system path variable.

2 Likes

Great to hear! Thanks so much for your patience, your troubleshooting efforts and for taking the time to document your fix for this, @Graywaren.

1 Like

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.