<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>journalctl &#8211; Zhijun Chen</title>
	<atom:link href="https://zhijunchen.com/tag/journalctl/feed/" rel="self" type="application/rss+xml" />
	<link>https://zhijunchen.com</link>
	<description></description>
	<lastBuildDate>Fri, 07 Jun 2024 06:31:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.4</generator>

<image>
	<url>https://zhijunchen.com/wp-content/uploads/2021/03/cropped-my-site-icon-1-32x32.png</url>
	<title>journalctl &#8211; Zhijun Chen</title>
	<link>https://zhijunchen.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Introduction To Systemd</title>
		<link>https://zhijunchen.com/introduction-to-systemd/</link>
		
		<dc:creator><![CDATA[Zhijun Chen]]></dc:creator>
		<pubDate>Mon, 17 May 2021 12:03:59 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[journalctl]]></category>
		<category><![CDATA[systemctl]]></category>
		<category><![CDATA[systemd]]></category>
		<guid isPermaLink="false">https://zhijunchen.com/?p=161</guid>

					<description><![CDATA[I came across systemd while working with Ubuntu and other linux distributions on AWS. Its main aim is to unify service configuration and behaviour across Linux distributions and is used widely in software development. If you go through Kubernetes The Hard Way tutorial, you will find that all of the key services, e.g. Kubernetes api server, control manager, kubelet, etc. are managed using systemd. ]]></description>
										<content:encoded><![CDATA[
<p>I came across <a rel="noreferrer noopener" href="https://systemd.io/" target="_blank"><code>systemd</code></a> while working with Ubuntu and other linux distributions on AWS. Its main aim is to unify service configuration and behaviour across&nbsp;Linux distributions and is used widely in software development. If you go through <a rel="noreferrer noopener" href="https://github.com/kelseyhightower/kubernetes-the-hard-way" target="_blank">Kubernetes The Hard Way</a> tutorial, you will find that all of the key services, e.g. Kubernetes api server, control manager, kubelet, etc. are managed using <code>systemd</code>. </p>



<h2 class="wp-block-heading">Units</h2>



<p>The basic object in <code>systemd</code> is <code>Unit</code>. Units can be, for example, services (<em>.service</em>), mount points (<em>.mount</em>), devices (<em>.device</em>) or sockets (<em>.socket</em>). You can see the full list of unit types using the following man command.</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">man systemd.unit</code></pre>



<p>Custom unit files are typically put inside <code>/etc/systemd/system</code> folder, with the unit definition file named <code>unit_name.service</code>. Along with the unit file, a &#8220;drop-in&#8221; directory&nbsp;<code>unit_name.service.d/</code>&nbsp;may exist. All files with the suffix &#8220;<code>.conf</code>&#8221; from this directory will be parsed after the unit file itself is parsed. This is useful to alter or add configuration settings for a unit, without having to modify unit files.&nbsp;</p>



<p>A service unit file usually looks like the following:</p>



<pre class="wp-block-code"><code class="">[Unit]
Description=Kubernetes Kubelet
Documentation=https://github.com/kubernetes/kubernetes
After=docker.service
Requires=docker.service

[Service]
ExecStart=/usr/local/bin/kubelet \
  --bootstrap-kubeconfig="/var/lib/kubelet/bootstrap-kubeconfig" \
  --config=/var/lib/kubelet/kubelet-config.yaml \
  --image-pull-progress-deadline=2m \
  --kubeconfig=/var/lib/kubelet/kubeconfig \
  --cert-dir=/var/lib/kubelet/pki/ \
  --rotate-certificates=true \
  --network-plugin=cni \
  --register-node=true \
  --v=2
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target</code></pre>



<p>The internal structure of unit files are organized with sections. Sections are denoted by a pair of square brackets with the section name enclosed within. Each section extends until the beginning of the subsequent section or until the end of the file. In the example, Unit, Service and Install are sections and inside sections we have key value pairs defining different directives. </p>



<p>For details on directives of different unit types, you can use the following commands to view more details.</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">man systemd.unit # View [Unit] and [Install] directives
man systemd.service # View [Service] directives</code></pre>



<p>Note that unit dependencies can be defined in the Install section using <code>WantedBy</code> and <code>RequiredBy</code> directives. </p>



<h2 class="wp-block-heading">Targets</h2>



<p>Targets are basically synchronization points that the server can used to bring the server into a specific state. Service and other unit files can be tied to a target and multiple targets can be active at the same time. Target is used for grouping units and as well-known synchronization points during start-up. </p>



<figure class="wp-block-table is-style-regular"><table><thead><tr><td><strong>systemd targets</strong></td><td><strong>target aliases</strong></td><td><strong>Description</strong></td></tr></thead><tbody><tr><td>default.target</td><td>&nbsp;</td><td>This target is always aliased with a symbolic link to either&nbsp;<strong>multi-user.target</strong>&nbsp;or&nbsp;<strong>graphical.target</strong>. systemd always uses the&nbsp;<strong>default.target</strong>&nbsp;to start the system. The&nbsp;<strong>default.target</strong>&nbsp;should never be aliased to&nbsp;<strong>halt.target</strong>,&nbsp;<strong>poweroff.target</strong>, or&nbsp;<strong>reboot.target</strong>.</td></tr><tr><td>graphical.target</td><td>runlevel5.target</td><td><strong>Multi-user.target</strong>&nbsp;with a GUI</td></tr><tr><td>&nbsp;</td><td>runlevel4.target</td><td>Unused. Runlevel 4 was identical to runlevel 3. This target could be created and customized to start local services without changing the default&nbsp;<strong>multi-user.target</strong>.</td></tr><tr><td>multi-user.target</td><td>runlevel3.target</td><td>All services running, but command-line interface (CLI) only</td></tr><tr><td>&nbsp;</td><td>runlevel2.target</td><td>Multi-user, without NFS, but all other non-GUI services running</td></tr><tr><td>rescue.target</td><td>runlevel1.target</td><td>A basic system, including mounting the filesystems with only the most basic services running and a rescue shell on the main console</td></tr><tr><td>emergency.target</td><td>&nbsp;</td><td>Single-user mode—no services are running; filesystems are not mounted. This is the most basic level of operation with only an emergency shell running on the main console for the user to interact with the system.</td></tr><tr><td>halt.target</td><td>&nbsp;</td><td>Halts the system without powering it down</td></tr><tr><td>reboot.target</td><td>runlevel6.target</td><td>Reboot</td></tr><tr><td>poweroff.target</td><td>runlevel0.target</td><td>Halts the system and turns the power off</td></tr></tbody></table><figcaption class="wp-element-caption">Targets in systemd</figcaption></figure>



<h2 class="wp-block-heading">Managing System Services Using Systemctl</h2>



<p>We mainly use <code>systemctl</code> command to manage services on a <code>systemd</code> enabled server.</p>



<p>To view the current status of a service:</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">systemctl status kubelet.service</code></pre>



<p>To start a service:</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">sudo systemctl start kubelet.service</code></pre>



<p>To stop a service:</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">sudo systemctl stop kubelet.service</code></pre>



<p>To restart a service:</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">sudo systemctl restart kubelet.service</code></pre>



<p>To attempt to reload the service without interrupting normal functionality:</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">sudo systemctl reload kubelet.service</code></pre>



<p>By default, most&nbsp;<code>systemd</code>&nbsp;unit files are not started automatically at boot. To configure this functionality, you need to enable to unit.</p>



<p>To enable a service:</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">sudo systemctl enable kubelet.service</code></pre>



<p>To disable a service:</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">sudo systemctl disable kubelet.service</code></pre>



<p>To view all active <code>systemd</code> units:</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">systemctl list-units</code></pre>



<p>To include all units loaded (active + inactive):</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">systemctl list-units --all</code></pre>



<h2 class="wp-block-heading">View Logs Using Journalctl</h2>



<p>A&nbsp;<code>systemd</code>&nbsp;component called&nbsp;<code>journald</code>&nbsp;collects and manages journal entries from all parts of the system. This is basically log information from applications and the kernel. We use <code>journalctl</code> to query logs.</p>



<p>To see all log entries, starting at the oldest entry:</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">journalctl</code></pre>



<p>This will include all logs for all services therefore is not very helpful, so you will need the following commands.</p>



<p>To view log since the current boot:</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">journalctl -b</code></pre>



<p>To view all logs for a service:</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">journalctl -u kubelet.service</code></pre>



<p>To view all logs for a service during the current boot:</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">journalctl -b -u kubelet.service</code></pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
