We help IT Professionals succeed at work.

How Do edit Hosts File on multiple machines

Medium Priority
2,081 Views
Last Modified: 2012-05-12
I would like to change the Host files to switch to a different IP address for multiple pcs.  I am not a programmer.
Comment
Watch Question

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Are you going to run a BAT file on each PC, or run it on a central PC and try to update the hosts file remotely.  Are the target machines Windows 8, as those will require admin privs to update the hosts file.  Are they part of a domain that you can do it with a logon script or such?

~bp
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Hosts tables?!  Can you not use a DNS entry?!

Steve
DarinTCHSenior CyberSecurity Engineer
CERTIFIED EXPERT

Commented:
the hosts file is a simple notepad
usually located in the etc folder
example
%SystemRoot%\system32\drivers\etc\hosts

see also
http://www.timeatlas.com/term_to_learn/general/taking_control_with_the_hosts_file
http://en.wikipedia.org/wiki/Hosts_(file)
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Also is this an existing entry in all the hosts tables, if so presumably you would want other entries to stay and this one be replaced.  Or can we replace the hosts table on each pc with a new standarx one.

Steve

Author

Commented:
would perfer to run .bat on a central PC and try to update the hosts file remotely.

Author

Commented:
And yes they are  part of a domain

Commented:
Can you make a master hosts file to be used to replace the hosts files on all the desired PCs, or you absolutly need to modify their hosts file?

Also, since your on a domain, you have a DNS server. Why not blanking all your hosts files and making one entry on your DNS server?

Cheers,
Rene
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
Exactly my point!

Commented:
Sorry Steve, I only read for some ques of answers, not previously having the time to thorowly anderstand posts.

@1baddude: Please disregard my comment 37065782 as it is a refrase of Steve's comment without adding anything or quoting him as a reference.

Cheers,
Rene
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
@Rene - turn off the defensive comments... was thanking you for backing up my suggestion too!

Commented:
Thanks Steve :)

Commented:
I had a situation where one department needed a hosts file entry because of an application they ran. I did it using a simple vbscript. I think I still have it if you want to try it out.

It added a line to the end as the machines had other entries and not alll were the same so I couldn't just overwrite them. I pushed it out using Ghost and it ran silently on Win2k and XP systems.

Interested?  I can attach tomorrow if you are.
IT Consultancy
CERTIFIED EXPERT
Commented:
To change hosts is easy enough to add one on the end, i.e.

echo 10.10.10.10  somehost >> "%windir%\system32\drivers\etc\hosts"

which you could do from AD startup script, psexec, or from admin w/s

and you can even check if a line is there already:

find /i "somehost" "%windir%\system32\drivers\etc\hosts" || echo 10.10.10.10  somehost >> "%windir%\system32\drivers\etc\hosts"

but editing to change a current line means looping down the file looking for the relevant line, or something like this.  You could run it as a startup script from GPO or if the users have admin privleges run it as them.

If that isn't an option we could run a script like this from psexec pushing to remote PC's but that relies on them being on of course.

@echo off
set host=somehost
set ip=10.10.10.10
set hosts=%windir%\system32\drivers\etc\hosts

find /i "%host%" %hosts%" && (
  copy /Y "%hosts%" "%hosts%.old"
  find /i /v "%host%" <"%hosts%.old% > "%hosts%"
)
echo %ip%    %host% >> %hosts%

i.e. look for somehost in the file.  If it is found then copy the hosts table to hosts.old.  Run find over hosts.old excluding the line "somehost" and back into the file hosts.  Then append the new ip / somehost on the end.