Issue
Got autorized_keys
with old key from a specific user say alice
and would like to retain last amended public key & delete rest of them.
Here is example content of autorized_keys file:
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA0YnNYVo/lHtSLtDUpQIqgS8WIpoBA7NRfaheQZXjqOjDMmBZA0cJy+ng3kmUuh5SbNapjaAYFwMHjLv6biHAasWgE77rJYXN8+JpHi/JwuXUnVUTwHrKXvkMr0sPgDxCEZO15GtiJwVPfmxpl3RBB/we5a+A6fhvXCyk/dxQRdU= alice-publickey
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDp6d0GmH5Q+lbx832n2W43Ikx8cpTeM8MRQ1yJmnhzaCSkQk9jWkwB5Msvj7uR923k0qLCZQ4ZbdPQ7liK9+Ks/7c0yDrBkU1KVOBNDQD1ck9Iwo2qwsS72b1kf4zeNKVJWkKBcFg7qxOmjIy9wNJ0XhbHmo/9gkp/hoOY3g0I5tExIlVdTH+uhEmOerghChTHTWU/kymZW0yYbDKl9zyPEDY9Hf/0Mt6g2dz+q3/cfcwCIZIeR5/Fjt7xXbt+fqmaiws1veGacw60u8ouMJ4DL9CcXRUbZHdEY4v2yIcEZRXMv9Q/6s0TcuC9s6Z5n2RzV8eW3RCZuzMWmYlkcA/H bob-publickey
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAsB3XsrmTHWygTrL8dT/Z5+qjjXfPxH7IPlyFZt0QcHA1a16g0ZPeEMB1RUu90vtbEvKjq+hAAANeLcmVW85i8LSsO3xcuCGTFNVNx2up7Fv7YxiHMJpfQqyiBW09HixrPiu+9Wl/olTB9kPu+fZ/jRqok3ZTARnQDoE2g1RWnNU= alice@mcosmac
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2UFK8Y7mODUZ7a2glErZbKk9hUy1pCOiGz4arAlTBiJ884vrjGaYNGFyWiNvqNKxLWn6Cul1eyPF1M/Z6l6QE3biJgJPln2ZZIoMcsNiCrFl0x5tUdnyVnVGz5pZTI1/0IFFLE4jbW2grMHOC7z+ik3WOD5aTj8pUWeTIeppMKLwjnJhm0ALsmLZqhtwnhgLSg9m4NQmcLWt5s0P/Z91hUKw2tY5iV9yOu5tS+oTMoeV7ays9MXM9ozpehLhsbqn98O7k2UpQzoWEo093lAjzydnBICd5tKdVJyGcyi3BLY4xhU+dnJ9NIUuYtnLZEhAGCbOQEGNivkxDQv1ccRTx trudy@newMacs
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPbEzZDSZNKdGsH0sP10abmosX0yFquCEx2ibNzmh0AvvsR6pbA/pdl1i9XqIJlDBustWfmnChXEQz9Nft5LbM/moR6DAe7Wd70kmY8CreGgZ2kF8KCZ3bTkKYfO5PexKRCZ8IFRs6xswbfV0DJOmGnNo+wKOFDK/J7GVd9e1u6FebSo/Nr0QN/maXKN+Tc57GnV+j34ihD9PoRmfGYRIjm/vMkfUB9l1fZDEawdMO+Ats+G9gSvlhIm4TSP4sHk+zUJJff+j6ubMsGW0Flh4YjVJMMSU9vrpsTS5WiEbsZVxuWkrd+9jMo5RGDoWt2HADo4Dup0DlyM1pj2u1zbfl alice@brandNew
Below option looks to help me filter with sed & as we know with -i
option we can do in-file edits directly. But I am unsure on # of old keys registered on machine for user alice
.
grep alice authorized_keys | sed '1,2d'
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPbEzZDSZNKdGsH0sP10abmosX0yFquCEx2ibNzmh0AvvsR6pbA/pdl1i9XqIJlDBustWfmnChXEQz9Nft5LbM/moR6DAe7Wd70kmY8CreGgZ2kF8KCZ3bTkKYfO5PexKRCZ8IFRs6xswbfV0DJOmGnNo+wKOFDK/J7GVd9e1u6FebSo/Nr0QN/maXKN+Tc57GnV+j34ihD9PoRmfGYRIjm/vMkfUB9l1fZDEawdMO+Ats+G9gSvlhIm4TSP4sHk+zUJJff+j6ubMsGW0Flh4YjVJMMSU9vrpsTS5WiEbsZVxuWkrd+9jMo5RGDoWt2HADo4Dup0DlyM1pj2u1zbfl alice@brandNew
Is there a way to delete all old keys of alice
except last one in-place holding this string match of alice
?
Solution
Remove a line with alice when you have another alice in the file.
Repeat until all alices except the last one are removed.
sed -rz ':a;s/(\n|^)[^\n]*alice[^\n]*\n(.*alice)/\1\2/;ta' authorized_keys
Answered By - Walter A Answer Checked By - Robin (WPSolving Admin)