1

I need to automate the changing of the hostname of a computer, but I can't figure out how to do it inside a program. My options are open; I would be happy with a solution in any of the following:

Command line
Java
Python
C# (would prefer one of the other 3, but this is ok)

It would be helpful to learn how to do this on both Linux and Windows.

5
  • Windows? Linux? You said hostname, so I guess it may be a *nix but then you mentioned C#? is it Windows? Commented Jul 2, 2010 at 16:34
  • I would ask this question on serverfault.com. Once you have a command line solution the programming is trivial. Commented Jul 2, 2010 at 16:38
  • @P.Brian.Mackey: this is only true on non-Windows systems. In Windows you are not supposed to use the command line to do administrative tasks... Commented Jul 2, 2010 at 16:44
  • @Lorenzo: No offense, but I have never heard that before. Do you have any MSDN or other documentation to cite? Commented Jul 2, 2010 at 16:46
  • @Brian: it is obvious if you have ever used Windows. It is almost impossible to administer (or use) just with command line, as Windows isn't a collection of command line tools and configuration files like UNIX. In Windows configuration is often doable only trough the GUI. Now it is changing a bit with PowerShell and Windows Management Infrastructure (WMI). Commented Jul 2, 2010 at 23:27

3 Answers 3

3

For Unix-based systems:

Command line:

$ hostname "host.domain.com"

Python (sort of):

import os
os.system('hostname "host.domain.com"')
Sign up to request clarification or add additional context in comments.

1 Comment

And, depending on the system/flavor/distribution, will revert to something stored in a file somewhere when you reboot.
2

You could also do this in powershell on windows. Seems safer to me than changing registry keys by hand :

$computer = Get-WmiObject Win32_ComputerSystem -OriginalPCname OriginalName -computername $originalPCName
$computer.Rename("NEWCOMPUTERNAME")
}

see this poshcode page

1 Comment

How can you do that programmatically?
0

In Windows you have to modify registry keys and the reboot the system.

You actually have to change two entries:

HostName under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\TcpIp\Parameters

and

ComputerName under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName

Please note that if the computer ha joined an NT Domain this change could be harmful (and in this case you have an additional entry to change under TcpIp\Parameters).

1 Comment

Ok cool, a couple of questions: 1. The ComputerName entry is all caps. When I change it should I also put the name in all caps? 2. What if I want to change the DNS suffix. Where would that be located? Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.