Check out this quick, simple-syntax tool for taking ownership of files
Ever since Windows Vista appeared, readers have repeatedly sent me the same question: "Why can't I modify the HOSTS file?" Many people modify the
HOSTS file—located in \windows\system32\drivers\etc—to protect the system from sites that promote the spread of malware. However, Vista blocks this kind of modification. By default, Vista administrators don't have permission to delete most files in the \windows directory—nor do they own those files. Therefore, to modify HOSTS, you first need to take ownership of the file. You've been able to take ownership of a file from the GUI ever since Windows NT 3.1, but that built-in capability isn't terribly useful for remote administration. Fortunately, Vista and Windows Server 2003 include a new command-line tool called Takeown that lets you take ownership of files or folders.
How It Works
Takeown's basic syntax looks like
takeown /f <file or folder name>
You'll probably need to run Takeown with your full administrative powers, and Vista's User Account Control might get in the way of that. Therefore, before you try to run Takeown, go to Start, All Programs, Accessories, Command Prompt—as you've probably done many times. But this time, in Vista, right-click the Command Prompt icon and choose Run as administrator. After you verify the action by clicking Confirm, you'll have what Vista refers to as an elevated command prompt.
You can now take ownership of your system's HOSTS file:
takeown /f C:\windows\system32\drivers\etc\hosts
(This command assumes that you have Vista on the C drive, of course. If Vista actually resides somewhere else, just change the drive letter.) Vista will respond with something like
SUCCESS: The file (or folder): "C:\windows\ system32\drivers\etc\hosts" now owned by user <youraccountname>.
At this point, you can assign yourself write permissions to the HOSTS file and make changes.
Takeown has more options than just /f, of course. To take ownership of an entire tree of folders and files, you can add the /r option. Thus, if you wanted to take ownership of a folder named C:\documents, as well as any files and folders inside C:\documents, you'd type
takeown /f C:\documents /r
Tackling an Annoyance
If you've ever tried to take ownership of a tree of folders, you might have run into something of an annoyance—namely, Windows' tendency to ask if you really, truly want to take ownership. Suppose, for example, that you not only don't own C:\documents but you also don't have permission to look in C:\documents. In that case, taking ownership of C:\ documents wouldn't equip you to take control of any subfolders in C:\documents for the simple reason that although you would now own C:\documents, you would still lack the NTFS permissions necessary to list its subfolders.
Feasibly, you could take ownership of each directory, one at a time, then add a list folders permission to that folder, which would let you see the subfolders in that folder, which in turn would let you take ownership of the subfolders. Thankfully, such manual labor isn't necessary: Whenever you use the /r option with Takeown, the command pauses at each folder and asks whether you'd like Takeown to add the list folders permission to that folder's NTFS permissions. That's a nice touch, but it means that taking control of a folder containing dozens of folders could get a bit tedious.
The good news is that you can configure Takeown so that it doesn't ask you if you want to add the list folders permission and instead just assumes that you'll answer yes or no. To do that, you use the /d option, followed by a y or an n. So, to take ownership of C:\documents and all its subfolders, while granting yourself the list folders permission, you'd type
takeown /f C:\documents /r /d y
Note, however, that in the process of giving you permission to list folders, Takeown /r first removes any other explicit permissions on that folder! Inherited permissions seem unaffected in my tests, however.
To my knowledge, Windows has previously offered only one command-line tool to take ownership: the resource kit's Subinacl tool. Subinacl is a powerful tool, but its syntax can require some study. Takeown offers a command-line alternative with simple syntax.