How to write to a NULL device/file on Windows (10) so I can **not** read back what was written?

3.4k Views Asked by At

Windows does have a concept for null devices as detailed here, here or here for example. All examples that I can find are about redirecting streams.

While porting a script from Linux to Windows, I came across something odd: If nul is opened like a file, I an write to it, close it, re-open it and read the data back. Sometimes. Checking the working directory, I can see a file named nul being created and growing as I write data to it. Ironically, I can not delete it from the explorer, only from the command line.

Windows 10 comes with its a native implementation of OpenSSH nowadays. I wanted to use an old trick: When it is looking for the known_hosts file, I want to point it to a null device instead. On Linux, this looks as follows: ssh -o UserKnownHostsFile=/dev/null user@host. On Windows, I tested it as follows: ssh -o UserKnownHostsFile=nul user@host. I got a file named nul containing the hosts. I also tried NUL, null and NULL with the same result. Trying to point to \Device\Null or /Device/Null causes ssh to complain that it can not find/open those files.

How do I correctly use and/or point to the null device on Windows (10)?

2

There are 2 best solutions below

1
On BEST ANSWER

The JS crowd has a solution: I need to point to \\.\NUL. This is the actual name or path of the null device, apparently.

0
On

What I'm doing is create a file C:\Users\myself\.ssh\config (just like on Linux), with this:

# Don't save local computers to known_hosts
Host localhost 127.?*.?*.?* *.local 10.?*.?*.?* 192.168.?*.?* 172.16.?*.?* 172.17.?*.?* 172.18.?*.?* 172.19.?*.?* 172.2?.?* 172.30.?*.?* 172.31.?*.?*
    StrictHostKeyChecking no
    UserKnownHostsFile=\\.\NUL

This matches most IPv4 thingies/whatever that are considered private networks. Do this at home, but not in your company network though - because of reasons.