<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.nixos.org/w/index.php?action=history&amp;feed=atom&amp;title=Systemd%2FHardening%2Fen</id>
	<title>Systemd/Hardening/en - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.nixos.org/w/index.php?action=history&amp;feed=atom&amp;title=Systemd%2FHardening%2Fen"/>
	<link rel="alternate" type="text/html" href="https://wiki.nixos.org/w/index.php?title=Systemd/Hardening/en&amp;action=history"/>
	<updated>2026-04-07T05:31:06Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.0</generator>
	<entry>
		<id>https://wiki.nixos.org/w/index.php?title=Systemd/Hardening/en&amp;diff=27108&amp;oldid=prev</id>
		<title>FuzzyBot: Updating to match new version of source page</title>
		<link rel="alternate" type="text/html" href="https://wiki.nixos.org/w/index.php?title=Systemd/Hardening/en&amp;diff=27108&amp;oldid=prev"/>
		<updated>2025-10-07T21:26:13Z</updated>

		<summary type="html">&lt;p&gt;Updating to match new version of source page&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 21:26, 7 October 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l7&quot;&gt;Line 7:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 7:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;A more granular way, would be to put these 3 paths into &amp;lt;code&amp;gt;BindReadOnlyPaths&amp;lt;/code&amp;gt;, and wait for the creation of &amp;lt;code&amp;gt;/etc/resolv.conf&amp;lt;/code&amp;gt; through a &amp;lt;code&amp;gt;systemd.path&amp;lt;/code&amp;gt; unit.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;A more granular way, would be to put these 3 paths into &amp;lt;code&amp;gt;BindReadOnlyPaths&amp;lt;/code&amp;gt;, and wait for the creation of &amp;lt;code&amp;gt;/etc/resolv.conf&amp;lt;/code&amp;gt; through a &amp;lt;code&amp;gt;systemd.path&amp;lt;/code&amp;gt; unit.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Dropping a shell inside a systemd service ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Dropping a shell inside a systemd service ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;While hardening a service, it often happens that you want a shell inside a hardened systemd unit, for &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;exemple &lt;/del&gt;to check access to files, or check the network connectivity. One way to do this is to use tmux to create a session inside the service, and attaching to it outside of the service.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;While hardening a service, it often happens that you want a shell inside a hardened systemd unit, for &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;example &lt;/ins&gt;to check access to files, or check the network connectivity. One way to do this is to use tmux to create a session inside the service, and attaching to it outside of the service.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Simple example:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Simple example:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;nix&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;nix&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key mediawiki:diff:1.41:old-16509:rev-27108:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>FuzzyBot</name></author>
	</entry>
	<entry>
		<id>https://wiki.nixos.org/w/index.php?title=Systemd/Hardening/en&amp;diff=16509&amp;oldid=prev</id>
		<title>FuzzyBot: Updating to match new version of source page</title>
		<link rel="alternate" type="text/html" href="https://wiki.nixos.org/w/index.php?title=Systemd/Hardening/en&amp;diff=16509&amp;oldid=prev"/>
		<updated>2024-08-08T08:36:58Z</updated>

		<summary type="html">&lt;p&gt;Updating to match new version of source page&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Systemd/breadcrumb}}&lt;br /&gt;
&lt;br /&gt;
Systemd&amp;#039;s service options are quite lax by default, and so it is often desirable to look at ways to harden systemd services.&lt;br /&gt;
A good way to get started on a given service is to look at the output of the command &amp;lt;code&amp;gt;systemd-analyze security myService&amp;lt;/code&amp;gt;. From there, you can look at the documentation for the options you see in the output, often in &amp;lt;code&amp;gt;man systemd.exec&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;man systemd.resource-control&amp;lt;/code&amp;gt;, and set the appropriate options for your service.&lt;br /&gt;
== Accessing the network with a different RootDirectory ==&lt;br /&gt;
To be able to access the network while having a RootDirectory specified, you need to give access to &amp;lt;code&amp;gt;/etc/ssl&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;/etc/static/ssl&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;/etc/resolv.conf&amp;lt;/code&amp;gt;. The simplest way of doing this is by simply putting &amp;lt;code&amp;gt;/etc&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;BindReadOnlyPaths&amp;lt;/code&amp;gt; option.&lt;br /&gt;
A more granular way, would be to put these 3 paths into &amp;lt;code&amp;gt;BindReadOnlyPaths&amp;lt;/code&amp;gt;, and wait for the creation of &amp;lt;code&amp;gt;/etc/resolv.conf&amp;lt;/code&amp;gt; through a &amp;lt;code&amp;gt;systemd.path&amp;lt;/code&amp;gt; unit.&lt;br /&gt;
== Dropping a shell inside a systemd service ==&lt;br /&gt;
While hardening a service, it often happens that you want a shell inside a hardened systemd unit, for exemple to check access to files, or check the network connectivity. One way to do this is to use tmux to create a session inside the service, and attaching to it outside of the service.&lt;br /&gt;
Simple example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;nix&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{ pkgs, ... }:&lt;br /&gt;
{&lt;br /&gt;
  systemd.services.myService = {&lt;br /&gt;
    serviceConfig = {&lt;br /&gt;
      ExecStart = &amp;quot;${pkgs.tmux}/bin/tmux -S /tmp/tmux.socket new-session -s my-session -d&amp;quot;;&lt;br /&gt;
      ExecStop = &amp;quot;${pkgs.tmux}/bin/tmux -S /tmp/tmux.socket kill-session -t my-session&amp;quot;;&lt;br /&gt;
      Type = &amp;quot;forking&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
      # ...&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Example with a &amp;lt;code&amp;gt;RootDirectory&amp;lt;/code&amp;gt; specified:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;nix&amp;quot;&amp;gt;&lt;br /&gt;
{ pkgs }:&lt;br /&gt;
{&lt;br /&gt;
  systemd.services.myService = {&lt;br /&gt;
    serviceConfig = {&lt;br /&gt;
      ExecStart = &amp;quot;${pkgs.tmux}/bin/tmux -S /run/myService/tmux.socket new-session -s my-session -d&amp;quot;;&lt;br /&gt;
      ExecStop = &amp;quot;${pkgs.tmux}/bin/tmux -S /run/myService/tmux.socket kill-session -t my-session&amp;quot;;&lt;br /&gt;
      Type = &amp;quot;forking&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
      # Used as root directory&lt;br /&gt;
      RuntimeDirectory = &amp;quot;myService&amp;quot;;&lt;br /&gt;
      RootDirectory = &amp;quot;/run/myService&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
      BindReadOnlyPaths = [&lt;br /&gt;
        &amp;quot;/nix/store&amp;quot;&lt;br /&gt;
&lt;br /&gt;
        # So tmux uses /bin/sh as shell&lt;br /&gt;
        &amp;quot;/bin&amp;quot;&lt;br /&gt;
      ];&lt;br /&gt;
&lt;br /&gt;
      # This sets up a private /dev/tty&lt;br /&gt;
      # The tmux server would crash without this&lt;br /&gt;
      # since there would be nothing in /dev&lt;br /&gt;
      PrivateDevices = true;&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
To attach to the shell, simply execute &amp;lt;code&amp;gt;tmux -S /path/to/tmux.socket attach&amp;lt;/code&amp;gt;.&lt;br /&gt;
== Hardening examples ==&lt;br /&gt;
This list contains proposed hardening options that are not yet upstreamed. Please use with caution, and please notify the author of the change if something breaks:&lt;br /&gt;
* Chrony: https://github.com/NixOS/nixpkgs/pull/104944/files&lt;br /&gt;
* Isso: https://github.com/NixOS/nixpkgs/pull/140840/files&lt;br /&gt;
* Mautrix-based bridge: https://github.com/mautrix/docs/pull/18/files&lt;br /&gt;
* Postfix: https://github.com/NixOS/nixpkgs/pull/93305/files&lt;br /&gt;
* TheLounge: https://github.com/thelounge/thelounge-deb/pull/78&lt;br /&gt;
== Related links ==&lt;br /&gt;
* SHH, systemd hardening helper:  [https://www.synacktiv.com/en/publications/systemd-hardening-made-easy-with-shh systemd hardening made easy with SHH]&lt;br /&gt;
&lt;br /&gt;
[[Category:NixOS]]&lt;br /&gt;
[[Category:Cookbook]]&lt;br /&gt;
[[Category:Security]]&lt;br /&gt;
[[Category:systemd]]&lt;/div&gt;</summary>
		<author><name>FuzzyBot</name></author>
	</entry>
</feed>