{"id":1819,"date":"2026-11-23T08:07:39","date_gmt":"2026-11-23T08:07:39","guid":{"rendered":"https:\/\/virtualserversvps.com\/blog\/?p=1819"},"modified":"2026-11-23T08:07:39","modified_gmt":"2026-11-23T08:07:39","slug":"apparmor-profiles-vps-service-confinement","status":"publish","type":"post","link":"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/","title":{"rendered":"Confining Services on a VPS with AppArmor Profiles: What Blocks What"},"content":{"rendered":"<div style=\"padding:20px\">\n<p>A service on a VPS already runs as an unprivileged user, so what does AppArmor add? Plenty, because most compromises do not involve the service&#8217;s own files. They involve the vulnerable service reading an unexpected path, exec&#8217;ing a shell, opening a raw socket, or writing to a directory it was never meant to touch. AppArmor confines each process to a whitelist of paths, capabilities, and network operations, and the kernel refuses anything outside it \u2014 even if the process has the right Unix permissions.<\/p>\n<p>This article covers what actually blocks things: default nginx and sshd confinement, reading a denial, and writing and testing a profile for your own service without locking yourself out.<\/p>\n<h2>How enforcement works and where profiles live<\/h2>\n<p>AppArmor is enabled on every Ubuntu VPS by default. <code>sudo aa-status<\/code> prints loaded profiles, enforce or complain mode, and confined processes. Profiles live in <code>\/etc\/apparmor.d\/<\/code> and are compiled into the kernel at boot by <code>apparmor.service<\/code>.<\/p>\n<p>Second, AppArmor matches on paths, not inodes: a file moved into an allowed directory becomes accessible at once. That is why profiles are best written after the service is running, using its real paths, and tested with logging on.<\/p>\n<h2>What the stock nginx profile blocks<\/h2>\n<p>The <code>nginx<\/code> profile shipped in <code>\/etc\/apparmor.d\/nginx<\/code> allows reads in <code>\/etc\/nginx\/**<\/code>, <code>\/usr\/share\/nginx\/**<\/code>, and <code>\/var\/www\/html\/**<\/code>, access to its own PID file and logs, and the network operations nginx needs. It denies nearly everything else. Suppose a PHP upload trick gets an attacker a file in <code>\/var\/www\/html\/uploads\/<\/code>; even if they can execute it, AppArmor denies <code>\/bin\/sh<\/code> child execution and denies reads of <code>\/etc\/passwd<\/code> and <code>\/etc\/shadow<\/code>.<\/p>\n<p>To see a denial, run <code>sudo aa-exec -p nginx -- \/bin\/cat \/etc\/shadow<\/code>. It fails with permission denied, and <code>dmesg | tail<\/code> shows <code>apparmor=\"DENIED\" operation=\"open\" profile=\"nginx\" name=\"\/etc\/shadow\" ... denied_mask=\"r\"<\/code> \u2014 path, operation, and profile, the three fields you need for every fix.<\/p>\n<h2>What the stock sshd profile blocks<\/h2>\n<p>Ubuntu&#8217;s <code>sshd<\/code> profile confines <code>\/usr\/sbin\/sshd<\/code> to reading its configuration, host keys, PAM modules, and nsswitch, and denies shell execution from the daemon itself. Sessions transition to unconfined or to the user&#8217;s profile. The daemon cannot be tricked into running a payload, while the sessions it spawns remain ordinary user shells.<\/p>\n<p>If you move the config path, the profile may need an extra rule. The symptom is <code>sshd<\/code> failing to start with a denial for <code>\/etc\/ssh\/sshd_config.d\/<\/code>; the fix is a one-line <code>\/etc\/ssh\/sshd_config.d\/** r,<\/code>, not disabling AppArmor.<\/p>\n<h2>Reading denial logs and turning them into rules<\/h2>\n<p>The kernel sends AppArmor events to the audit subsystem. <code>journalctl -k | grep apparmor=\"DENIED\"<\/code>, or <code>dmesg<\/code> without auditd. Each line carries <code>profile<\/code>, <code>operation<\/code>, <code>name<\/code>, and <code>denied_mask<\/code>.<\/p>\n<p><code>aa-logprof<\/code> turns those into profile text: it walks the log, proposes a rule per denial, and lets you accept or reject each. Run it in complain mode so you see the full set of denials a normal workload produces, not just the first one that killed the service.<\/p>\n<ol>\n<li><code>sudo aa-complain \/etc\/apparmor.d\/usr.sbin.myapp<\/code> \u2014 log-only mode<\/li>\n<li><code>sudo aa-logprof<\/code> \u2014 accept the rules you expect and reject the rest<\/li>\n<li><code>sudo apparmor_parser -r \/etc\/apparmor.d\/usr.sbin.myapp<\/code> \u2014 reload<\/li>\n<li><code>sudo aa-enforce \/etc\/apparmor.d\/usr.sbin.myapp<\/code> \u2014 turn enforcement back on<\/li>\n<\/ol>\n<h2>Writing a minimal profile for a custom service<\/h2>\n<p>Start from <code>aa-genprof \/opt\/myapp\/bin\/myapp<\/code>. It writes a stripped-down profile to <code>\/etc\/apparmor.d\/<\/code>, puts it in complain mode, and asks you to run the service through a typical workload. Accept the <code>abstractions\/base<\/code> include for libc, locales, and DNS, and add only the paths your service truly needs.<\/p>\n<p>A profile for a small Go binary behind nginx allows the binary and its data directory, denies <code>\/home<\/code> and <code>ptrace<\/code>, and allows only <code>inet stream<\/code> and <code>inet6 stream<\/code> networking. To bind port 80, add one <code>capability net_bind_service,<\/code> line instead of running as root.<\/p>\n<h2>Testing that denials happen where you expect<\/h2>\n<p>A profile that loads is not a profile that works. Test both directions: the service must succeed at its real job and fail at everything else. The second half is the part people skip.<\/p>\n<table>\n<thead>\n<tr>\n<th>Test<\/th>\n<th>Command<\/th>\n<th>Expected result<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Read its own config<\/td>\n<td><code>aa-exec -p myapp -- cat \/etc\/myapp\/config.yml<\/code><\/td>\n<td>succeeds<\/td>\n<\/tr>\n<tr>\n<td>Read a shadow file<\/td>\n<td><code>aa-exec -p myapp -- cat \/etc\/shadow<\/code><\/td>\n<td>DENIED<\/td>\n<\/tr>\n<tr>\n<td>Spawn a shell<\/td>\n<td><code>aa-exec -p myapp -- \/bin\/sh<\/code><\/td>\n<td>DENIED<\/td>\n<\/tr>\n<tr>\n<td>Write outside its data dir<\/td>\n<td><code>aa-exec -p myapp -- touch \/tmp\/escape<\/code><\/td>\n<td>DENIED<\/td>\n<\/tr>\n<tr>\n<td>Raw packet socket<\/td>\n<td><code>aa-exec -p myapp -- ping -c1 127.0.0.1<\/code><\/td>\n<td>DENIED<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Every DENIED above is a real boundary. If one succeeds, the profile is too loose \u2014 usually a broad <code>\/etc\/** r,<\/code> or <code>\/tmp\/** rw,<\/code> accepted during <code>aa-logprof<\/code>.<\/p>\n<h2>Avoiding the two classic mistakes<\/h2>\n<p>The first mistake is enforcing a profile never tested in complain mode. A service that silently fails to bind a socket looks like an application bug for hours while the audit log shows the reason in one line. Run complain mode for a full traffic cycle, including certificate reloads, before enforcing.<\/p>\n<p>The second is path drift. An upgrade can move a binary from <code>\/usr\/sbin\/foo<\/code> to <code>\/usr\/libexec\/foo<\/code>; the old profile no longer attaches and the binary runs unconfined. Check <code>aa-status<\/code> after upgrades and add the new path to the profile&#8217;s attachment line.<\/p>\n<p>Remember too that AppArmor cannot deny what the process never attempts: it blocks exploitation paths, not vulnerabilities. Pair it with a narrow systemd unit \u2014 <code>ProtectSystem=strict<\/code>, <code>PrivateTmp=yes<\/code>, <code>NoNewPrivileges=yes<\/code>. Before building a strategy on it, check <a href=\"https:\/\/virtualserversvps.com\/\">what an instance can promise<\/a>.<\/p>\n<p>Finally, if the service runs containers, note that AppArmor applies to the container process through the runtime&#8217;s profile. Docker&#8217;s <code>docker-default<\/code> is a starting point, not a confinement strategy; the same <code>aa-logprof<\/code> loop works on a custom profile, as long as the container runtime is configured to load it. On a modest VPS the overhead is negligible \u2014 profile matching is a few hundred nanoseconds per syscall.<\/p>\n<p>AppArmor is a kernel feature, so on a VPS you rely on the provider exposing securityfs and a kernel built with it enabled. Most KVM hosts do; some containers do not, and <code>aa-status<\/code> then reports no profiles loaded.<\/p>\n<p>If you need a specific kernel version or LSM stacking with both AppArmor and eBPF confinement, a <a href=\"https:\/\/virtualserversvps.com\/vps-vs-dedicated-server\">dedicated server gives you the kernel you install yourself<\/a>. On a VPS, verify first, then write the profile.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A service on a VPS already runs as an unprivileged user, so what does AppArmor add? Plenty, because most compromises do not involve the service&#8217;s own files. They involve the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1819","post","type-post","status-publish","format-standard","hentry","category-vps-guides-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.1 (Yoast SEO v26.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Confining Services on a VPS with AppArmor Profiles: What Blocks What - Virtual Servers VPS Blog<\/title>\n<meta name=\"description\" content=\"A service on a VPS already runs as an unprivileged user, so what does AppArmor add? Plenty, because most compromises do not involve the service&#039;s own\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Confining Services on a VPS with AppArmor Profiles: What Blocks What\" \/>\n<meta property=\"og:description\" content=\"Confining Services on a VPS with AppArmor Profiles: What Blocks What\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual Servers VPS Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-23T08:07:39+00:00\" \/>\n<meta name=\"author\" content=\"Virtual-Servers-Vps-Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virtual-Servers-Vps-Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/\",\"name\":\"Confining Services on a VPS with AppArmor Profiles: What Blocks What - Virtual Servers VPS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\"},\"datePublished\":\"2026-11-23T08:07:39+00:00\",\"author\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\"},\"description\":\"A service on a VPS already runs as an unprivileged user, so what does AppArmor add? Plenty, because most compromises do not involve the service's own\",\"breadcrumb\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtualserversvps.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Confining Services on a VPS with AppArmor Profiles: What Blocks What\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/\",\"name\":\"Virtual Servers VPS Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\",\"name\":\"Virtual-Servers-Vps-Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"caption\":\"Virtual-Servers-Vps-Editor\"},\"sameAs\":[\"https:\/\/virtualserversvps.com\/blog\"],\"url\":\"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Confining Services on a VPS with AppArmor Profiles: What Blocks What - Virtual Servers VPS Blog","description":"A service on a VPS already runs as an unprivileged user, so what does AppArmor add? Plenty, because most compromises do not involve the service's own","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/","og_locale":"en_US","og_type":"article","og_title":"Confining Services on a VPS with AppArmor Profiles: What Blocks What","og_description":"Confining Services on a VPS with AppArmor Profiles: What Blocks What","og_url":"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/","og_site_name":"Virtual Servers VPS Blog","article_published_time":"2026-11-23T08:07:39+00:00","author":"Virtual-Servers-Vps-Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Virtual-Servers-Vps-Editor","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/","url":"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/","name":"Confining Services on a VPS with AppArmor Profiles: What Blocks What - Virtual Servers VPS Blog","isPartOf":{"@id":"https:\/\/virtualserversvps.com\/blog\/#website"},"datePublished":"2026-11-23T08:07:39+00:00","author":{"@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0"},"description":"A service on a VPS already runs as an unprivileged user, so what does AppArmor add? Plenty, because most compromises do not involve the service's own","breadcrumb":{"@id":"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/virtualserversvps.com\/blog\/apparmor-profiles-vps-service-confinement\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtualserversvps.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Confining Services on a VPS with AppArmor Profiles: What Blocks What"}]},{"@type":"WebSite","@id":"https:\/\/virtualserversvps.com\/blog\/#website","url":"https:\/\/virtualserversvps.com\/blog\/","name":"Virtual Servers VPS Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0","name":"Virtual-Servers-Vps-Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","caption":"Virtual-Servers-Vps-Editor"},"sameAs":["https:\/\/virtualserversvps.com\/blog"],"url":"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/"}]}},"_links":{"self":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/comments?post=1819"}],"version-history":[{"count":1,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1819\/revisions"}],"predecessor-version":[{"id":1820,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1819\/revisions\/1820"}],"wp:attachment":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/media?parent=1819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/categories?post=1819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/tags?post=1819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}