<p>I have my ssh private key (/home/user/.ssh/id_rsa) as a volume mounted secret in my container. Kubernetes seems to mount it with uid 0. However, my app runs as a specific user, and therefore can't access the ssh private key whose permission must be 600 at min. How can I change the ownership of my private key to reflect that of a specific user?</p>

<p>thanks.</p>

sebastian
<p>In Linux, usernames are mapped to a user id which can be seen with the command <code>id -u someusername</code>.</p>

<p>SSH requires by default in many cases that your SSH key be owned by the user running SSH and be hidden to others <code>600</code></p>

<p>Therefore, I highly recommend you copy your key instead of mounting it, unless your container user has the same user id as you.</p>

<hr>

<p>If you are using a linux container, you can run the command inside the container to get the exact user id, and then chown your files with the user id instead of a user name. </p>

<p><code>kubectl exec -it mypod bash</code> or <code>sh</code> if bash doesn't work
<code>$ id -u someuser</code></p>

<p>OR</p>

<p><code>kubectl exec -it mypod id -u</code> if your container has one user which started the main process</p>

<p>THEN </p>

<p>Copy your id file so you can chown it without interfering with your ability to ssh.</p>

<pre><code>mkdir -p /data/secrets/myapp
cp /home/user/.ssh/id_rsa /data/secrets/myapp/id_rsa
chown $MYAPPUSERID:$MYAPPUSERID /data/secrets/myapp/id_rsa
chmod 600 /data/secrets/myapp/id_rsa
</code></pre>

<p>Because the host OS might have already mapped this user id, it may <em>seem</em> that your files are owned by another arbitrary user, but what ultimately matters is the user id of the owner/group.</p>
