<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Sudo on blog.iankulin.com</title><link>https://blog.iankulin.com/tags/sudo/</link><description>Recent content in Sudo on blog.iankulin.com</description><generator>Hugo</generator><language>en-AU</language><lastBuildDate>Fri, 10 Feb 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://blog.iankulin.com/tags/sudo/index.xml" rel="self" type="application/rss+xml"/><item><title>Saved by the qemu_guest_agent</title><link>https://blog.iankulin.com/saved-by-the-qemu_guest_agent/</link><pubDate>Fri, 10 Feb 2023 00:00:00 +0000</pubDate><guid>https://blog.iankulin.com/saved-by-the-qemu_guest_agent/</guid><description>&lt;p&gt;Literally an hour after I wrote the post &lt;a href="https://blog.iankulin.com/proxmox-qemu-guest-agent/"&gt;about installing the qemu guest agent&lt;/a&gt; in a VM and explaining how it can be used to inject root level commands into a VM, I had use of it due to a mistake.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;d decided to add myself to the sudoers file. Since the last line in that file is a directive to include all the files in the /etc/sudoers.d directory, the accepted way to do that for local changes is to create a file in that directory with the necessary commands.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;# User privilege specification
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;root	ALL=(ALL:ALL) ALL
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;# Members of the admin group may gain root privileges
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;%admin ALL=(ALL) ALL
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;# Allow members of group sudo to execute any command
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;%sudo	ALL=(ALL:ALL) ALL
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;# See sudoers(5) for more information on &amp;#34;@include&amp;#34; directives:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;@includedir /etc/sudoers.d
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The format of this command is important to get right, since if you stuff it up, sudo will not work, and I don&amp;rsquo;t even have a root login for this server, so then I&amp;rsquo;d be in a pickle. It&amp;rsquo;s so important to not stuff this up that there is a special command for editing the files that won&amp;rsquo;t let you save them if you&amp;rsquo;ve made a mistake.&lt;/p&gt;
&lt;p&gt;Out of an abundance of caution, I decided to copy the system sudoers file to the directory as a starting point since it would have the correct format and be easy to edit. It didn&amp;rsquo;t occur to me that then the &lt;code&gt;@includedir&lt;/code&gt; at the end would become an infinite loop.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.iankulin.com/images/screen-shot-2023-01-29-at-2.06.12-pm.png" alt=""&gt;&lt;/p&gt;
&lt;p&gt;So here I am, logged in as ian, with no sudo, needing to edit or delete a protected file, and with no root login. Luckily, it&amp;rsquo;s a VM running the qemu user agent, so I can access it from Proxmox.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://blog.iankulin.com/images/screen-shot-2023-01-29-at-2.04.37-pm.png"&gt;&lt;img src="https://blog.iankulin.com/images/screen-shot-2023-01-29-at-2.04.37-pm.png" width="895" alt=""&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Saved by over-engineering! Thank you open source contributors.&lt;/p&gt;</description></item><item><title>sudo Incident Reports - where do they go?</title><link>https://blog.iankulin.com/sudo-incident-reports-where-do-they-go/</link><pubDate>Sat, 04 Feb 2023 00:00:00 +0000</pubDate><guid>https://blog.iankulin.com/sudo-incident-reports-where-do-they-go/</guid><description>&lt;p&gt;Even though it&amp;rsquo;s &lt;em&gt;my&lt;/em&gt; server, I still have a pang of guilt when this happens.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.iankulin.com/images/screen-shot-2023-01-28-at-10.40.43-am-copy.png" alt=""&gt;&lt;/p&gt;
&lt;p&gt;I always imagine &lt;a href="https://en.wikipedia.org/wiki/Richard_Stallman"&gt;Richard Stallman&lt;/a&gt; (or someone with a similar 2000&amp;rsquo;s database administrator beard) looking at me disappointedly and shaking his head slowly.&lt;/p&gt;
&lt;p&gt;It does raise the question though - since it&amp;rsquo;s my server, shouldn&amp;rsquo;t I be getting a text message from CERN or something?&lt;/p&gt;
&lt;h4 id="where-is-this-report"&gt;Where is this report?&lt;/h4&gt;
&lt;p&gt;(&lt;a href="https://xkcd.com/838/"&gt;Relevant xkcd&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;Like everything, the answer is &amp;lsquo;it&amp;rsquo;s logged&amp;rsquo;. We can use the &lt;code&gt;journalctl&lt;/code&gt; command to look at the logs, on this server that&amp;rsquo;s been running less than 20 hours, there&amp;rsquo;s already several thousand lines to look through if you just enter &lt;code&gt;journalctl&lt;/code&gt;, so I&amp;rsquo;m going to just send all the high priority logs to a file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;journalctl -p 3 &amp;gt; errors.txt
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then since this just happened, it should be at the end of the file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;tail errors.txt
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Jan 28 12:10:40 enrico-rider sshd[5168]: fatal: Timeout before authentication for 110.41.153.190 port 41826
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Jan 28 12:11:01 enrico-rider sshd[5170]: fatal: Timeout before authentication for 110.41.153.190 port 41856
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Jan 28 12:23:15 enrico-rider sshd[5222]: fatal: Timeout before authentication for 61.177.173.39 port 29421
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Jan 28 12:23:26 enrico-rider sshd[5223]: fatal: Timeout before authentication for 61.177.173.39 port 49692
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Jan 28 12:23:37 enrico-rider sshd[5226]: fatal: Timeout before authentication for 61.177.173.39 port 10416
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Jan 28 12:39:51 enrico-rider sshd[5517]: fatal: Timeout before authentication for 61.177.172.108 port 53867
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Jan 28 12:50:06 enrico-rider sshd[5653]: error: kex_exchange_identification: Connection closed by remote host
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Jan 28 13:03:53 enrico-rider sshd[5696]: error: kex_exchange_identification: Connection closed by remote host
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Jan 28 13:24:58 enrico-rider sshd[5804]: fatal: Timeout before authentication for 61.177.173.39 port 46041
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Jan 28 13:40:06 enrico-rider sudo[6077]: ian : user NOT in sudoers ; TTY=pts/0 ; PWD=/home/ian ; USER=root ; COMMAND=/usr/bin/docker ps
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There we go, it really has been reported!&lt;/p&gt;
&lt;h4 id="how-to-add-a-user-to-the-sudoers-file"&gt;How to add a user to the sudoers file&lt;/h4&gt;
&lt;p&gt;To avoid these terrible reports, it sounds like I need to add myself to the &amp;lsquo;sudoers file&amp;rsquo;. I won&amp;rsquo;t be able to do that as myself, so I&amp;rsquo;ll log back in as &lt;code&gt;root&lt;/code&gt; for a bit. The reason I don&amp;rsquo;t just operate as &lt;code&gt;root&lt;/code&gt; all the time is that I quite like the constant reminder that I&amp;rsquo;m about to do something administratory - so I should have a second thought before I sudo that shell command I just copied out of a stackoverflow answer.&lt;/p&gt;
&lt;p&gt;Since the error message says I&amp;rsquo;m not in the sudoers file, I should just add my name right? Well yes, and no. That is possible, but it&amp;rsquo;s slightly dangerous - it has a specific format, and if you stuff things up bad things can happen. For this reason there&amp;rsquo;s a special command to edit it (visudo) which refuses to save it if you make a mistake.&lt;/p&gt;
&lt;p&gt;The sudoers file is at &lt;code&gt;/etc/sudoers&lt;/code&gt;, if you &lt;code&gt;cat&lt;/code&gt; it, it has a heap of commented out stuff, but there&amp;rsquo;s be a section that looks like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;# User privilege specification
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;root	ALL=(ALL:ALL) ALL
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;# Members of the admin group may gain root privileges
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;%admin ALL=(ALL) ALL
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;# Allow members of group sudo to execute any command
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;%sudo	ALL=(ALL:ALL) ALL
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;# See sudoers(5) for more information on &amp;#34;@include&amp;#34; directives:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;@includedir /etc/sudoers.d
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That last line includes any files in the &lt;code&gt;/ect/sudoers.d&lt;/code&gt; as part of this one, so if we really did want to add &lt;code&gt;ian&lt;/code&gt; to this file, we&amp;rsquo;d do it there, but still by using the &lt;code&gt;visudo&lt;/code&gt; command to do it safely.&lt;/p&gt;
&lt;p&gt;But, we don&amp;rsquo;t need to. The &lt;code&gt;%admin&lt;/code&gt; and &lt;code&gt;%sudo&lt;/code&gt; lines are granting these permissions to groups, so all we need to do is add &lt;code&gt;ian&lt;/code&gt; to the &lt;code&gt;sudo&lt;/code&gt; group and those permissions will be granted, safely.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;usermod -a -G sudo ian
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Success:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-zed" data-lang="zed"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ian&lt;span style="color:#bf616a"&gt;@&lt;/span&gt;enrico&lt;span style="color:#81a1c1"&gt;-&lt;/span&gt;rider&lt;span style="color:#81a1c1"&gt;:~&lt;/span&gt;&lt;span style="color:#bf616a"&gt;$&lt;/span&gt; docker ps
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Got &lt;span style="color:#81a1c1;font-weight:bold"&gt;permission&lt;/span&gt; denied while trying to connect to the Docker daemon socket at unix&lt;span style="color:#81a1c1"&gt;:&lt;/span&gt;&lt;span style="color:#616e87;font-style:italic"&gt;///var/run/docker.sock: Get &amp;#34;http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json&amp;#34;: dial unix /var/run/docker.sock: connect: permission denied
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ian&lt;span style="color:#bf616a"&gt;@&lt;/span&gt;enrico&lt;span style="color:#81a1c1"&gt;-&lt;/span&gt;rider&lt;span style="color:#81a1c1"&gt;:~&lt;/span&gt;&lt;span style="color:#bf616a"&gt;$&lt;/span&gt; sudo docker ps
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#eceff4"&gt;[&lt;/span&gt;sudo&lt;span style="color:#eceff4"&gt;]&lt;/span&gt; password for ian&lt;span style="color:#81a1c1"&gt;:&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#bf616a"&gt;520&lt;/span&gt;ed656ef12 &lt;span style="color:#8fbcbb"&gt;dockersamples/&lt;/span&gt;&lt;span style="color:#bf616a"&gt;101&lt;/span&gt;&lt;span style="color:#81a1c1"&gt;-&lt;/span&gt;tutorial &lt;span style="color:#bf616a"&gt;&amp;#34;&lt;/span&gt;nginx &lt;span style="color:#81a1c1"&gt;-&lt;/span&gt;g &lt;span style="color:#bf616a"&gt;&amp;#39;&lt;/span&gt;daemon of&lt;span style="color:#bf616a"&gt;…&amp;#34;&lt;/span&gt; &lt;span style="color:#bf616a"&gt;14&lt;/span&gt; hours ago Up &lt;span style="color:#bf616a"&gt;14&lt;/span&gt; hours &lt;span style="color:#bf616a"&gt;0&lt;/span&gt;&lt;span style="color:#eceff4"&gt;.&lt;/span&gt;&lt;span style="color:#bf616a"&gt;0&lt;/span&gt;&lt;span style="color:#eceff4"&gt;.&lt;/span&gt;&lt;span style="color:#bf616a"&gt;0&lt;/span&gt;&lt;span style="color:#eceff4"&gt;.&lt;/span&gt;&lt;span style="color:#bf616a"&gt;0&lt;/span&gt;&lt;span style="color:#81a1c1"&gt;:&lt;/span&gt;&lt;span style="color:#bf616a"&gt;80&lt;/span&gt;&lt;span style="color:#81a1c1"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#bf616a"&gt;80&lt;/span&gt;&lt;span style="color:#81a1c1"&gt;/&lt;/span&gt;tcp&lt;span style="color:#eceff4"&gt;,&lt;/span&gt; &lt;span style="color:#81a1c1"&gt;:::&lt;/span&gt;&lt;span style="color:#bf616a"&gt;80&lt;/span&gt;&lt;span style="color:#81a1c1"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color:#bf616a"&gt;80&lt;/span&gt;&lt;span style="color:#81a1c1"&gt;/&lt;/span&gt;tcp pedantic_bartik
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ian&lt;span style="color:#bf616a"&gt;@&lt;/span&gt;enrico&lt;span style="color:#81a1c1"&gt;-&lt;/span&gt;rider&lt;span style="color:#81a1c1"&gt;:~&lt;/span&gt;&lt;span style="color:#bf616a"&gt;$&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item></channel></rss>