{"id":1133,"date":"2026-03-25T08:15:58","date_gmt":"2026-03-25T08:15:58","guid":{"rendered":"https:\/\/techno.slomka.biz\/?p=1133"},"modified":"2026-03-25T08:43:52","modified_gmt":"2026-03-25T08:43:52","slug":"keeping-an-eye-on-ssh-keys-easily-review-authorized_keys-with-awk","status":"publish","type":"post","link":"https:\/\/techno.slomka.biz\/?p=1133","title":{"rendered":"Keeping an Eye on SSH Keys: Easily Review authorized_keys with awk"},"content":{"rendered":"\n<p>Hello fellow Admins!<\/p>\n\n\n\n<p>Who hasn&#8217;t been there? The <code>~\/.ssh\/authorized_keys<\/code> file is a crucial component for the security of your servers. But sometimes, especially when many keys are stored or very long keys are in use, the file can quickly become overwhelming. Getting a quick overview without endlessly scrolling through lengthy character strings is truly golden.<\/p>\n\n\n\n<p>That&#8217;s precisely why I have a small but powerful tip for you: an <code>awk<\/code> script that makes your <code>authorized_keys<\/code> more readable by shortening the SSH keys to a manageable length, without losing any vital information.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why is this useful?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Quick Overview:<\/strong> You can see at a glance which keys are stored, without being overwhelmed by incredibly long lines.<\/li>\n\n\n\n<li><strong>Ansible-Friendly:<\/strong> Your Ansible block markers remain exactly as they are, ensuring your automation continues to work seamlessly.<\/li>\n\n\n\n<li><strong>Troubleshooting:<\/strong> If something isn&#8217;t working, you can quickly find the relevant entries.<\/li>\n\n\n\n<li><strong>Security Check: <\/strong>During regular audits of your keys, you&#8217;ll immediately see which keys are present.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">The awk Magic (Ansible-Aware!)<\/h2>\n\n\n\n<p>This <code>awk<\/code> script shortens the actual SSH keys (the second column) to the first and last 10 characters, supplemented by three dots in the middle. This way, you know the key has been truncated, but you still have enough identifying features. All other columns (such as <code>command=\"\u2026\"<\/code> or <code>no-port-forwarding<\/code>) naturally remain untouched.<\/p>\n\n\n\n<p>\u24d8 <em>All options are detailed in the <a href=\"https:\/\/man.openbsd.org\/OpenBSD-current\/man8\/sshd.8#AUTHORIZED_KEYS_FILE_FORMAT\">sshd(8)<\/a> man page (section <strong>AUTHORIZED_KEYS FILE FORMAT<\/strong>).<\/em><\/p>\n\n\n\n<p>Crucially, it checks for Ansible block markers and prints those lines exactly as they are, without any modification.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#EEFFFF;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#304047;color:#d5ffff\">Awk<\/span><span role=\"button\" tabindex=\"0\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>awk '\nBEGIN { FS = \" \" } # Sets the field separator to a space\n{\n    # Check if the line is an Ansible block marker\n    if ($0 ~ \/^# (BEGIN|END) ANSIBLE MANAGED BLOCK\/) {\n        print # Print Ansible block markers as-is\n    } else {\n        # Process regular lines for key shortening\n        # Check if the line has a key in the second column\n        if (NF >= 2) {\n            key = $2 # Store the key in a variable\n            # If the key is longer than 20 characters, we'll shorten it\n            if (length(key) > 20) {\n                # The second column is reassigned: first 10 chars, \"...\", last 10 chars\n                $2 = substr(key, 1, 10) \"...\" substr(key, length(key) - 9, 10)\n            }\n        }\n        print # Print the (potentially shortened) line\n    }\n}' ~\/.ssh\/authorized_keys<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme\" style=\"background-color: #263238\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #EEFFFF\">awk &#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #89DDFF\">BEGIN<\/span><span style=\"color: #EEFFFF\"> { FS <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #C3E88D\"> <\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #EEFFFF\"> } <\/span><span style=\"color: #546E7A; font-style: italic\"># Sets the field separator to a space<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #546E7A; font-style: italic\"># Check if the line is an Ansible block marker<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    <\/span><span style=\"color: #89DDFF; font-style: italic\">if<\/span><span style=\"color: #EEFFFF\"> ($0 <\/span><span style=\"color: #89DDFF\">~<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF\">\/^<\/span><span style=\"color: #546E7A; font-style: italic\"># (BEGIN|END) ANSIBLE MANAGED BLOCK\/) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">        <\/span><span style=\"color: #F78C6C\">print<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #546E7A; font-style: italic\"># Print Ansible block markers as-is<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    } else {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">        <\/span><span style=\"color: #546E7A; font-style: italic\"># Process regular lines for key shortening<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">        <\/span><span style=\"color: #546E7A; font-style: italic\"># Check if the line has a key in the second column<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">        <\/span><span style=\"color: #89DDFF; font-style: italic\">if<\/span><span style=\"color: #EEFFFF\"> (NF <\/span><span style=\"color: #89DDFF\">&gt;=<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #F78C6C\">2<\/span><span style=\"color: #EEFFFF\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">            key <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> $2 <\/span><span style=\"color: #546E7A; font-style: italic\"># Store the key in a variable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">            <\/span><span style=\"color: #546E7A; font-style: italic\"># If the key is longer than 20 characters, we&#39;ll shorten it<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">            <\/span><span style=\"color: #89DDFF; font-style: italic\">if<\/span><span style=\"color: #EEFFFF\"> (<\/span><span style=\"color: #82AAFF\">length<\/span><span style=\"color: #EEFFFF\">(key) <\/span><span style=\"color: #89DDFF\">&gt;<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #F78C6C\">20<\/span><span style=\"color: #EEFFFF\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                <\/span><span style=\"color: #546E7A; font-style: italic\"># The second column is reassigned: first 10 chars, &quot;...&quot;, last 10 chars<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">                $2 <\/span><span style=\"color: #89DDFF\">=<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">substr<\/span><span style=\"color: #EEFFFF\">(key<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #F78C6C\">1<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #F78C6C\">10<\/span><span style=\"color: #EEFFFF\">) <\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #C3E88D\">...<\/span><span style=\"color: #89DDFF\">&quot;<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">substr<\/span><span style=\"color: #EEFFFF\">(key<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #82AAFF\">length<\/span><span style=\"color: #EEFFFF\">(key) <\/span><span style=\"color: #89DDFF\">-<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #F78C6C\">9<\/span><span style=\"color: #89DDFF\">,<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #F78C6C\">10<\/span><span style=\"color: #EEFFFF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">            }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">        <\/span><span style=\"color: #F78C6C\">print<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #546E7A; font-style: italic\"># Print the (potentially shortened) line<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #EEFFFF\">}&#39; <\/span><span style=\"color: #89DDFF\">~\/<\/span><span style=\"color: #EEFFFF\">.ssh<\/span><span style=\"color: #89DDFF\">\/<\/span><span style=\"color: #EEFFFF\">authorized_keys<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" style=\"margin-top:var(--wp--preset--spacing--40);margin-bottom:var(--wp--preset--spacing--40)\">How to Use It<\/h2>\n\n\n\n<p>Simply copy the awk command above and execute it directly in your terminal:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#EEFFFF;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#304047;color:#d5ffff\">Bash<\/span><span role=\"button\" tabindex=\"0\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>awk 'BEGIN { FS = \" \" } { if ($0 ~ \/^# (BEGIN|END) ANSIBLE MANAGED BLOCK\/) { print } else { if (NF >= 2) { key = $2; if (length(key) > 20) { $2 = substr(key, 1, 10) \"...\" substr(key, length(key) - 9, 10) } } print } }' ~\/.ssh\/authorized_keys<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme\" style=\"background-color: #263238\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #FFCB6B\">awk<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #89DDFF\">&#39;<\/span><span style=\"color: #C3E88D\">BEGIN { FS = &quot; &quot; } { if ($0 ~ \/^# (BEGIN|END) ANSIBLE MANAGED BLOCK\/) { print } else { if (NF &gt;= 2) { key = $2; if (length(key) &gt; 20) { $2 = substr(key, 1, 10) &quot;...&quot; substr(key, length(key) - 9, 10) } } print } }<\/span><span style=\"color: #89DDFF\">&#39;<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C3E88D\">~\/.ssh\/authorized_keys<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>An example of what the output might look like. Instead of:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#EEFFFF;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#304047;color:#d5ffff\">Bash<\/span><span role=\"button\" tabindex=\"0\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>ssh-rsa AAAABC12345abc VERY_LONG_KEY_HERE abc123abc123abc123abc123abc123abc123== usera@host\n# BEGIN ANSIBLE MANAGED BLOCK developers\nssh-rsa AAAABC78901xyz VERY_LONG_KEY_HERE xyz789xyz789xyz789xyz789xyz789xyz789== userb@host\n# END ANSIBLE MANAGED BLOCK developers<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme\" style=\"background-color: #263238\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #FFCB6B\">ssh-rsa<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C3E88D\">AAAABC12345abc<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C3E88D\">VERY_LONG_KEY_HERE<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C3E88D\">abc123abc123abc123abc123abc123abc123==<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C3E88D\">usera@host<\/span><\/span>\n<span class=\"line\"><span style=\"color: #546E7A; font-style: italic\"># BEGIN ANSIBLE MANAGED BLOCK developers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #FFCB6B\">ssh-rsa<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C3E88D\">AAAABC78901xyz<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C3E88D\">VERY_LONG_KEY_HERE<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C3E88D\">xyz789xyz789xyz789xyz789xyz789xyz789==<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C3E88D\">userb@host<\/span><\/span>\n<span class=\"line\"><span style=\"color: #546E7A; font-style: italic\"># END ANSIBLE MANAGED BLOCK developers<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>You&#8217;ll then see:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#EEFFFF;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#304047;color:#d5ffff\">Bash<\/span><span role=\"button\" tabindex=\"0\" style=\"color:#EEFFFF;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>ssh-rsa AAAABC123\u202623abc123== usera@host\n# BEGIN ANSIBLE MANAGED BLOCK developers\nssh-rsa AAAABC789\u202689xyz789== userb@host\n# END ANSIBLE MANAGED BLOCK developers<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki material-theme\" style=\"background-color: #263238\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #FFCB6B\">ssh-rsa<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C3E88D\">AAAABC123\u202623abc123==<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C3E88D\">usera@host<\/span><\/span>\n<span class=\"line\"><span style=\"color: #546E7A; font-style: italic\"># BEGIN ANSIBLE MANAGED BLOCK developers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #FFCB6B\">ssh-rsa<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C3E88D\">AAAABC789\u202689xyz789==<\/span><span style=\"color: #EEFFFF\"> <\/span><span style=\"color: #C3E88D\">userb@host<\/span><\/span>\n<span class=\"line\"><span style=\"color: #546E7A; font-style: italic\"># END ANSIBLE MANAGED BLOCK developers<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" style=\"margin-top:var(--wp--preset--spacing--40);margin-bottom:var(--wp--preset--spacing--40)\">A Quick Explanation for the Curious<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>BEGIN { FS = \" \" }<\/code>: Defines the space as the field separator. awk splits each line into &#8220;fields&#8221; (columns) based on this separator.<\/li>\n\n\n\n<li><code>if ($0 ~ \/^# (BEGIN|END) ANSIBLE MANAGED BLOCK\/)<\/code>: This is the crucial part, when using ansible blocks!\n<ul class=\"wp-block-list\">\n<li><code>$0<\/code> refers to the entire current line.<\/li>\n\n\n\n<li><code>~<\/code> is the match operator.<\/li>\n\n\n\n<li><code>^# (BEGIN|END) ANSIBLE MANAGED BLOCK<\/code> is a regular expression that looks for lines starting with <code># BEGIN ANSIBLE MANAGED BLOCK<\/code> or <code># END ANSIBLE MANAGED BLOCK<\/code>. These lines usually have a marker at the end. This marker is ignored.<\/li>\n\n\n\n<li>If a line matches this pattern, print is executed immediately, printing the line as is, and awk moves to the next line.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>else:\n<ul class=\"wp-block-list\">\n<li><code>if (NF >= 2)<\/code>: Ensures that the line has at least two fields, so we can actually find a key to shorten.<\/li>\n\n\n\n<li><code>key = $2<\/code>: Stores the content of the second field (our SSH key) in the key variable.<\/li>\n\n\n\n<li><code>if (length(key) > 20)<\/code>: Checks if the key is long enough to be shortened (10 characters start + 10 characters end = 20).<\/li>\n\n\n\n<li><code>$2 = substr(key, 1, 10) \"\u2026\" substr(key, length(key) - 9, 10)<\/code>: This is the core of the shortening. substr extracts parts of the string. Here, we take the first 10 characters, add &#8220;\u2026&#8221;, and then the last 10 characters.<\/li>\n\n\n\n<li><code>print<\/code>: Outputs the entire, now potentially adjusted, line.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Give it a try and make your authorized_keys files clear and manageable again! I hope this little trick helps you in your daily admin tasks.<\/p>\n\n\n\n<p>Happy Administering!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello fellow Admins! Who hasn&#8217;t been there? The ~\/.ssh\/authorized_keys file is a crucial component for the security of your servers. But sometimes, especially when many keys are stored or very long keys are in use, the file can quickly become overwhelming. Getting a quick overview without endlessly scrolling through lengthy character strings is truly golden. That&#8217;s precisely why I have a small but powerful tip for you: an awk script that makes your authorized_keys more readable by shortening the SSH keys to a manageable length, without losing any vital information. Why is this useful? The awk Magic (Ansible-Aware!) This awk script shortens the actual SSH keys (the second column) to the first and last 10 characters, supplemented by three dots in the middle. This way, you know the key has been truncated, but you still have enough identifying features. All other columns (such as command=&#8221;\u2026&#8221; or no-port-forwarding) naturally remain untouched. \u24d8 All options are detailed in the sshd(8) man page (section AUTHORIZED_KEYS FILE FORMAT). Crucially, it checks for Ansible block markers and prints those lines exactly as they are, without any modification. How to Use It Simply copy the awk command above and execute it directly in your terminal: An example of what the output might look like. Instead of: You&#8217;ll then see: A Quick Explanation for the Curious Give it a try and make your authorized_keys files clear and manageable again! I hope this little trick helps you in your daily admin tasks. Happy Administering!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,37,17],"tags":[40,42,88],"class_list":["post-1133","post","type-post","status-publish","format-standard","hentry","category-ansible","category-korn-shell","category-security","tag-ansible","tag-awk","tag-shell"],"_links":{"self":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/1133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1133"}],"version-history":[{"count":7,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/1133\/revisions"}],"predecessor-version":[{"id":1140,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=\/wp\/v2\/posts\/1133\/revisions\/1140"}],"wp:attachment":[{"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techno.slomka.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}