views:

202

answers:

3

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.

+2  A: 

For Unix-based systems:

Command line:

$ hostname "host.domain.com"

Python (sort of):

import os
os.system('hostname "host.domain.com"')
Karmastan
Note that this won't work if you're not `root`.
R. Bemrose
And, depending on the system/flavor/distribution, will revert to something stored in a file somewhere when you reboot.
Jed Smith
A: 

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).

Lorenzo
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.
Nick
+1  A: 

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

Jasper
How can you do that programmatically?
Lorenzo