Dealing with MacOS extended file attributes

A year or so ago, I ran into a mysterious issue whereby I couldn’t add new machines to my .ssh/known_hosts file. A few days ago, after restoring from Time Machine “the wrong way” (copying some directories directly out of the filesystem on the Time Machine backup) I ran into them again.

Symptoms: I couldn’t add new host keys to my known hosts file, nor could I remove old ones.

Directory listing:

-rw-r--r--@ 1 me  staff  16588 10 Mar 20:59 .ssh/known_hosts

The trailing @ means the file has extended attributes. After some work with a search engine, I found out I wanted to use the xattr command, or perhaps “ls -lO@”. Either way, the attributes preventing me from modifying the files were:

com.apple.metadata:_kTimeMachineNewestSnapshot
com.apple.metadata:_kTimeMachineOldestSnapshot

Running the appropriate incantation of xattr -d on the affected files (it wasn’t just my known_hosts file) worked a treat.