<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://feeds.feedblitz.com/feedblitz_rss.xslt"?>
<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/"
	xmlns:webfeeds="http://webfeeds.org/rss/1.0"  xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
<channel>
	<title>Baeldung - Linux</title>
	<atom:link href="https://www.baeldung.com/linux/feed" rel="self" type="application/rss+xml" />
	<link>https://www.baeldung.com/linux</link>
	<description>Learn about Linux in practice</description>
	<lastBuildDate>Wed, 01 Apr 2026 15:45:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
<meta xmlns="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
<item>
<feedburner:origLink>https://www.baeldung.com/linux/file-viewing-series</feedburner:origLink>
		<title>Linux File Viewing Series</title>
		<link>https://feeds.feedblitz.com/~/952800815/0/baeldung/linux~Linux-File-Viewing-Series</link>
					<comments>https://feeds.feedblitz.com/~/952800815/0/baeldung/linux~Linux-File-Viewing-Series#respond</comments>
		
		<dc:creator><![CDATA[baeldung]]></dc:creator>
		<pubDate>Wed, 01 Apr 2026 15:45:15 +0000</pubDate>
				<category><![CDATA[File Viewing]]></category>
		<category><![CDATA[Series]]></category>
		<guid isPermaLink="false">https://www.baeldung.com/linux/?p=77774</guid>
					<description><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Files-Featured-Image-03-1024x536.jpg" class="webfeedsFeaturedVisual wp-post-image" alt="" style="max-width:100% !important;height:auto !important;float: left; margin-right: 5px;" fetchpriority="high" /><p>This guide covers the essential techniques for viewing files in Linux.</p>
<p>The post <a rel="NOFOLLOW" href="https://feeds.feedblitz.com/~/952800815/0/baeldung/linux~Linux-File-Viewing-Series">Linux File Viewing Series</a> first appeared on <a rel="NOFOLLOW" href="https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/952800815/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/952800815/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2025%2f01%2fLinux-Files-Featured-Image-03-1024x536.jpg"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/952800815/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/952800815/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/952800815/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a rel="NOFOLLOW" title="View Comments" href="https://www.baeldung.com/linux/file-viewing-series#respond"><img height="20" style="border:0;margin:0;padding:0;" src="https://assets.feedblitz.com/i/comments20.png"></a>&#160;<a title="Follow Comments via RSS" href="https://www.baeldung.com/linux/file-viewing-series/feed"><img height="20" style="border:0;margin:0;padding:0;" src="https://assets.feedblitz.com/i/commentsrss20.png"></a><h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/visio-files-open">How To Open Visio Files in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/file-editing-series">Linux File Editing Series</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/font-files">How to View Font Files in Linux</a></li></ul>&#160;</div>]]>
</description>
										<content:encoded><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Files-Featured-Image-03-1024x536.jpg" class="webfeedsFeaturedVisual wp-post-image" alt="" style="float: left; margin-right: 5px;" decoding="async" loading="lazy" srcset="https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Files-Featured-Image-03-1024x536.jpg 1024w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Files-Featured-Image-03-300x157.jpg 300w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Files-Featured-Image-03-768x402.jpg 768w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Files-Featured-Image-03-100x52.jpg 100w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Files-Featured-Image-03-600x314.jpg 600w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Files-Featured-Image-03.jpg 1200w" sizes="auto, (max-width: 580px) 100vw, 580px" /><p>Linux offers a rich set of tools for <strong>reading, inspecting, and displaying file content</strong> from the command line. Whether you&#8217;re paging through a large log file, searching for patterns with <em>grep</em>, or navigating a file in <em>vim</em>, the shell gives you precise control over how content is viewed.</p>
<p>This guide covers the essential techniques for viewing files in Linux: from core pagers and readers to text searching, Vi/Vim navigation, file comparison, and tools for opening specialized formats like EPUB, PDF, HEIC, and CAD files.</p>
<h2 id="bd-viewing-and-reading-file-content" data-id="viewing-and-reading-file-content"><img decoding="async" class=" wp-image-76987 alignnone" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/04/linux-icons-01.png" alt="linux-icons-01" width="52" height="52" /> Viewing and Reading File Content</h2>
<div class="bd-anchor" id="viewing-and-reading-file-content"></div>
<hr style="margin-top: -50px" />
<ul>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/files-cat-more-less">Viewing Files in Linux Using cat, more, and less</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/more-less-most-commands">Differences Between more, less, and most in Linux</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/display-first-n-characters-of-file">Display the First &#8220;n&#8221; Characters of a File in Linux</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/print-specified-lines-file">Printing Specified Lines From a File</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/file-first-last-line">How to Read the First and the Last Line From Input</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/less-pager-syntax-highlighting">Adding Syntax Highlighting for the less Command</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/tail-colored-output">Using the tail Command with Colored Output</a></li>
</ul>
<h2 id="bd-searching-and-filtering-text" data-id="searching-and-filtering-text"><img decoding="async" class=" wp-image-76988 alignnone" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/04/linux-icons-02.png" alt="linux-icons-02" width="52" height="52" /> Searching and Filtering Text</h2>
<div class="bd-anchor" id="searching-and-filtering-text"></div>
<hr style="margin-top: -50px" />
<ul>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/line-breaks-types">Difference Between CR LF, LF, and CR Line Break Types</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/grep-output-capturing-group-content">grep – How to Output Only the Content of a Capturing Group</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/find-file-encoding">How to Find File Encoding in Linux</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/grep-limit-output">Limiting the Output of grep</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/show-nth-line-after-match">Show Only the N-th Line After the Match</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/grep-after-line-number">Using grep After a Specified Line Number</a></li>
</ul>
<h2 id="bd-navigating-and-viewing-in-vi-and-vim" data-id="navigating-and-viewing-in-vi-and-vim"><img loading="lazy" decoding="async" class=" wp-image-76989 alignnone" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/04/linux-icons-03.png" alt="linux-icons-03" width="52" height="52" /> Navigating and Viewing in Vi and Vim</h2>
<div class="bd-anchor" id="navigating-and-viewing-in-vi-and-vim"></div>
<hr style="margin-top: -50px" />
<ul>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/vi-position-cursor-view">Basic Vi Navigation and Scroll</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/vi-basic-and-advanced-search">Basic and Advanced Searching in Vi</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/jump-specific-character-vi">Character Navigation or How to Jump to a Specific Character in Vi</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/vim-find-full-path-current-file">Displaying the Full Path of the Current File in Vim</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/vim-remove-highlighting">How to Clear Word Highlighting in Vim</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/vim-last-edit-position">How to Make Vim Remember the Last Edit Position</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/vim-windows">Working With Vim Windows</a></li>
</ul>
<h2 id="bd-comparing-and-inspecting-files" data-id="comparing-and-inspecting-files"><img loading="lazy" decoding="async" class=" wp-image-76990 alignnone" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/04/linux-icons-04.png" alt="linux-icons-04" width="52" height="52" /> Comparing and Inspecting Files</h2>
<div class="bd-anchor" id="comparing-and-inspecting-files"></div>
<hr style="margin-top: -50px" />
<ul>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/tar-view-without-extracting">How to View the Contents of tar.gz Without Extracting It</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/compare-two-directories">How To Compare Two Directories on Linux</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/tar-integrity-check">How to Test tar File Integrity</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/compare-text-files">Comparing Two Files in Linux</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/tarballs-difference">Comparing tar Files in Linux</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/color-diff-ouput">How to Color the Output of diff</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/files-display-compare">Displaying Files Side by Side in Linux</a></li>
</ul>
<h2 id="bd-viewing-specialized-file-formats" data-id="viewing-specialized-file-formats"><img loading="lazy" decoding="async" class=" wp-image-76991 alignnone" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/04/linux-icons-05.png" alt="linux-icons-05" width="52" height="52" /> Viewing Specialized File Formats</h2>
<div class="bd-anchor" id="viewing-specialized-file-formats"></div>
<hr style="margin-top: -50px" />
<ul>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/view-images-from-terminal">View Images from the Terminal in Linux</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/epub-viewer-tools">EPUB Viewer Tools in Linux</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/ocr-tools">Linux Optical Character Recognition (OCR) Tools</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/ubuntu-dwg-file-open">How to Open .dwg Format in Ubuntu</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/view-heic-images">How to View .HEIC Photos on Linux</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/dmg-file-manipulation">Opening .dmg Files on a Linux System</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/pdf-metadata-terminal">How to Check PDF Metadata in the Linux Terminal</a></li>
<li style="margin-bottom: 5px"><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/view-msg-file">How to View a .msg File in Linux</a></li>
</ul><p>The post <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/file-viewing-series">Linux File Viewing Series</a> first appeared on <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><Img align="left" border="0" height="1" width="1" alt="" style="border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;" hspace="0" src="https://feeds.feedblitz.com/~/i/952800815/0/baeldung/linux">
<div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/952800815/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/952800815/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2025%2f01%2fLinux-Files-Featured-Image-03-1024x536.jpg"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/952800815/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/952800815/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/952800815/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a rel="NOFOLLOW" title="View Comments" href="https://www.baeldung.com/linux/file-viewing-series#respond"><img height="20" style="border:0;margin:0;padding:0;" src="https://assets.feedblitz.com/i/comments20.png"></a>&#160;<a title="Follow Comments via RSS" href="https://www.baeldung.com/linux/file-viewing-series/feed"><img height="20" style="border:0;margin:0;padding:0;" src="https://assets.feedblitz.com/i/commentsrss20.png"></a><h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/visio-files-open">How To Open Visio Files in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/file-editing-series">Linux File Editing Series</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/font-files">How to View Font Files in Linux</a></li></ul>&#160;</div>]]>
</content:encoded>
					
					<wfw:commentRss>https://feeds.feedblitz.com/~/952800815/0/baeldung/linux~Linux-File-Viewing-Series/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<webfeeds:featuredImage>https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Files-Featured-Image-03-150x150.jpg</webfeeds:featuredImage></item>
<item>
<feedburner:origLink>https://www.baeldung.com/linux/gimp-swap-colors</feedburner:origLink>
		<title>How Can I Swap Colors With GIMP?</title>
		<link>https://feeds.feedblitz.com/~/922051058/0/baeldung/linux~How-Can-I-Swap-Colors-With-GIMP</link>
		
		<dc:creator><![CDATA[Haroon Javed]]></dc:creator>
		<pubDate>Wed, 23 Jul 2025 03:46:22 +0000</pubDate>
				<category><![CDATA[File Editing]]></category>
		<category><![CDATA[Image Processing]]></category>
		<guid isPermaLink="false">https://www.baeldung.com/linux/gimp-swap-colors</guid>
					<description><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/linux-mainpage-on-administration.png" class="webfeedsFeaturedVisual wp-post-image" alt="" style="max-width:100% !important;height:auto !important;float: left; margin-right: 5px;" loading="lazy" /><p>Learn how to swap colors using GIMP.</p>
<p>The post <a rel="NOFOLLOW" href="https://feeds.feedblitz.com/~/922051058/0/baeldung/linux~How-Can-I-Swap-Colors-With-GIMP">How Can I Swap Colors With GIMP?</a> first appeared on <a rel="NOFOLLOW" href="https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/922051058/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/922051058/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2022%2f11%2flinux-mainpage-on-administration.png"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/922051058/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/922051058/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/922051058/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/file-editing-series">Linux File Editing Series</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/image-comparison">Compare Two Images in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/digital-pens-software">Software Alternatives for Utilizing Digital Pens on Linux</a></li></ul>&#160;</div>]]>
</description>
										<content:encoded><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/linux-mainpage-on-administration.png" class="webfeedsFeaturedVisual wp-post-image" alt="" style="float: left; margin-right: 5px;" decoding="async" loading="lazy" srcset="https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/linux-mainpage-on-administration.png 680w, https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/linux-mainpage-on-administration-300x176.png 300w, https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/linux-mainpage-on-administration-100x59.png 100w" sizes="auto, (max-width: 580px) 100vw, 580px" /><h2 id="bd-overview" data-id="overview">1. Overview</h2>
<div class="bd-anchor" id="overview"></div>
<p><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.gimp.org/">GIMP</a> (GNU Image Manipulation Program) is a free, <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/cs/open-source-explained">open-source</a> image editor for tasks ranging from simple drawing to professional photo retouching. It offers layers, brushes, selection tools, and filters like a <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/image-editors-mspaint-alternatives">basic paint app</a> or an advanced photo-editing suite.</p>
<p>While working in GIMP, sometimes we need to swap colors, such as when modifying a logo using a new color scheme, adjusting a photo&#8217;s palette, or just having fun experimenting. Whatever the reason, we can apply these changes in GIMP using simple methods.</p>
<p>In this tutorial, we&#8217;ll explore multiple methods to swap colors in GIMP.</p>
<h2 id="bd-using-the-color-exchange-tool" data-id="using-the-color-exchange-tool">2. Using the <em>Color Exchange</em> Tool</h2>
<div class="bd-anchor" id="using-the-color-exchange-tool"></div>
<p>We can use the <em>Color Exchange</em> tool to directly replace one color with another. <strong>It works best on images with clear, solid colors or when we define the color that we want to replace</strong>.</p>
<p>To swap one color with another, we first open our image and ensure the correct layer is selected. Then, we go to <em>Colors &gt; Map &gt; Color Exchange</em>:</p>
<img decoding="async" class="alignnone size-full wp-image-143498" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/07/Launching-Color-Exchange-Tool.png" alt="Launching Color Exchange Tool" />
<p>A dialog box appears where we choose the color we want to replace by clicking the <em>From Color</em> swatch. Alternatively, we can use the eyedropper to select the color from the image. Next, we click the <em>To Color</em> swatch and choose the new color we want to apply:</p>
<img decoding="async" class="alignnone size-full wp-image-143499" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/07/color-exchange-tool-dialog-menu-box.png" alt="Color exchange tool menu" />
<p><strong>If the color in our image has variations, like shadows or highlights, we can fine-tune the change using the Red, Green, and Blue threshold sliders</strong>. These sliders control how closely a pixel&#8217;s color must match the original for it to be replaced. Lower values will replace only exact matches, while higher values allow for broader color replacements. Once we&#8217;ve set everything, we click <em>OK</em> to apply the changes:</p>
<img decoding="async" class="alignnone size-full wp-image-143500" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/07/color-exchange-tool-dialog-menu-box-with-higher-threshold-value.png" alt="Color exchange tool output with higher threshold value" />
<p>Here, because the threshold value is slightly higher, the image swaps colors over a broader area and may mix with other colors. <strong>To correct this, we can lower the threshold value from 0.500 to 0.2 or 0.3 so that only exact matches are replaced</strong>:</p>
<img decoding="async" class="alignnone size-full wp-image-143501" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/07/Lower-threshold-value.png" alt="Lower threshold value" />
<p>Furthermore, this method works best for simple graphics with flat, solid colors. It might not be ideal for detailed photos or images with complex textures, but it&#8217;s quick and effective for basic color swaps.</p>
<h2 id="bd-using-the-by-color-select-tool" data-id="using-the-by-color-select-tool">3. Using the <em>By Color Select</em> Tool</h2>
<div class="bd-anchor" id="using-the-by-color-select-tool"></div>
<p><strong>In GIMP, we can also use the <em>By Color Select</em> tool to change specific colors in an image, with more control than automatic color replacement</strong>. This method involves selecting all pixels of a specific color first, then changing the color of the selection.</p>
<p>First, <strong>we open our image and choose the <em>By Color Select</em> tool from the toolbox, or we simply press Shift+O on the keyboard</strong>:</p>
<img decoding="async" class="alignnone size-full wp-image-143504" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/07/select-by-color-method.png" alt="Select by Color option" />
<p>Then, we click on the color in our image that we want to change. <strong>GIMP selects all matching pixels based on the threshold setting, which we can adjust in the Tool Options</strong>. Again, a lower threshold selects only very similar colors, while a higher threshold includes a broader range of shades.</p>
<p>Furthermore, <strong>if we want to refine the selection, we can hold Shift to add more colors or Ctrl to remove areas from the selection</strong>. For softer edges, we can go to <em>Select &gt; Feather</em> and choose a small value, like 2 or 3 pixels.</p>
<p>Once the selection looks right, we go to <em>Colors &gt; Colorize</em> to apply a new color. Here, we can adjust the Hue, Saturation, and Lightness sliders, or click the color bar to pick an exact color using RGB or HEX values. After we&#8217;re satisfied with the preview, we click <em>OK</em> to apply the changes:</p>
<img decoding="async" class="alignnone size-full wp-image-143505" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/07/select-by-color-colorize-tool.png" alt="Select by color colorize tool" />
<p>Alternatively, <strong>instead of using <em>Colorize</em>, we can pick the <em>Bucket Fill</em> tool, select a new foreground color, and click inside the selected area to fill it directly</strong>:</p>
<img decoding="async" class="alignnone size-full wp-image-143506" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/07/select-by-color-bucket-tool.png" alt="Select by Color with bucket tool." />
<p>When done, we press Ctrl+Shift+A to deselect and view the results. This method is especially useful for images with patterns, shading, or complex textures where we need precise color control.</p>
<h2 id="bd-using-layer-masks-for-non-destructive-swapping" data-id="using-layer-masks-for-non-destructive-swapping">4. Using Layer Masks for Non-Destructive Swapping</h2>
<div class="bd-anchor" id="using-layer-masks-for-non-destructive-swapping"></div>
<p>We can also use layer masks in GIMP to swap or recolor parts of an image with more precision. For example, to start,<strong> we open our image in GIMP and duplicate the original layer</strong>. This helps us preserve the original image while we work on a copy.</p>
<p>Next, we use a selection tool like <em>By Color Select</em> or <em>Fuzzy Select</em> to isolate the area we want to recolor. If the object has a solid color, the <em>By Color Select</em> tool usually works best. We click on the target area and adjust the threshold if needed, so only the desired region gets selected.</p>
<p>Once the area is selected, we right-click on the duplicated layer and choose <em>Add Layer Mask</em>:</p>
<img decoding="async" class="alignnone size-full wp-image-143508" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/07/adding-layer-mask.png" alt="Adding layer mask" />
<p>Next, in the options that appear, we choose the <em>Selection</em> option and hit <em>Add</em>:</p>
<img decoding="async" class="alignnone size-full wp-image-143509" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/07/adding-a-mask-to-the-layer-slection-menu.png" alt="Adding layer selection " />
<p>This creates a mask that limits edits to only the selected part of the image. Now,<strong> with the layer thumbnail (not the mask) active, we go to <em>Colors &gt; Hue-Saturation</em> or <em>Colors &gt; Colorize</em> to apply a color change</strong>. The mask ensures that the adjustment only affects the specific region we selected earlier.</p>
<p>If the effect spills over too much or misses spots, we can fine-tune the mask. By clicking on the mask thumbnail, we can paint directly on it using a brush: black to hide, white to reveal, and gray for soft blending. This gives us full control over exactly where the new color appears.</p>
<p>Finally, <strong>we check our results by toggling the visibility of the original layer</strong>. Once we&#8217;re happy with the outcome, we can merge the layers or export the image as needed.</p>
<h2 id="bd-conclusion" data-id="conclusion">5. Conclusion</h2>
<div class="bd-anchor" id="conclusion"></div>
<p>In this article, we explored three different ways to swap colors in GIMP.</p>
<p>First, we can use the <em>Color Exchange</em> tool, which is quick and works best for simple, solid colors. Next, the <em>By Color Select</em> tool, combined with <em>Colorize</em> or <em>Hue-Saturation,</em> gives us more control and preserves texture, making it ideal for photos. Finally, Layer Masks offer the most flexibility, allowing us to make detailed edits.</p><p>The post <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/gimp-swap-colors">How Can I Swap Colors With GIMP?</a> first appeared on <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><Img align="left" border="0" height="1" width="1" alt="" style="border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;" hspace="0" src="https://feeds.feedblitz.com/~/i/922051058/0/baeldung/linux">
<div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/922051058/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/922051058/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2022%2f11%2flinux-mainpage-on-administration.png"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/922051058/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/922051058/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/922051058/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/file-editing-series">Linux File Editing Series</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/image-comparison">Compare Two Images in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/digital-pens-software">Software Alternatives for Utilizing Digital Pens on Linux</a></li></ul>&#160;</div>]]>
</content:encoded>
					
		
		
		<webfeeds:featuredImage>https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/linux-mainpage-on-administration-150x150.png</webfeeds:featuredImage></item>
<item>
<feedburner:origLink>https://www.baeldung.com/linux/identify-system-package-manager</feedburner:origLink>
		<title>How to Identify the Package Manager of a Linux System</title>
		<link>https://feeds.feedblitz.com/~/921621143/0/baeldung/linux~How-to-Identify-the-Package-Manager-of-a-Linux-System</link>
		
		<dc:creator><![CDATA[Umara Mushtaq]]></dc:creator>
		<pubDate>Mon, 14 Jul 2025 02:13:11 +0000</pubDate>
				<category><![CDATA[Package Management]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[apt]]></category>
		<category><![CDATA[dnf]]></category>
		<category><![CDATA[pacman]]></category>
		<category><![CDATA[snap]]></category>
		<category><![CDATA[yum]]></category>
		<guid isPermaLink="false">https://www.baeldung.com/linux/identify-system-package-manager</guid>
					<description><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-11-k-1024x536.png" class="webfeedsFeaturedVisual wp-post-image" alt="" style="max-width:100% !important;height:auto !important;float: left; margin-right: 5px;" loading="lazy" /><p>Learn different ways to guess or identify the default package manager of a Linux system.</p>
<p>The post <a rel="NOFOLLOW" href="https://feeds.feedblitz.com/~/921621143/0/baeldung/linux~How-to-Identify-the-Package-Manager-of-a-Linux-System">How to Identify the Package Manager of a Linux System</a> first appeared on <a rel="NOFOLLOW" href="https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/921621143/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/921621143/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2022%2f11%2fLinux-Featured-Image-11-k-1024x536.png"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/921621143/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/921621143/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/921621143/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/check-available-package-updates">How to Check for Available Package Updates in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/valgrind-install-uninstall">How to Properly Install Valgrind?</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/bash-background-jobs-loop">Background Jobs in a Loop in Bash</a></li></ul>&#160;</div>]]>
</description>
										<content:encoded><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-11-k-1024x536.png" class="webfeedsFeaturedVisual wp-post-image" alt="" style="float: left; margin-right: 5px;" decoding="async" loading="lazy" srcset="https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-11-k-1024x536.png 1024w, https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-11-k-300x157.png 300w, https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-11-k-768x402.png 768w, https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-11-k-100x52.png 100w, https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-11-k.png 1200w" sizes="auto, (max-width: 580px) 100vw, 580px" /><h2 id="bd-introduction" data-id="introduction">1. Introduction</h2>
<div class="bd-anchor" id="introduction"></div>
<p>In Linux, package managers handle software installation, updates, and dependency management. Each distribution typically includes a default package manager:</p>
<ul>
<li>Debian and its derivatives often use <em><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/yum-and-apt#apt-advanced-packaging-tool">apt</a></em></li>
<li>RHEL and similar distributions commonly use <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/yum-dnf-reinstall"><em>dnf</em> or <em>yum</em></a></li>
<li>Arch uses <em>pacman</em></li>
<li>SUSE-based systems use <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/list-upgradable-packages#list-zypper-upgradable-packages"><em>zypper</em></a></li>
</ul>
<p>In this tutorial, we&#8217;ll discuss how to identify the package manager used by a given Linux distribution. In scripting, automation, or multi-distro environments, selecting the correct package manager can improve efficiency and reduce the chance of errors.</p>
<h2 id="bd-identifying-package-manager-by-distribution-name" data-id="identifying-package-manager-by-distribution-name">2. Identifying Package Manager by Distribution Name</h2>
<div class="bd-anchor" id="identifying-package-manager-by-distribution-name"></div>
<p>One method to identify the package manager of a system involves checking the distribution name. <strong>Since each major Linux distribution comes with a default package manager, <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/detect-distro">identifying the distribution</a> often indicates which package manager is in use</strong>.</p>
<p>One way to check the distribution is the <em>/etc/os-release</em> script:</p>
<pre><code class="language-bash">$ source /etc/os-release &amp;&amp; echo $NAME
Ubuntu</code></pre>
<p>Alternatively, the <em>lsb_release</em> command should show the name of the distribution:</p>
<pre><code class="language-bash">$ lsb_release -i | cut -f2
Ubuntu</code></pre>
<p>In this case, we extract the second whitespace-separated field.</p>
<p><strong>With this information, it could be easier to determine the default package manager</strong> via a list similar to the one we saw earlier.</p>
<p>In this case, the output shows that the distro is <em>Ubuntu</em>. As already outlined, <em>apt</em> is the default package manager for Ubuntu. To verify that <em>apt</em> is installed, we can use the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/get-path-of-linux-command#which-command"><em>which</em></a> command:</p>
<pre><code class="language-bash">$ which apt
/usr/bin/apt</code></pre>
<p>If the output shows a file path like <em>/usr/bin/apt</em> or similar, <em>apt</em> is installed in the current system and distribution.</p>
<p>If not, it likely isn&#8217;t installed. Of course, <strong>a system can use different package managers</strong>.</p>
<h2 id="bd-identifying-package-managers-with-a-bash-script" data-id="identifying-package-managers-with-a-bash-script">3. Identifying Package Managers With a Bash Script</h2>
<div class="bd-anchor" id="identifying-package-managers-with-a-bash-script"></div>
<p>For a quick and automated way to identify the package manager, we can use a Bash script that checks for common package managers.</p>
<h3 id="bd-1-basic-script-for-a-single-package-manager" data-id="1-basic-script-for-a-single-package-manager">3.1. Basic Script for a Single Package Manager</h3>
<div class="bd-anchor" id="1-basic-script-for-a-single-package-manager"></div>
<p>Let&#8217;s create the script:</p>
<pre><code class="language-bash">$ cat pkgmgrid.sh
if command -v apt &gt; /dev/null 2&gt;&amp;1; then
    echo "apt package manager"
elif command -v dnf &gt; /dev/null 2&gt;&amp;1; then
    echo "dnf package manager"
elif command -v yum &gt; /dev/null 2&gt;&amp;1; then
    echo "yum package manager"
elif command -v zypper &gt; /dev/null 2&gt;&amp;1; then
    echo "zypper package manager"
elif command -v pacman &gt; /dev/null 2&gt;&amp;1; then
    echo "pacman package manager"
else
    echo "No known package manager found"
fi
</code></pre>
<p>Now, we make the script executable and run it:</p>
<pre><code class="language-bash">$ chmod +x pkgmgrid.sh
$ ./pkgmgrid.sh
apt package manager
</code></pre>
<p>Yet, this script detects only one package manager, even if multiple are installed. It uses the chain of <em>if</em>&#8211;<em>elif</em> statements, which means that once it meets a condition, it skips the other options. For example, if Ubuntu has both <em>apt</em> and <em>snap</em> installed, the script identifies only the first one it matches and ignores the others.</p>
<h3 id="bd-2-advanced-script-for-multiple-package-managers" data-id="2-advanced-script-for-multiple-package-managers">3.2. Advanced Script for Multiple Package Managers</h3>
<div class="bd-anchor" id="2-advanced-script-for-multiple-package-managers"></div>
<p>However, some distributions support more than one package manager. For instance, Debian uses both the high-level <em>apt</em> and low-level <em>dpkg</em>. Similarly, Fedora uses <em>dnf</em> and <em>rpm</em>. Additionally, <strong>some systems support universal package managers such as <em><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/snaps-intro">snap</a></em> and <em><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/flatpak">flatpak</a></em> for cross-distribution compatibility</strong>.</p>
<p>For systems with more than one package manager, we can use a more flexible script. <strong>This script uses the <em>for</em> loop to iterate through a list of some common package managers, checking each one without stopping at the first match</strong>. This way, it detects more than one installed package manager. It also detects the Linux distribution and identifies the default package manager based on the detected distro.</p>
<p>Let’s create the script:</p>
<pre><code class="language-bash">$ cat package-manager-id.sh
#!/bin/bash
# Detect installed package managers
package_managers=("apt" "yum" "dnf" "zypper" "pacman" "snap" "flatpak")
echo "Installed package managers on this Linux system:"
for manager in "${package_managers[@]}"; do
    if command -v "$manager" &gt; /dev/null 2&gt;&amp;1; then
        echo "-$manager"
    fi
done
# Detect distro
if [[ -f /etc/os-release ]]; then
    distro=$(source /etc/os-release &amp;&amp; echo $NAME)
else
    distro="*Unknown*"
fi
echo "Detected distro: $distro"
# Default package manager, if any
echo -n "Default distro package manager: "
if [[ "$distro" == "Ubuntu" || "$distro" == "Debian" || "$distro" == "Linux Mint" ]]; then
    echo "apt"
elif [[ "$distro" == "Fedora" || "$distro" == "Red Hat"* ]]; then
   echo "dnf"
elif [[ "$distro" == "CentOS" ]]; then
   echo "yum"
elif [[ "$distro" == "Arch Linux" || "$distro" == "Manjaro" ]]; then
    echo "pacman"
elif [[ "$distro" == "openSUSE" ]]; then
    echo "zypper"
fi</code></pre>
<p>Now, we make the script executable and run it:</p>
<pre><code class="language-bash">$ chmod +x package-manager-id.sh
$ ./package-manager-id.sh
Installed package managers on this Linux system:
-apt
-snap
Detected distro: Ubuntu
Default package manager: apt</code></pre>
<p>The output confirms that the system has both the <em>apt</em> and <em>snap</em> package managers installed, with <em>apt</em> expected to serve as the default package manager.</p>
<h2 id="bd-conclusion" data-id="conclusion">4. Conclusion</h2>
<div class="bd-anchor" id="conclusion"></div>
<p>In this article, we&#8217;ve covered how to identify the package manager of a Linux distribution. We explored methods using system files, commands, and Bash scripts to detect both installed and default package managers. Knowing which package manager the system uses helps make installing, updating, and running scripts easier and more reliable across different Linux systems.</p><p>The post <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/identify-system-package-manager">How to Identify the Package Manager of a Linux System</a> first appeared on <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><Img align="left" border="0" height="1" width="1" alt="" style="border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;" hspace="0" src="https://feeds.feedblitz.com/~/i/921621143/0/baeldung/linux">
<div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/921621143/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/921621143/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2022%2f11%2fLinux-Featured-Image-11-k-1024x536.png"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/921621143/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/921621143/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/921621143/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/check-available-package-updates">How to Check for Available Package Updates in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/valgrind-install-uninstall">How to Properly Install Valgrind?</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/bash-background-jobs-loop">Background Jobs in a Loop in Bash</a></li></ul>&#160;</div>]]>
</content:encoded>
					
		
		
		<webfeeds:featuredImage>https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-11-k-150x150.png</webfeeds:featuredImage></item>
<item>
<feedburner:origLink>https://www.baeldung.com/linux/bash-background-jobs-loop</feedburner:origLink>
		<title>Background Jobs in a Loop in Bash</title>
		<link>https://feeds.feedblitz.com/~/920673941/0/baeldung/linux~Background-Jobs-in-a-Loop-in-Bash</link>
		
		<dc:creator><![CDATA[Sourov Jajodia]]></dc:creator>
		<pubDate>Tue, 24 Jun 2025 11:47:03 +0000</pubDate>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[bash]]></category>
		<guid isPermaLink="false">https://www.baeldung.com/linux/?p=77295</guid>
					<description><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-1024x536.jpg" class="webfeedsFeaturedVisual wp-post-image" alt="" style="max-width:100% !important;height:auto !important;float: left; margin-right: 5px;" loading="lazy" /><p>Learn several patterns to run background jobs in a loop in Bash.</p>
<p>The post <a rel="NOFOLLOW" href="https://feeds.feedblitz.com/~/920673941/0/baeldung/linux~Background-Jobs-in-a-Loop-in-Bash">Background Jobs in a Loop in Bash</a> first appeared on <a rel="NOFOLLOW" href="https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/920673941/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/920673941/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2025%2f01%2fLinux-Administration-Featured-Image-03-1024x536.jpg"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/920673941/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/920673941/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/920673941/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/gnuplot-script-via-shell-script">Executing Commands in Gnuplot through Shell Script</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/convert-output-string">Converting Output to String in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/identify-system-package-manager">How to Identify the Package Manager of a Linux System</a></li></ul>&#160;</div>]]>
</description>
										<content:encoded><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-1024x536.jpg" class="webfeedsFeaturedVisual wp-post-image" alt="" style="float: left; margin-right: 5px;" decoding="async" loading="lazy" srcset="https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-1024x536.jpg 1024w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-300x157.jpg 300w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-768x402.jpg 768w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-100x52.jpg 100w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-600x314.jpg 600w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03.jpg 1200w" sizes="auto, (max-width: 580px) 100vw, 580px" /><h2 id="bd-introduction" data-id="introduction">1. Introduction</h2>
<div class="bd-anchor" id="introduction"></div>
<p>When we write <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/shell-scripts-explained">Bash scripts</a>, we often need to perform repetitive tasks, such as processing files, making requests, or handling data in a loop. The tasks may be time-consuming, and executing them one by one slows down the process. We can execute each iteration as a background process. This way, we don&#8217;t wait for one task to finish before starting the next. But while this looks simple at first, backgrounding inside loops comes with its own set of challenges.</p>
<p>Background jobs can overwhelm the system or create processes that are difficult to control if left unchecked. We should therefore be careful of job limits, handle cleanup correctly, and ensure the script waits for all jobs to complete.</p>
<p>In this tutorial, we&#8217;ll discuss several patterns for running background jobs in loops. We&#8217;ll also have a quick mention of alternatives like <em>xargs</em> and <em>parallel</em>.</p>
<h2 id="bd-basics-of-backgrounding-in-bash" data-id="basics-of-backgrounding-in-bash">2. Basics of Backgrounding in Bash</h2>
<div class="bd-anchor" id="basics-of-backgrounding-in-bash"></div>
<p><strong>Backgrounding commands allows us to move on without waiting for each command to complete. We background a command in Bash using the <em>&amp;</em> operator</strong>. It&#8217;s handy when we need to execute lots of tasks simultaneously, particularly in loops. There is nothing extra to install in the system. As long as we have Bash installed (typically by default on most Linux distros), we are set.</p>
<p>Let&#8217;s take a brief look at an example Bash script to understand how backgrounding works:</p>
<pre><code class="language-bash">#!/bin/bash
sleep 5 &amp; echo "Waiting for the background job..."
wait
echo "Done"</code></pre>
<p>Here we run the sleep 5 command in the background using <em>&amp;</em>. Then we immediately print a message while the sleep command runs in the background. The <em>wait</em> command makes the script wait for the background process to finish. Finally, when everything is finished, we print &#8220;<em>Done</em>.&#8221; This is the foundation of more advanced job control, which we&#8217;ll look at next.</p>
<h2 id="bd-naive-backgrounding-in-a-loop" data-id="naive-backgrounding-in-a-loop">3. Naive Backgrounding in a Loop</h2>
<div class="bd-anchor" id="naive-backgrounding-in-a-loop"></div>
<p><strong>One of the simplest ways to run multiple tasks at once in Bash is to send a command to the background using <em>&amp;</em> within a <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/shell-for-while-loops-multiple-variables">loop</a></strong>. This makes it easy to start many tasks without waiting for each one to finish. It&#8217;s easy to write and is acceptable for small scripts. Let&#8217;s see a simple example:</p>
<pre><code class="language-bash">#!/bin/bash
for i in {1..5}; do
  echo "Processing item $i" &amp;
done
wait
echo "All background jobs finished."
</code></pre>
<p>In here, we&#8217;ve got a for loop echoing messages for items 1 through 5. Each of the echo statements runs in the background using <em>&amp;</em>, so the loop doesn&#8217;t wait. The wait command instructs the script to wait until all background jobs finish before echoing the last message.</p>
<p>This is an efficient way to parallelize tasks, but it has a risk. If the command in the loop is resource-intensive, launching too many background jobs at once can overwhelm the system. We&#8217;ll look at a better means of handling this in subsequent sections.</p>
<h2 id="bd-controlled-parallelism-with-wait" data-id="controlled-parallelism-with-wait">4. Controlled Parallelism With <i>wait</i></h2>
<div class="bd-anchor" id="controlled-parallelism-with-wait"></div>
<p>Background jobs in a loop are fast, but launching too many can overwhelm the system. To alleviate that, we control how many jobs run in parallel.<strong> Bash has the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/wait-command#:~:text=Let's%20create%20a%20shell%20script,with%20the%20name%20testwait.sh.&amp;text=Here%2C%20the%20parent%20process%20will,in%20the%20background%20via%20the%20%26"><em>wait</em></a> and <em>wait -n</em> features to control background jobs nicely without external utility assistance</strong>.</p>
<p>We don&#8217;t have to install anything for this, as <em>wait</em> is included in Bash. To use <em>wait -n</em>, though, we need Bash 5 or newer. To install or upgrade Bash on most Linux distributions:</p>
<pre><code class="language-bash">$sudo apt-get install bash</code></pre>
<p>Let&#8217;s see a basic example where we control the number of background jobs in a loop:</p>
<pre><code class="language-bash">#!/bin/bash
max_jobs=3
for i in {1..10}; do
  echo "Starting job $i" &amp;
  while (( $(jobs -r | wc -l) &gt;= max_jobs )); do
    wait -n
  done
done
wait
echo "All jobs completed."
</code></pre>
<p>Here we have <em>max_jobs = 3</em>, so up to three background jobs are run concurrently. Inside the for loop, we start a job in the background using <em>&amp;</em>. Then we find out how many jobs are running by using <em>jobs -r</em> piped to <em>wc -l</em>. If the active jobs reach the limit, we use <em>wait -n</em>, which waits for any one of them to finish before running another. This technique gives us easy but excellent control over concurrency among jobs, keeping it silky and safe.</p>
<h2 id="bd-using-gnu-parallel-or-xargs--p-as-alternatives" data-id="using-gnu-parallel-or-xargs--p-as-alternatives">6. Using GNU Parallel or <i>xargs -P</i> as Alternatives</h2>
<div class="bd-anchor" id="using-gnu-parallel-or-xargs--p-as-alternatives"></div>
<p><strong>Instead of using background jobs and manually creating a loop to control them, we can use <em><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/xargs-command-tutorial#:~:text=The%20xargs%20Command%20and%20Its%20Basic%20Usage&amp;text=With%20xargs%2C%20we%20can%20use,only%20take%20inputs%20as%20arguments.&amp;text=If%20no%20command%20is%20provided,command%20is%20%2Fbin%2Fecho.">xargs</a> -P</em> or GNU <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/parallel-command-tutorial#:~:text=2.-,The%20parallel%20Command,the%20jobs%20and%20output%20handling.">Parallel</a>. Both simplify parallel execution and let us specify how many jobs run simultaneously</strong>. They create job control internally, which simplifies our scripts. At first, let&#8217;s start by installing these tools:</p>
<pre><code class="language-bash">$ sudo apt-get install parallel findutils</code></pre>
<p>Now, let’s start with a basic example using <i>xargs</i>:</p>
<pre><code class="language-bash">$ seq 1 10 | xargs -n 1 -P 3 -I{} echo "Processing item {}"</code></pre>
<p>In this command, <em>seq 1 10</em> outputs numbers 1 to 10. We pipe it to <em>xargs</em>, which runs the <em>echo</em> command for each number. The <em>-n 1</em> option tells <em>xargs</em> to use a single item per command, <em>-P 3</em> sets the number of parallel jobs to 3, and <em>-I{}</em> specifies a placeholder. So, three echo commands run in parallel. As soon as any job finishes, another starts. Now let&#8217;s try the same thing with GNU Parallel:</p>
<pre><code class="language-bash">$ seq 1 10 | parallel -j 3 echo "Processing item {}"</code></pre>
<p>In this, <em>parallel</em> reads from <em>seq 1 10</em> and runs the <em>echo</em> command on them. The <em>-j 3</em> option does three jobs at one time. <em>{}</em> is a wildcard, like <em>xargs</em>.</p>
<p>Parallel has additional features than <em>xargs</em>—like better handling of output and multi-core awareness—so it&#8217;s better for more complex jobs. Both facilities enable us to keep things clean and efficient without the need to write special job control code. In handling large amounts of input or big commands, they can avoid time wastage and system crashes.</p>
<h2 id="bd-cleanup-and-error-handling" data-id="cleanup-and-error-handling">7. Cleanup and Error Handling</h2>
<div class="bd-anchor" id="cleanup-and-error-handling"></div>
<p><strong>When running background jobs in Bash, we have to be careful about cleaning up the processes we start. If our script is ended prematurely or interrupted, we may leave behind orphaned jobs. </strong>To avoid this, we utilize the <em>trap</em> command. It lets us trap signals like <em>SIGINT</em> or <em>EXIT</em> and run custom commands if something goes wrong or the script ends.
<br>
Here’s a basic example showing how we can use <i>trap</i> to clean up running background jobs:</p>
<pre><code class="language-bash">cleanup() {
  echo "Cleaning up..."
  jobs -p | xargs -r kill
}
trap cleanup SIGINT SIGTERM EXIT
for i in {1..5}; do
  sleep 60 &amp;
done
wait</code></pre>
<p>In this script, we define a <i>cleanup</i> function that kills all running background jobs using <i>jobs -p</i> and <i>xargs kill</i>. Then we use <i>trap</i> to connect this function to signals like <i>SIGINT</i> (Ctrl+C), <i>SIGTERM</i>, and <i>EXIT</i>. This way, if the script is stopped manually or crashes, it&#8217;ll still clean up the background jobs. Finally, we run five background <i>sleep</i> commands and use <i>wait</i> to let them finish. This pattern keeps our scripts safe and avoids leaving stray processes behind.</p>
<h2 id="bd-conclusion" data-id="conclusion">8. Conclusion</h2>
<div class="bd-anchor" id="conclusion"></div>
<p>In this article, we’ve shown that running background jobs in a loop under Bash isn’t just about adding <em>&amp;</em>. Without proper control, it can overwhelm the system. Using <em>wait</em> helps manage this, and <em>wait -n</em> offers better control by reacting as jobs finish.</p>
<p>We also covered tools like <em>xargs -P</em> and <em>parallel</em> for cleaner parallel execution. For real-world scripts, it’s essential to handle cleanup, set job limits, and manage failures. Bash’s built-in tools like <em>trap</em> and <em>jobs</em> make this possible without extra dependencies. These techniques make batch processing faster, safer, and more scalable.</p><p>The post <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/bash-background-jobs-loop">Background Jobs in a Loop in Bash</a> first appeared on <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><Img align="left" border="0" height="1" width="1" alt="" style="border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;" hspace="0" src="https://feeds.feedblitz.com/~/i/920673941/0/baeldung/linux">
<div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/920673941/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/920673941/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2025%2f01%2fLinux-Administration-Featured-Image-03-1024x536.jpg"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/920673941/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/920673941/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/920673941/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/gnuplot-script-via-shell-script">Executing Commands in Gnuplot through Shell Script</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/convert-output-string">Converting Output to String in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/identify-system-package-manager">How to Identify the Package Manager of a Linux System</a></li></ul>&#160;</div>]]>
</content:encoded>
					
		
		
		<webfeeds:featuredImage>https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-150x150.jpg</webfeeds:featuredImage></item>
<item>
<feedburner:origLink>https://www.baeldung.com/linux/c-dup2-redirect-stdout</feedburner:origLink>
		<title>Redirecting Standard Output in C With the dup2() Function</title>
		<link>https://feeds.feedblitz.com/~/920203664/0/baeldung/linux~Redirecting-Standard-Output-in-C-With-the-dup-Function</link>
		
		<dc:creator><![CDATA[Marcin Buczkowski]]></dc:creator>
		<pubDate>Sat, 14 Jun 2025 07:02:25 +0000</pubDate>
				<category><![CDATA[Processes]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[Redirection]]></category>
		<category><![CDATA[Streams]]></category>
		<guid isPermaLink="false">https://www.baeldung.com/linux/c-dup2-redirect-stdout</guid>
					<description><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-12-k-1024x536.png" class="webfeedsFeaturedVisual wp-post-image" alt="" style="max-width:100% !important;height:auto !important;float: left; margin-right: 5px;" loading="lazy" /><p>Learn how redirection works and the way the <em>dup2()</em> function achieves it in C.</p>
<p>The post <a rel="NOFOLLOW" href="https://feeds.feedblitz.com/~/920203664/0/baeldung/linux~Redirecting-Standard-Output-in-C-With-the-dup-Function">Redirecting Standard Output in C With the <em>dup2()</em> Function</a> first appeared on <a rel="NOFOLLOW" href="https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/920203664/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/920203664/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2022%2f11%2fLinux-Featured-Image-12-k-1024x536.png"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/920203664/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/920203664/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/920203664/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/find-sched_rr-time-slice">How to Find the SCHED_RR Scheduling Policy&#x2019;s Time Slice</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/process-user-username-variable">Which Process Sets $USER and $USERNAME</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/start-process-different-tty">Starting a Process on a Different TTY</a></li></ul>&#160;</div>]]>
</description>
										<content:encoded><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-12-k-1024x536.png" class="webfeedsFeaturedVisual wp-post-image" alt="" style="float: left; margin-right: 5px;" decoding="async" loading="lazy" srcset="https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-12-k-1024x536.png 1024w, https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-12-k-300x157.png 300w, https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-12-k-768x402.png 768w, https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-12-k-100x52.png 100w, https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-12-k.png 1200w" sizes="auto, (max-width: 580px) 100vw, 580px" /><h2 id="bd-overview" data-id="overview">1. Overview</h2>
<div class="bd-anchor" id="overview"></div>
<p>Most Linux system commands are built around a simple flow: the command reads from the standard input and writes to the standard output. This arrangement is very rudimentary but also extremely flexible when combined with input-output redirection. As a result, we can perform arbitrary operations on files.</p>
<p>In this tutorial, we&#8217;ll learn how to perform input-output redirection within a C program.</p>
<h2 id="bd-linux-redirection-basics" data-id="linux-redirection-basics">2. Linux Redirection Basics</h2>
<div class="bd-anchor" id="linux-redirection-basics"></div>
<p>In the Linux command line, <strong>two operators govern redirection: <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/pipes-redirection">&lt; for input and &gt; for output</a></strong>.</p>
<p>For instance, let&#8217;s consider the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/cat-command-guide"><em>cat</em></a> command. It reads input and prints it on the screen when Return is encountered:</p>
<pre><code class="language-bash">$ cat
This line will be repeated
This line will be repeated
^C
</code></pre>
<p>If we want to send input to a file, we can redirect the output:</p>
<pre><code class="language-bash">$ cat &gt; output_file
It will go to the file</code></pre>
<p>Now, let&#8217;s redirect <em>output_file</em> to the input of <em>cat</em>, which means <em>cat</em> should print it in the terminal:</p>
<pre><code class="language-bash">$ cat &lt; output_file
It will go to the file</code></pre>
<p>Thus, we populate a file through standard input and produce its contents via standard output.</p>
<h2 id="bd-c-file-descriptors" data-id="c-file-descriptors">3. C File Descriptors</h2>
<div class="bd-anchor" id="c-file-descriptors"></div>
<p>With C, we access files using <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/stale-file-handles#file-handle">file descriptors</a>. For a secondary storage file, we can obtain a descriptor with the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://man7.org/linux/man-pages/man2/open.2.html"><em>open</em></a> system call. The descriptor is an integer variable, set by <em>open</em> to the lowest available value. <strong>A function that operates on file descriptors uses them to find the file metadata, complete information about the corresponding file or device</strong>.</p>
<p>We use special, predefined values for standard streams:</p>
<ul>
<li>standard input (STDIN): 0 (zero)</li>
<li>standard output (STDOUT): 1 (one)</li>
<li>standard error (STDERR): 2 (two)</li>
</ul>
<p>Armed with this basic information, we should be ready to understand file descriptor duplication.</p>
<h2 id="bd-thedup2-function" data-id="thedup2-function">4. The <em>dup2()</em> Function</h2>
<div class="bd-anchor" id="thedup2-function"></div>
<p>The <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://man7.org/linux/man-pages/man2/dup.2.html"><em>dup2()</em></a> function duplicates a file descriptor:</p>
<pre><code class="language-c">int dup2(int oldfd, int newfd)</code></pre>
<p>When the function returns, <em>newfd</em> refers to the file or device indicated by <em>oldfd</em> before. Consequently, the file pointed to by <em>oldfd</em> is closed. So, if the old file descriptor points to standard output and the new one to a file, the file should now act as the standard output. <strong>Any C function or Linux command called inside a C program and designed to write to STDOUT should now output to that file</strong>.</p>
<p>Critically, we achieve this duplication and redirection without any modification to the function code.</p>
<p>Finally, <em>dup2()</em> returns the new file descriptor on success and <em>-1</em> on failure.</p>
<h2 id="bd-examples" data-id="examples">5. Examples</h2>
<div class="bd-anchor" id="examples"></div>
<p>Let&#8217;s examine a few examples to get familiar with the <em>dup2()</em> function. For the sake of simplicity and clarity, we skip error checking after function calls.</p>
<h3 id="bd-1-redirecting-to-file" data-id="1-redirecting-to-file">5.1. Redirecting to File</h3>
<div class="bd-anchor" id="1-redirecting-to-file"></div>
<p>In this first program, <em>example_1.c</em>, we redirect the <em>printf</em> command output to the user-provided file:</p>
<pre><code class="language-c">#include &lt;stdlib.h&gt;   /* for exit */
#include &lt;stdio.h&gt;    /* for printf and stderr */
#include &lt;fcntl.h&gt;    /* for open */
#include &lt;unistd.h&gt;   /* for dup2 */
#define STDOUT 1
int main(int argc, char **argv)
{
    int file_desc;
    char *output_file_name;
    if (argc != 2)
    {
        fprintf(stderr, "Provide output filename, correct use: %s output_file\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    output_file_name = argv[1];
    file_desc = open(output_file_name,
                O_CREAT | O_TRUNC | O_WRONLY, /* create, trunc, only write to */
                S_IRUSR | S_IWUSR);           /* user can read and write, if newly created */
    printf("Printing to standard output before redirection \n");
    printf("Switching standard output to file \"%s\".\n", output_file_name);
    dup2(file_desc, STDOUT); 
    printf("Printing to standard output after redirection.\n");
    exit(EXIT_SUCCESS);
}</code></pre>
<p>In this case, the output filename comes from the command line, stored in<em> argv[1]</em>. Then, we create or open the corresponding file, saving its descriptor in the <em>file_desc</em> variable. Finally, with the <em>dup2()</em> call, we replace STDOUT with this variable.</p>
<p>Let&#8217;s compile the program:</p>
<pre><code class="language-bash">$ gcc example_1.c</code></pre>
<p>Then, we run it, using the default program name, <em>a.out</em>:</p>
<pre><code class="language-bash">$ ./a.out test_output_file
Printing to standard output before redirection 
Switching standard output to file "test_output_file".</code></pre>
<p>Finally, let&#8217;s check the output file:</p>
<pre><code class="language-bash">$ cat test_output_file
Printing to standard output after redirection.</code></pre>
<p>As expected, we see that the specified file contains the expected data.</p>
<h3 id="bd-2-overwriting-current-process" data-id="2-overwriting-current-process">5.2. Overwriting Current Process</h3>
<div class="bd-anchor" id="2-overwriting-current-process"></div>
<p>In the next example, we start the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/basic-system-info-disk-partitions"><em>uname</em></a> command to provide the system name and details. Let&#8217;s study <em>example_2.c</em> code:</p>
<pre><code class="language-c">#include &lt;stdlib.h&gt;   /* for exit */
#include &lt;stdio.h&gt;    /* for printf and stderr */
#include &lt;fcntl.h&gt;    /* for open */
#include &lt;unistd.h&gt;   /* for dup2 */
#define STDOUT 1
int main(int argc, char **argv)
{
    int file_desc;
    char *output_file_name;
    char *cmd[] = { "/usr/bin/uname", "-a", 0 };
    if (argc != 2)
    {
        fprintf(stderr, "Provide output filename, correct use: %s output_file\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    output_file_name = argv[1];
    file_desc = open(output_file_name,
                O_CREAT | O_TRUNC | O_WRONLY, /* create, trunc, only write to */
                S_IRUSR | S_IWUSR);           /* user can read and write, if newly created */
    printf("Output of command %s will be redirected to file \"%s\"\n", cmd[0], output_file_name);
    dup2(file_desc, STDOUT);
    execvp(cmd[0], cmd);
    exit(EXIT_FAILURE);    /* get here only if error in execvp */
}</code></pre>
<p>First, we provide a Linux shell command with arguments as an array of strings in the <em>cmd</em> variable. Then, we create a file and substitute the standard output with it. Afterward, we start the <em>uname</em> command using the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/exec-functions"><em>execvp()</em></a> function. Thus, <strong>the command should run in a new process, an image of the current one</strong>. So, the output substitution holds for this process too.</p>
<p>Now, let&#8217;s compile the program:</p>
<pre><code class="language-bash">$ gcc example_1.c</code></pre>
<p>Afterward, we can start it and examine the result:</p>
<pre><code class="language-c">$ ./a.out test_output_file
Output of command /usr/bin/uname will be redirected to file "test_output_file"
$ cat test_output_file
Linux ubuntu 6.8.0-59-generic #61~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Apr 15 17:03:15 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
</code></pre>
<p>Of course, the redirection works in this case as well.</p>
<h3 id="bd-3-child-process-for-redirection" data-id="3-child-process-for-redirection">5.3. Child Process for Redirection</h3>
<div class="bd-anchor" id="3-child-process-for-redirection"></div>
<p>The successful call of <em>execvp()</em> ends the current process. Now, we want to preserve the current process, so we use the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/fork-child-process"><em>fork()</em></a> function to create a child process.</p>
<pre><code class="language-c">#include &lt;stdlib.h&gt;   /* for exit */
#include &lt;stdio.h&gt;    /* for printf and stderr */
#include &lt;fcntl.h&gt;    /* for open */
#include &lt;unistd.h&gt;   /* for dup2 */
#include &lt;sys/wait.h&gt; /* for wait */
#define STDOUT 1
/* child process function prototype */
void run_cmd(int fd, char **cmd);
int main(int argc, char **argv)
{
    int file_desc;
    char *output_file_name;
    char *cmd[] = { "/usr/bin/uname", "-a", 0 };
    if (argc != 2)
    {
        fprintf(stderr, "Provide output filename, correct use: %s output_file\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    output_file_name = argv[1];
    file_desc = open(output_file_name,
                O_CREAT | O_TRUNC | O_WRONLY, /* create, trunc, only write to */
                S_IRUSR | S_IWUSR);           /* user can read and write, if newly created */
    run_cmd(file_desc, cmd);
    printf("We're finished!\n");
    exit(EXIT_SUCCESS);
}
/* Function to fork and execute in a new thread */
void run_cmd(int fd, char **cmd)
{
    int status; 
    switch (fork()) {
    case 0:    /* child process */
        dup2(fd, STDOUT);
        execvp(cmd[0], cmd);
        exit(EXIT_FAILURE);    /* get here only if error in execvp */
    default: /* parent process */
        while (wait(&amp;status) != -1) ;    /* wait for child to be finished */
        printf("Child process finished\n");
        break;
    case -1: /* fork error, skip handling it */
    }
    return;
}</code></pre>
<p>The <em>run_cmd</em> function creates a new process with <em>fork()</em> and takes appropriate action according to its result. Notably, the code of the <em>run_cmd</em> function is executed in both the child and the parent processes. The return value of <em>fork()</em> is zero, indicating the child process. In this case, we start a new process with changed file descriptors, as in the last example. In the parent process, we wait for the child with the <em>wait()</em> system call.</p>
<p>Let&#8217;s compile and test the program:</p>
<pre><code class="language-bash">$ gcc example_3.c
$ ./a.out test_output_file
Child process finished</code></pre>
<p>We can see that the file descriptor duplication has a process scope. The child <em>dup2()</em> call doesn&#8217;t affect the parent&#8217;s standard output, and the <em>Child process finished</em> message appears in the terminal.</p>
<h2 id="bd-restoring-standard-output" data-id="restoring-standard-output">6. Restoring Standard Output</h2>
<div class="bd-anchor" id="restoring-standard-output"></div>
<p>Now that we&#8217;ve gotten to know different redirections, <strong>let&#8217;s revisit the first example and switch back the standard output to the terminal</strong>. Let&#8217;s check <em>example_4.c</em>:</p>
<pre><code class="language-c">#include &lt;stdlib.h&gt;   /* for exit */
#include &lt;stdio.h&gt;    /* for printf and stderr */
#include &lt;fcntl.h&gt;    /* for open */
#include &lt;unistd.h&gt;   /* for dup2 */
#define STDOUT 1
int main(int argc, char **argv)
{
    int file_desc;
    char *output_file_name;
    if (argc != 2)
    {
        fprintf(stderr, "Provide output filename, correct use: %s output_file\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    output_file_name = argv[1];
    file_desc = open(output_file_name,
                O_CREAT | O_TRUNC | O_WRONLY, /* create, trunc, only write to */
                S_IRUSR | S_IWUSR);       /* user can read and write, if newly created */
    printf("Printing to standard output before redirection \n");
    fflush(stdout);
    int saved_stdout = dup(STDOUT); /* save standard output description for later */
    printf("Switching standard output to file \"%s\".\n", output_file_name);
    dup2(file_desc, STDOUT); 
    printf("Printing to standard output after redirection.\n");
    dup2(saved_stdout, STDOUT); /* copy saved STDOUT to the current one. Ovewrite previous redirection */
    printf("The standard output is terminal again!\n");
    exit(EXIT_SUCCESS);
}</code></pre>
<p>We use <em>dup(),</em> a sister function of <em>dup2()</em>. <strong>This function creates a copy of the file description and enables both files to be operated on simultaneously</strong>. Finally, we use <em>dup2()</em> to switch STDOUT back to standard output.</p>
<p>Let&#8217;s verify the result:</p>
<pre><code class="language-bash">$ gcc example_1a.c
$ ./a.out test_output_file
Printing to standard output before redirection 
Switching standard output to file "test_output_file".
The standard output is terminal again!</code></pre>
<p>This way, we can restore the original STDOUT.</p>
<h2 id="bd-conclusion" data-id="conclusion">7. Conclusion</h2>
<div class="bd-anchor" id="conclusion"></div>
<p>In this article, we learned about output redirection in C.</p>
<p>First, we recalled Linux redirection basics. Then, we took a brief look at how C programs dealt with file descriptors and introduced the <em>dup2()</em> function. Later, we moved to examples of the output redirection in C, substituting a file for the standard output and even redirecting the output of the Linux command <em>uname</em> called from inside the C program.</p><p>The post <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/c-dup2-redirect-stdout">Redirecting Standard Output in C With the <em>dup2()</em> Function</a> first appeared on <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><Img align="left" border="0" height="1" width="1" alt="" style="border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;" hspace="0" src="https://feeds.feedblitz.com/~/i/920203664/0/baeldung/linux">
<div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/920203664/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/920203664/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2022%2f11%2fLinux-Featured-Image-12-k-1024x536.png"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/920203664/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/920203664/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/920203664/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/find-sched_rr-time-slice">How to Find the SCHED_RR Scheduling Policy&#x2019;s Time Slice</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/process-user-username-variable">Which Process Sets $USER and $USERNAME</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/start-process-different-tty">Starting a Process on a Different TTY</a></li></ul>&#160;</div>]]>
</content:encoded>
					
		
		
		<webfeeds:featuredImage>https://www.baeldung.com/wp-content/uploads/sites/2/2022/11/Linux-Featured-Image-12-k-150x150.png</webfeeds:featuredImage></item>
<item>
<feedburner:origLink>https://www.baeldung.com/linux/emergency-recovery-init-bin-bash</feedburner:origLink>
		<title>Linux Emergency Recovery With init=/bin/bash</title>
		<link>https://feeds.feedblitz.com/~/920030138/0/baeldung/linux~Linux-Emergency-Recovery-With-initbinbash</link>
		
		<dc:creator><![CDATA[Francesco Galgani]]></dc:creator>
		<pubDate>Tue, 10 Jun 2025 19:40:59 +0000</pubDate>
				<category><![CDATA[Boot Process]]></category>
		<category><![CDATA[passwd]]></category>
		<category><![CDATA[systemd]]></category>
		<guid isPermaLink="false">https://www.baeldung.com/linux/emergency-recovery-init-bin-bash</guid>
					<description><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-01-1024x536.jpg" class="webfeedsFeaturedVisual wp-post-image" alt="" style="max-width:100% !important;height:auto !important;float: left; margin-right: 5px;" loading="lazy" /><p>Learn to use the init=/bin/bash kernel parameter to access a minimalist root shell on systems that wouldn't otherwise boot.</p>
<p>The post <a rel="NOFOLLOW" href="https://feeds.feedblitz.com/~/920030138/0/baeldung/linux~Linux-Emergency-Recovery-With-initbinbash">Linux Emergency Recovery With init=/bin/bash</a> first appeared on <a rel="NOFOLLOW" href="https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><div class="fbz_enclosure" style="clear:left"><video controls="controls" style="display:block;padding:0.5em 0;max-width:100%;" ><source src="https://feeds.feedblitz.com/-/920030135/0/baeldung/linux.mp4">Click the icon below to watch.</video><a href="https://feeds.feedblitz.com/-/920030135/0/baeldung/linux.mp4" title="Play video"><img border="0" width="40" height="40" src="https://assets.feedblitz.com/i/movie.png"/></a></div>
<div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/920030138/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/920030138/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2025%2f01%2fLinux-Administration-Featured-Image-01-1024x536.jpg"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/920030138/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/920030138/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/920030138/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/wp-content/uploads/sites/2/2025/06/Linux-recovery-with-GRUB-kernel-init-bin-bash-and-LUKS.mp4">Linux Emergency Recovery With init=/bin/bash - Enclosure</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/systemd-deleted-restore">How to Restore Systemd After Deleting It Accidentally</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/dual-boot-windows">How to Dual Boot Windows and Linux</a></li></ul>&#160;</div>]]>
</description>
										<content:encoded><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-01-1024x536.jpg" class="webfeedsFeaturedVisual wp-post-image" alt="" style="float: left; margin-right: 5px;" decoding="async" loading="lazy" srcset="https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-01-1024x536.jpg 1024w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-01-300x157.jpg 300w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-01-768x402.jpg 768w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-01-100x52.jpg 100w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-01-600x314.jpg 600w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-01.jpg 1200w" sizes="auto, (max-width: 580px) 100vw, 580px" /><h2 id="bd-overview" data-id="overview">1. Overview</h2>
<div class="bd-anchor" id="overview"></div>
<p>A Linux system can become unbootable due to misconfigurations, broken packages, or accidental deletions. While a live distro on USB or CD/DVD is typically the recommended recovery support, physical access or compatible media isn&#8217;t always available. In these situations, <strong>booting directly into a shell with the command <em>init=/bin/bash</em> provides just enough access to fix critical problems</strong>, assuming the bootloader and kernel still work.</p>
<p>In this tutorial, we&#8217;ll learn how to apply this technique to a modern, encrypted Linux installation with LVM+LUKS. We&#8217;ll use Linux Mint 22 for our tests, but the information applies to any Debian/Ubuntu-based system.</p>
<h2 id="bd-how-and-why-to-use-initbinbash" data-id="how-and-why-to-use-initbinbash">2. How and Why to Use <em>init=/bin/bash</em></h2>
<div class="bd-anchor" id="how-and-why-to-use-initbinbash"></div>
<p>In most modern Linux distributions, <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/systemd"><em>systemd</em></a> is the first user-space process during the boot process. It runs with Process ID (PID) <em>1</em> and is responsible for launching and managing all other processes in the system:</p>
<pre><code class="language-bash">$ ps -p 1
PID TTY          TIME CMD
  1 ?        00:00:03 systemd</code></pre>
<p>In the GRUB menu, let&#8217;s press <em>e</em> to edit the commands before booting. <strong>Adding <em>init=/bin/bash</em> to the kernel parameters skips the usual startup and launches a Bash shell with PID 1</strong>. Optionally, we can remove <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/quiet-splash-boot-parameters"><em>quiet</em> and <em>splash</em></a>. Then, let&#8217;s press F10 to boot. If the disk uses LUKS and LVM, the unlock prompt appears automatically.</p>
<p>This video shows the full process, with an arrow to highlight where to pay attention:</p>
<p>The system is now in an <strong>emergency recovery state with <em>root</em> privileges</strong>, and <em>/bin/bash</em> has the PID<em> 1</em>:</p>
<pre><code class="language-bash"># ps -p 1
PID TTY          TIME CMD
  1 ?        00:00:00 bash</code></pre>
<p>This allows us to bypass broken service managers, login daemons, and most boot-time scripts. We can use this approach as a last resort when misconfigurations or errors prevent a normal boot.</p>
<h2 id="bd-resetting-the-root-password" data-id="resetting-the-root-password">3. Resetting the <em>root</em> Password</h2>
<div class="bd-anchor" id="resetting-the-root-password"></div>
<p><strong>We can use the <em>init=/bin/bash</em> emergency shell to reset passwords.</strong></p>
<p>First, we need to remount the file system as read/write because it&#8217;s read-only by default:</p>
<pre><code class="language-bash"># mount | grep " / "
/dev/mapper/vgmint-root on / type ext4 (ro,relatime)
# mount -o remount,rw /
[...] EXT4-fs (dm-1): re-mounted [...] r/w. Quota mode: none.
# mount | grep " / "
/dev/mapper/vgmint-root on / type ext4 (rw,relatime,errors=remount-ro)</code></pre>
<p>Then, we can change the password for the <em>root</em> or any other user:</p>
<pre><code class="language-bash"># passwd root
[...]
passwd: password updated successfully</code></pre>
<p>Standard commands like <em>poweroff</em>, <em>reboot</em>, or <em>shutdown</em> don&#8217;t seem to work because the standard <em>init</em> system isn&#8217;t running:</p>
<pre><code class="language-bash"># reboot
System has not been booted with systemd as init system (PID 1). Can't operate.</code></pre>
<p>Usually, the <em>-f</em> flag solves the problem:</p>
<pre><code class="language-bash"># reboot -f</code></pre>
<p>In rare cases, this isn&#8217;t sufficient, so we need a different approach. First, we have to flush any pending disk writes by running <em>sync</em>, and then <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.kernel.org/doc/html/latest/admin-guide/sysrq.html#what-are-the-command-keys">write <em>b</em> to the file <em>/proc/sysrq-trigger</em></a>:</p>
<pre><code class="language-bash"># sync
# echo b &gt; /proc/sysrq-trigger</code></pre>
<p><strong>This method only makes sense in an emergency environment</strong>, as it forces an immediate reboot without first unmounting file systems or stopping services properly. In a production environment, this can result in data loss or corruption.</p>
<h2 id="bd-recovering-a-broken-linux-installation" data-id="recovering-a-broken-linux-installation">4. Recovering a Broken Linux Installation</h2>
<div class="bd-anchor" id="recovering-a-broken-linux-installation"></div>
<p>Here we&#8217;ll try to identify and resolve some issues preventing our Linux machine from booting.</p>
<h3 id="bd-1-fix-disk-errors-preventing-booting" data-id="1-fix-disk-errors-preventing-booting">4.1. Fix Disk Errors Preventing Booting</h3>
<div class="bd-anchor" id="1-fix-disk-errors-preventing-booting"></div>
<p><strong>Unexpected power outages can leave a Linux system inoperable upon reboot</strong>. This includes virtual private servers, where abrupt reboots during cloud provider maintenance can leave guest operating systems with corrupted file systems.</p>
<p>The video below shows a similar failure. We corrupted the file system on purpose and disabled every automatic repair to see what would happen if <em>fsck</em> couldn&#8217;t fix the damage on its own. This is a rare but real situation:</p>
<p>In this scenario, using only <em>init=/bin/bash</em> isn&#8217;t ideal because the kernel will still attempt to mount the corrupted root file system. However, <strong>adding <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://manpages.ubuntu.com/manpages/xenial/man8/initramfs-tools.8.html#kernel%20command%20line"><em>break=premount</em></a> gives us the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/busybox-vs-alpine-docker-images#1-busybox">BusyBox&#8217;s emergency shell</a> without mounting any file system</strong>. It runs entirely from the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/initrd-vs-initramfs#what-is-initramfs"><em>initramfs</em></a> in RAM:</p>
<a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/wp-content/uploads/sites/2/2025/06/Linux-kernel-break-premount-parameter-to-avoid-mounting-disks.png"><img decoding="async" class="aligncenter size-full wp-image-143335" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/06/Linux-kernel-break-premount-parameter-to-avoid-mounting-disks.png" alt="Linux kernel break premount parameter to avoid mounting disks" /></a>
<p>BusyBox offers a variety of <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://busybox.net/downloads/BusyBox.html#commands">essential recovery commands</a>, such as <em>fsck</em>, <em>cryptsetup</em>, and <em>lvm</em>. However, the list of commands in the man page is incomplete.</p>
<p><strong>In BusyBox, let&#8217;s manually mount LVM and fix any errors in the file system</strong>. Of course, we must be careful to adapt these example commands to our configuration:</p>
<pre><code class="language-bash">(initramfs) cryptsetup open --type luks /dev/sda3 cryptroot
Enter passphrase for /dev/sda3: 
 
(initramfs) lvm vgchange -ay
  2 logical volume(s) in volume group "vgmint" now active
 
(initramfs) fsck.ext4 -f /dev/mapper/vgmint-root
e2fsck 1.47.0 (5-Feb-2023)
/dev/mapper/vgmint-root: recovering journal
Clearing orphaned inode 264824 (uid=0, gid=0, mode=0100600, size=2)
Clearing orphaned inode 264849 (uid=0, gid=0, mode=0100644, size=1131)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Free blocks count wrong (3456692, counted=3456674).
Fix&lt;y&gt;? yes
Free inodes count wrong (901506, counted=901501).
Fix&lt;y&gt;? yes
/dev/mapper/vgmint-root: ***** FILE SYSTEM WAS MODIFIED *****
/dev/mapper/vgmint-root: 469379/1370880 files (0.1% non-contiguous), 2022750/5479424 blocks
 
(initramfs) reboot -f</code></pre>
<p>This way, Linux reboots smoothly without disk errors.</p>
<p>Alternatively, instead of rebooting, <strong>we can type <em>exit</em> to quit BusyBox and start <em>/bin/bash</em> to perform additional recovery operations</strong>.</p>
<h3 id="bd-2-internet-connection-to-reinstall-broken-packages" data-id="2-internet-connection-to-reinstall-broken-packages">4.2. Internet Connection to Reinstall Broken Packages</h3>
<div class="bd-anchor" id="2-internet-connection-to-reinstall-broken-packages"></div>
<p>If essential packages are missing or damaged, the system can&#8217;t finish a normal boot. In this example, Linux fails to boot because <em>/sbin/init</em> is missing:</p>
<p><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/wp-content/uploads/sites/2/2025/06/Linux-doesnt-boot-because-init-is-missing.png"><img loading="lazy" decoding="async" class="aligncenter wp-image-143343 size-full" src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/06/Linux-doesnt-boot-because-init-is-missing.png" alt="Linux doesnt boot because init is missing" width="1054" height="189" /></a>If we can boot with <em>init=/bin/bash</em>, then we can try to restore some critical packages.</p>
<p><strong>Here is an example of how to manually set up an Internet connection to use the package manager</strong>. However, let&#8217;s keep in mind to adapt the commands to our network configuration:</p>
<pre><code class="language-bash"># Remount root filesystem as read-write
# mount -o remount,rw /
# Show network interfaces
# ip link show
enp0s3: &lt;BROADCAST,MULTICAST&gt; mtu 1500 qdisc noop state DOWN [...]
# Bring up the network interface
# ip link set enp0s3 up
[...] enp0s3 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
# Request an IP address via DHCP (error may appear, safe to ignore)
# dhclient enp0s3
# Check assigned IP address
# ip -brief addr show enp0s3
enp0s3  UP  10.0.2.15/24 fe80::a00:27ff:fe95:27fd/64
# Confirm network interface configuration
# ifconfig
enp0s3: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        [...]
# Mount proc filesystem
# mount -t proc none /proc
# Mount sysfs
# mount --rbind /sys /sys
# Mount devfs
# mount --rbind /dev /dev
# Add default route (may already exist)
# ip route add default via 10.0.2.2 dev enp0s3
# Show current routing table
# ip route
default via 10.0.2.2 dev enp0s3
10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.15
# Set DNS resolver manually
# echo "nameserver 1.1.1.1" &gt; /etc/resolv.conf
# Test internet connectivity
# ping -c 1 baeldung.com
PING baeldung.com (172.66.40.248) 56(84) bytes of data.
64 bytes from 172.66.40.248: icmp_seq=1 ttl=255 time=52.8 ms
--- baeldung.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms</code></pre>
<p>We&#8217;re now ready to reinstall some essential packages:</p>
<ul>
<li><em>systemd + systemd-sysv</em>: provide <em>/sbin/init</em> and start every other service</li>
<li><em>libc6</em>: the GNU C runtime; almost every binary on the disk links to it</li>
<li><em>libpam-modules + libpam-modules-bin</em>: vital for login, <em>su</em>, <em>sudo</em>, and graphical display managers</li>
<li><em>initramfs-tools</em>: builds the <em>initramfs</em> image that the bootloader hands to the kernel</li>
<li><em>linux-image-generic</em>: meta-package for the current kernel image and headers</li>
</ul>
<p>Let&#8217;s try to reinstall them:</p>
<pre><code class="language-shell"># dpkg --configure -a
# apt update
# apt install --reinstall systemd systemd-sysv libc6 \
    libpam-modules libpam-modules-bin initramfs-tools linux-image-generic \
    libpam0g libpam-runtime
# update-initramfs -c -k all
# dpkg --configure -a</code></pre>
<p>We can install as many other packages as we want.</p>
<p><strong>At this stage, we can manually edit configuration files such as <em>/etc/fstab</em>, <em>/etc/crypttab</em>, and <em>/etc/shadow</em>. We can also remove problematic drivers or modules and perform other administrative tasks to fix a system that won&#8217;t boot up.</strong></p>
<p>Finally, let&#8217;s run <em>reboot -f</em> to make Linux reboot smoothly.</p>
<h2 id="bd-conclusion" data-id="conclusion">5. Conclusion</h2>
<div class="bd-anchor" id="conclusion"></div>
<p><strong>In this article, we examined how to use the <em>init=/bin/bash</em> kernel parameter and the <em>break=premount</em> fallback to access a minimalist root shell on systems that wouldn&#8217;t otherwise boot</strong>.</p>
<p>This <em>root</em> environment enables us to unlock LUKS volumes, activate LVM, remount the root file system for reading and writing, reset forgotten passwords, and repair corrupted file systems using <em>fsck</em>. We can also reinstall core packages after setting up the network manually and carry out various configuration fixes.</p>
<p>Mastering these low-level recovery techniques can transform seemingly catastrophic &#8220;won&#8217;t boot&#8221; situations into solvable problems. However, it&#8217;s always important to maintain an up-to-date backup for disaster recovery purposes.</p><p>The post <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/emergency-recovery-init-bin-bash">Linux Emergency Recovery With init=/bin/bash</a> first appeared on <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><Img align="left" border="0" height="1" width="1" alt="" style="border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;" hspace="0" src="https://feeds.feedblitz.com/~/i/920030138/0/baeldung/linux">
<div class="fbz_enclosure" style="clear:left"><video controls="controls" style="display:block;padding:0.5em 0;max-width:100%;" ><source src="https://feeds.feedblitz.com/-/920030135/0/baeldung/linux.mp4">Click the icon below to watch.</video><a href="https://feeds.feedblitz.com/-/920030135/0/baeldung/linux.mp4" title="Play video"><img border="0" width="40" height="40" src="https://assets.feedblitz.com/i/movie.png"/></a></div>
<div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/920030138/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/920030138/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2025%2f01%2fLinux-Administration-Featured-Image-01-1024x536.jpg"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/920030138/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/920030138/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/920030138/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/wp-content/uploads/sites/2/2025/06/Linux-recovery-with-GRUB-kernel-init-bin-bash-and-LUKS.mp4">Linux Emergency Recovery With init=/bin/bash - Enclosure</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/systemd-deleted-restore">How to Restore Systemd After Deleting It Accidentally</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/dual-boot-windows">How to Dual Boot Windows and Linux</a></li></ul>&#160;</div>]]>
</content:encoded>
					
		
		<enclosure url="https://feeds.feedblitz.com/-/920030135/0/baeldung/linux.mp4" length="873366" type="video/mp4" />
<enclosure url="https://www.baeldung.com/linux/wp-content/uploads/sites/2/2025/06/Linux-fails-to-boot-panic-for-disk-error.mp4" length="927198" type="video/mp4" />
		<webfeeds:featuredImage>https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-01-150x150.jpg</webfeeds:featuredImage>
<feedburner:origEnclosureLink>https://www.baeldung.com/linux/wp-content/uploads/sites/2/2025/06/Linux-recovery-with-GRUB-kernel-init-bin-bash-and-LUKS.mp4</feedburner:origEnclosureLink>
</item>
<item>
<feedburner:origLink>https://www.baeldung.com/linux/selinux</feedburner:origLink>
		<title>What Is SELinux?</title>
		<link>https://feeds.feedblitz.com/~/919939004/0/baeldung/linux~What-Is-SELinux</link>
		
		<dc:creator><![CDATA[Zachary Bouhannana]]></dc:creator>
		<pubDate>Mon, 09 Jun 2025 04:53:08 +0000</pubDate>
				<category><![CDATA[Administration]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[chmod]]></category>
		<category><![CDATA[ls]]></category>
		<category><![CDATA[SELinux]]></category>
		<guid isPermaLink="false">https://www.baeldung.com/linux/selinux</guid>
					<description><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-1-1024x536.png" class="webfeedsFeaturedVisual wp-post-image" alt="" style="max-width:100% !important;height:auto !important;float: left; margin-right: 5px;" loading="lazy" /><p>Learn what SELinux is, how it works, and the way it can provide better security when paired with the common discretionary access control.</p>
<p>The post <a rel="NOFOLLOW" href="https://feeds.feedblitz.com/~/919939004/0/baeldung/linux~What-Is-SELinux">What Is SELinux?</a> first appeared on <a rel="NOFOLLOW" href="https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/919939004/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/919939004/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2021%2f09%2fFeatured-Linux-1-1024x536.png"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/919939004/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/919939004/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/919939004/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/ssh-scp-password-subprocess">Sending a Password via SSH or SCP Using subprocess.Popen in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/ssh-reverse-tunnel-port-forward-errors">Resolving the Reverse Tunnel Remote port forwarding failed for listen port SSH Error</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/usb-fix-read-only-mounts">How to Fix USB Sticks Mounted as Read-Only</a></li></ul>&#160;</div>]]>
</description>
										<content:encoded><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-1-1024x536.png" class="webfeedsFeaturedVisual wp-post-image" alt="" style="float: left; margin-right: 5px;" decoding="async" loading="lazy" srcset="https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-1-1024x536.png 1024w, https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-1-300x157.png 300w, https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-1-768x402.png 768w, https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-1-100x52.png 100w, https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-1.png 1200w" sizes="auto, (max-width: 580px) 100vw, 580px" /><h2 id="bd-overview" data-id="overview">1. Overview</h2>
<div class="bd-anchor" id="overview"></div>
<p>We often rely on traditional security measures, such as strong passwords, permissions, and firewalls, to protect Linux systems. However, sometimes we need more fine-grained control, such as managing the possible actions of processes. That&#8217;s where <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/selinux-setup-disable#introduction">SELinux (Security-Enhanced Linux)</a> comes in.</p>
<p>Originally developed by the National Security Agency (NSA) of the USA and later released to the open-source community, SELinux enables sysadmins to define specific access policies to control how processes and users interact with system resources.</p>
<p>In this tutorial, we&#8217;ll explore what SELinux is, how it works, and why it matters.</p>
<h2 id="bd-what-exactly-is-selinux" data-id="what-exactly-is-selinux">2. What Exactly Is SELinux?</h2>
<div class="bd-anchor" id="what-exactly-is-selinux"></div>
<p>SELinux is a security module built into the Linux kernel to provide extra access control beyond the usual Unix-style permission system. It plays a key role in protecting against privilege escalation, misconfigurations, and zero-day exploits.</p>
<p>SELinux matters most in environments where security is crucial. Whether running web servers, databases, or containerized apps, SELinux can mean the difference between a contained threat and a full system compromise.</p>
<p><strong>To truly appreciate SELinux, it&#8217;s wise to understand how it differs from the standard Linux security models</strong>.</p>
<h3 id="bd-1-the-problem-with-traditional-permissions-dac" data-id="1-the-problem-with-traditional-permissions-dac">2.1. The Problem With Traditional Permissions (DAC)</h3>
<div class="bd-anchor" id="1-the-problem-with-traditional-permissions-dac"></div>
<p>In Linux, Discretionary Access Control (DAC) is the &#8220;read-write-execute&#8221; permissions that control who can do what with a file. These provide the standard layer of security. Simply put, the owner of a file or process can decide who gets access.</p>
<p>While flexible, this can be a weak point. If a malicious program manages to get control, it might be able to use those discretionary permissions to escalate the damage.</p>
<p>For example, if a user owns a script, that user decides who can execute it. If a privileged user (like <em>root</em>) or a compromised process gains control, it can potentially access or modify any resource on the system. Traditionally, a <em>root</em> user can bypass DAC checks.</p>
<h3 id="bd-2-the-solution-of-mandatory-access-control-mac" data-id="2-the-solution-of-mandatory-access-control-mac">2.2. The Solution of Mandatory Access Control (MAC)</h3>
<div class="bd-anchor" id="2-the-solution-of-mandatory-access-control-mac"></div>
<p>The big shift with SELinux is how it flips the way access control works. Most administrators and users are well aware of DAC, where file owners have a say. However, SELinux adds MAC to the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/kernel-source-code-headers#the-linux-kernel">kernel</a>.</p>
<p><strong>Under MAC, the operating system, rather than individual users, strictly enforces a system-wide security policy</strong> defined by the admin. Every single system call, every access attempt, gets checked against this policy.</p>
<p>What&#8217;s more, even the <em>root</em> user must comply. <strong>SELinux can limit <em>root</em> actions if they&#8217;re outside the defined policy</strong>. In other words, this is a much tighter security. We can stop unauthorized access or modifications to files, processes, and resources, even from a superuser account.</p>
<h3 id="bd-3-default-deny-principle" data-id="3-default-deny-principle">2.3. Default Deny Principle</h3>
<div class="bd-anchor" id="3-default-deny-principle"></div>
<p>The SELinux MAC approach relies on the <em>default deny</em> principle.</p>
<p>In short, <strong>if the SELinux rules don&#8217;t explicitly allow a specific action or access,  it&#8217;s automatically denied</strong>. This <em>deny-by-default</em> stance alone provides a great security advantage by reducing the attack surface.</p>
<h3 id="bd-4-how-mac-and-dac-complete-each-other" data-id="4-how-mac-and-dac-complete-each-other">2.4. How MAC and DAC Complete Each Other</h3>
<div class="bd-anchor" id="4-how-mac-and-dac-complete-each-other"></div>
<p>SELinux doesn&#8217;t replace the basic file permissions that <em>chmod</em> or <em>chown</em> manage. Instead, it works alongside them in a specific order to provide deeper and mandatory control.</p>
<p>When a process tries to access a file, the system first checks DAC policy rules. Only if those basic permissions allow access, SELinux then steps in to evaluate its security policies (MAC).</p>
<p><strong>This sequence means SELinux acts as a second checkpoint</strong>. This adds extra protection for scenarios where the DAC level might be too permissive, or when a compromised process manages to bypass it.</p>
<h2 id="bd-selinux-core" data-id="selinux-core">3. SELinux Core</h2>
<div class="bd-anchor" id="selinux-core"></div>
<p>A SELinux-enabled system relies on key security concepts to guide access decisions. Let&#8217;s see how these principles can improve Linux system security.</p>
<h3 id="bd-1-security-context-label" data-id="1-security-context-label">3.1. Security Context (Label)</h3>
<div class="bd-anchor" id="1-security-context-label"></div>
<p>SELinux assigns a <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/7/html/selinux_users_and_administrators_guide/chap-security-enhanced_linux-selinux_contexts">security context</a> (also called a security label) to every file, user, and process on the system. This context or label is a set of attributes that SELinux uses to determine access permissions. Simply put, <strong>these labels are name tags that tell what&#8217;s allowed and what&#8217;s not</strong>.</p>
<p>A security context consists of four parts (<em>user:role:type:level</em>). Let&#8217;s check what each one does:</p>
<ul>
<li><em><strong>user</strong></em>: maps to one or more Linux users and defines which roles a user can assume, e.g., <em>user_u</em> or <em>unconfined_u</em> (not the same as the OS user)</li>
<li><em><strong>role</strong></em>: limits the actions certain users or types can perform, defining which types a user assigned to a role can access (SELinux assigns role names with the <em>_r</em> suffix, such as <em>system_r</em>)</li>
<li><em><strong>type</strong></em>: domain for processes and a type for files, which defines how processes and files interact with each other (all files and running processes are assigned a type ending in <em>_t</em>, for example, <em>httpd_t</em> for the web server process or <em>var_t</em> for files in <em>/var</em>)</li>
<li><em><strong>level</strong></em> (optional): range of clearance levels within Multi-Level Security (MLS) systems to set sensitivity levels, such as <em>s0</em> or <em>s1</em></li>
</ul>
<p>Now, <strong>let&#8217;s run the <em>ls -Z</em> command on a directory with files to see some examples of what a security context may look like</strong>:</p>
<pre><code class="language-bash">$ ls -Z
-rw-------. root root system_u:object_r:httpd_sys_content_t:s0 index.html
drwxrwxrwx. root root unconfined_u:object_r:admin_home_t:s0 backup</code></pre>
<p>Here, the command lists file ownership (<em>root</em>) along with its <em>rwx</em> permissions. Moreover, the <em>-Z</em> switch includes the four parts of the security context of each entry (user, role, type, and security level), as in <em>system_u:object_r:httpd_sys_content_t:s0</em>.</p>
<h3 id="bd-2-policy-rules" data-id="2-policy-rules">3.2. Policy Rules</h3>
<div class="bd-anchor" id="2-policy-rules"></div>
<p>SELinux uses <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/8/html/using_selinux/writing-a-custom-selinux-policy_using-selinux">policy</a> modules to define what each process can do. Every possible action (like <em>process of type X reads file of type Y</em>) needs an explicit rule allowing it, or else SELinux denies it. As we mentioned before, <strong>SELinux forbids everything by default unless we explicitly allow it</strong>.</p>
<p>For example, a policy rule might say &#8220;allow <em>httpd_t</em> processes to read <em>httpd_sys_content_t</em> files&#8221;. If we try an action not in the rules, SELinux blocks it and logs a denial.</p>
<p><strong>To set a policy rule, we do so by following a basic syntax</strong>:</p>
<pre><code class="language-bash">&lt;rule_type&gt; &lt;subject&gt; &lt;object&gt;:&lt;object_class&gt; &lt;permission&gt;;</code></pre>
<p>Let&#8217;s break down what each part means:</p>
<ul>
<li><em>rule_type</em>: action SELinux should take, usually <em>allow</em>, <em>dontaudit</em>, or <em>neverallow</em></li>
<li><em>subject</em>: domain or type of the process requesting access</li>
<li><em>object</em>: type of the resource being accessed</li>
<li><em>object_class</em>: class of the object, like <em>file</em>, <em>dir</em>, <em>tcp_socket</em></li>
<li><em>permission</em>: action being allowed, like <em>read</em>, <em>write</em>, <em>open</em></li>
</ul>
<p>For example, we can check a simple policy rule in action:</p>
<pre><code class="language-bash">ALLOW apache_process apache_log:FILE READ;</code></pre>
<p>This rule simply allows a process labeled <em>apache_process</em> to read a log file labeled <em>apache_log</em>.</p>
<h3 id="bd-3-booleans" data-id="3-booleans">3.3. Booleans</h3>
<div class="bd-anchor" id="3-booleans"></div>
<p>How about if we want to turn off a rule without editing the security policy?</p>
<p>An SELinux boolean is an on-off switch that enables changing how rules behave without rewriting an entire SELinux policy. For example, a common boolean is <em>httpd_can_network_connect</em>. By default, it might be off, so the web server (<em>httpd_t</em>) can&#8217;t make outbound network connections.</p>
<p>Let&#8217;s see how we can turn it on using the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.man7.org/linux/man-pages/man8/setsebool.8.html"><em>setsebool</em></a> command:</p>
<pre><code class="language-bash">$ setsebool -P httpd_can_network_connect on</code></pre>
<p>By setting the above boolean to <em>on</em>, SELinux allows access without needing to tinker with policy files. Moreover, <strong>instead of using the <em>on</em>&#8211;<em>off</em> options, we can use their equivalent numerical values <em>0</em>&#8211;<em>1</em></strong>.</p>
<p>In fact, SELinux comes with many such booleans &#8211; for actions like allowing FTP write access, using NFS, and more &#8211; to adjust rules at runtime. We can list all the rules with <em><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.man7.org/linux/man-pages/man8/getsebool.8.html">getsebool</a> -a</em>:</p>
<pre><code class="language-bash">$ getsebool -a | less
abrt_anon_write --&gt; off
abrt_handle_event --&gt; off
abrt_upload_watch_anon_write --&gt; on
antivirus_can_scan_system --&gt; off
antivirus_use_jit --&gt; off
auditadm_exec_content --&gt; on
authlogin_nsswitch_use_ldap --&gt; off
authlogin_radius --&gt; off
authlogin_yubikey --&gt; off
awstats_purge_apache_log_files --&gt; off
:</code></pre>
<p>Here, the output can be quite long, so it&#8217;s best to pipe it to <em>less</em> to scroll through it page by page.</p>
<h3 id="bd-4-modes" data-id="4-modes">3.4. Modes</h3>
<div class="bd-anchor" id="4-modes"></div>
<p>SELinux can run in three <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/8/html/using_selinux/changing-selinux-states-and-modes_using-selinux">modes</a>, each offering different levels of security control:</p>
<ul>
<li><strong>enforcing (<em>0</em>)</strong>: actively blocks violations</li>
<li><strong>permissive (<em>1</em>)</strong>: logs violations without blocking</li>
<li><strong>disabled</strong>: turns off SELinux completely, with the system falling back to normal DAC permissions</li>
</ul>
<p>We can check the current SELinux mode with <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.man7.org/linux/man-pages/man8/getenforce.8.html"><em>getenforce</em></a>:</p>
<pre><code class="language-bash">$ getenforce
Enforcing</code></pre>
<p>In this case, SELinux is in <em>Enforcing</em> mode.</p>
<p>Now, let&#8217;s use the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://man7.org/linux/man-pages/man8/setenforce.8.html"><em>setenforce</em></a> mode to change it to <em>Permissive</em>:</p>
<pre><code class="language-bash">$ setenforce 0</code></pre>
<p>Here, the <em>0</em> is the <em>MODE_ID</em> for the Permissive mode. Conversely, 1 is the ID for <em>Enforcing</em> mode.</p>
<p>Notably, <em>setenforce</em> doesn&#8217;t persist after reboot. However, <strong>we can directly edit the SELinux <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/selinux-setup-disable#1-etcselinuxconfig">config file</a> (<em>/etc/selinux/config</em>) to make changes persist</strong>. Moreover, editing the config file is also how we can switch to <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/selinux-setup-disable#disable-selinux"><em>Disabled</em> mode</a>.</p>
<h2 id="bd-why-is-selinux-important" data-id="why-is-selinux-important">4. Why Is SELinux Important?</h2>
<div class="bd-anchor" id="why-is-selinux-important"></div>
<p>It may seem that Linux file permissions (read, write, execute) and user ownership should be enough to keep a system secure. Of course, for simple setups, they usually are. However, once we run complex Internet-facing services, standard access permissions don&#8217;t cover every scenario.</p>
<p>A major advantage of SELinux is its ability to limit damage during security breaches.</p>
<p>Let&#8217;s say a web server (Apache <em>httpd</em>) is hacked. With traditional permissions, the attacker could read sensitive files in <em>/etc</em>, modify scripts in <em>/var/www</em>, and access user data.</p>
<p>Now, we can see a simple example of how SELinux can block access even for <em>root</em> users based on file labels.</p>
<p>First, we create a test file in the <em>root</em> user&#8217;s home directory:</p>
<pre><code class="language-bash">$ echo "secret" &gt; /root/zfile</code></pre>
<p>Now let&#8217;s change the SELinux label of that file with the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://man7.org/linux/man-pages/man1/chcon.1.html"><em>chcon</em></a> command:</p>
<pre><code class="language-bash">$ chcon -t admin_home_t /root/zfile</code></pre>
<p>Here, we assign <em>/root/zfile</em> the type <em>admin_home_t</em>, which isn&#8217;t meant for web servers to access.</p>
<p>Finally, let&#8217;s check whether Apache can access the file:</p>
<pre><code class="language-bash">$ runcon -t httpd_t -- cat /root/testfile
cat: /root/zfile: Permission denied</code></pre>
<p>We use the <em><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://man7.org/linux/man-pages/man1/runcon.1.html">runcon</a> -t</em> command to run <em>cat /root/zfile</em> under a different SELinux context (<em>httpd_t</em>), which is the domain Apache runs in. <strong>Even though <em>cat</em> runs as <em>root</em>, SELinux steps in and blocks it</strong>.</p>
<h2 id="bd-conclusion" data-id="conclusion">5. Conclusion</h2>
<div class="bd-anchor" id="conclusion"></div>
<p>In this article, we explored what SELinux is, why it matters, and how it strengthens Linux security.</p>
<p>In particular, we looked at the way it works alongside traditional access permissions using labels, policies, and strict access rules. Through examples, we saw how SELinux can block unauthorized actions, even from superusers.</p><p>The post <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/selinux">What Is SELinux?</a> first appeared on <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><Img align="left" border="0" height="1" width="1" alt="" style="border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;" hspace="0" src="https://feeds.feedblitz.com/~/i/919939004/0/baeldung/linux">
<div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/919939004/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/919939004/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2021%2f09%2fFeatured-Linux-1-1024x536.png"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/919939004/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/919939004/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/919939004/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/ssh-scp-password-subprocess">Sending a Password via SSH or SCP Using subprocess.Popen in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/ssh-reverse-tunnel-port-forward-errors">Resolving the Reverse Tunnel Remote port forwarding failed for listen port SSH Error</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/usb-fix-read-only-mounts">How to Fix USB Sticks Mounted as Read-Only</a></li></ul>&#160;</div>]]>
</content:encoded>
					
		
		
		<webfeeds:featuredImage>https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-1-150x150.png</webfeeds:featuredImage></item>
<item>
<feedburner:origLink>https://www.baeldung.com/linux/ssh-reverse-tunnel-port-forward-errors</feedburner:origLink>
		<title>Resolving the Reverse Tunnel Remote port forwarding failed for listen port SSH Error</title>
		<link>https://feeds.feedblitz.com/~/919939007/0/baeldung/linux~Resolving-the-Reverse-Tunnel-Remote-port-forwarding-failed-for-listen-port-SSH-Error</link>
		
		<dc:creator><![CDATA[Hiks Gerganov]]></dc:creator>
		<pubDate>Mon, 09 Jun 2025 04:49:53 +0000</pubDate>
				<category><![CDATA[Network Configuration]]></category>
		<category><![CDATA[Network Monitoring]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[ssh]]></category>
		<guid isPermaLink="false">https://www.baeldung.com/linux/ssh-reverse-tunnel-port-forward-errors</guid>
					<description><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-1024x536.jpg" class="webfeedsFeaturedVisual wp-post-image" alt="" style="max-width:100% !important;height:auto !important;float: left; margin-right: 5px;" loading="lazy" /><p>Learn about reverse SSH tunnels, i.e., SSH port forwarding, and issues that you might encounter when trying to use them.</p>
<p>The post <a rel="NOFOLLOW" href="https://feeds.feedblitz.com/~/919939007/0/baeldung/linux~Resolving-the-Reverse-Tunnel-Remote-port-forwarding-failed-for-listen-port-SSH-Error">Resolving the Reverse Tunnel <em>Remote port forwarding failed for listen port</em> SSH Error</a> first appeared on <a rel="NOFOLLOW" href="https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/919939007/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/919939007/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2025%2f01%2fLinux-Administration-Featured-Image-03-1024x536.jpg"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/919939007/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/919939007/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/919939007/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/ssh-scp-password-subprocess">Sending a Password via SSH or SCP Using subprocess.Popen in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/selinux">What Is SELinux?</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/packet-drops-troubleshooting">Finding Out Why a Network Interface is Dropping Packets</a></li></ul>&#160;</div>]]>
</description>
										<content:encoded><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-1024x536.jpg" class="webfeedsFeaturedVisual wp-post-image" alt="" style="float: left; margin-right: 5px;" decoding="async" loading="lazy" srcset="https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-1024x536.jpg 1024w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-300x157.jpg 300w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-768x402.jpg 768w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-100x52.jpg 100w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-600x314.jpg 600w, https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03.jpg 1200w" sizes="auto, (max-width: 580px) 100vw, 580px" /><h2 id="bd-introduction" data-id="introduction">1. Introduction</h2>
<div class="bd-anchor" id="introduction"></div>
<p>As the ubiquitous method to facilitate and secure connections between remote systems, the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/cs/ssh-intro">Secure Shell (SSH)</a> protocol supports multiple ways to handle point-to-point communication. Furthermore, one of the most useful features in this regard is remote port forwarding. In one of its basic forms, the latter enables going through Network Address Translation (NAT) filters and similar firewall restrictions.</p>
<p>In this tutorial, <strong>we talk about remote SSH port forwarding or reverse tunnels</strong>. First, we understand the problems that these features solve. Next, we go through the prerequisites and process of establishing a reverse tunnel. After that, we explore problems that might result from different aspects of SSH remote port forwarding, i.e., reverse tunnels.</p>
<p>We tested the code in this tutorial on Debian 12 (Bookworm) with GNU Bash 5.2.15. Unless otherwise specified, it should work in most POSIX-compliant environments.</p>
<h2 id="bd-remote-or-reverse-tunneling-and-client-protection" data-id="remote-or-reverse-tunneling-and-client-protection">2. Remote or Reverse Tunneling and Client Protection</h2>
<div class="bd-anchor" id="remote-or-reverse-tunneling-and-client-protection"></div>
<p>To begin with, let&#8217;s understand the concept of remote or reverse tunneling. In essence, the idea is to use SSH from a protected client to establish a permanent tunnel to a remote server, enabling traffic in both directions.</p>
<p><strong>There can be different kinds of client protection</strong> in this context:</p>
<ul>
<li>behind a router</li>
<li>NAT</li>
<li>firewall</li>
<li>Virtual Private Network (VPN)</li>
</ul>
<p>Basically, <strong>we can use this technique to gain access to the client side and manage any network resource that the client can control, all from a remote server that normally can&#8217;t directly connect to that client</strong>. In addition, local client ports can be forwarded to the remote server side.</p>
<p>This idea has two main names: <em>remote tunneling</em> and <em>reverse tunneling</em>, depending on the way it&#8217;s applied. In all cases, it provides <em>port forwarding</em>.</p>
<h2 id="bd-ssh-remote-port-forwarding" data-id="ssh-remote-port-forwarding">3. SSH Remote Port Forwarding</h2>
<div class="bd-anchor" id="ssh-remote-port-forwarding"></div>
<p>Remote port forwarding or reverse tunneling is a special case of remote tunneling, which exposes a local service or application to a remote server even when the local machine is protected in any of the ways mentioned earlier.</p>
<h3 id="bd-1-requirements" data-id="1-requirements">3.1. Requirements</h3>
<div class="bd-anchor" id="1-requirements"></div>
<p>To employ remote port forwarding with SSH, we need several prerequisites to be in place:</p>
<ul>
<li>SSH client on the client machine</li>
<li>SSH server on the server machine</li>
<li>network connectivity and visibility from client to server</li>
</ul>
<p>In this case, we use <em>xost</em> as the server.</p>
<h3 id="bd-2-ssh-client--r-flag" data-id="2-ssh-client--r-flag">3.2. SSH Client <em>-R</em> Flag</h3>
<div class="bd-anchor" id="2-ssh-client--r-flag"></div>
<p>When it comes to most <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/ssh-key-types-convert-ppk#openssh-and-putty">SSH clients</a>, <strong>the way to perform remote port forwarding is the <em>-R</em> flag, which takes a colon-separated list as its value</strong>. The latter contains four fields:</p>
<ol>
<li>(optional, default <em>localhost</em>) <em>remote_server_bind_address</em>: address to use on the remote server for the reverse tunnel</li>
<li><em>remote_server_bind_port</em>: port to use on the remote server for the reverse tunnel connection</li>
<li><em>client_network_host</em>: address that the client has network access to</li>
<li><em>client_network_host_port</em>: port at <em>client_network_host</em></li>
</ol>
<p>The SSH argument is the remote server that should be the opposite end of the tunnel.</p>
<p>In essence, <strong>connections to <em>[remote_server_bind_address]</em>:<em>remote_server_bind_port</em> on the remote server (provided as an argument) should be forwarded to the client network at <em>client_network_host</em>:<em>local_port</em></strong>. Notably, both address-port combinations can be sockets instead. Without a <em>remote_server_bind_address</em>, connections only work for <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/cs/127-0-0-1-vs-localhost">loopback interfaces</a>, while an empty <em>bind_address</em> or <em>*</em> specifies all interfaces.</p>
<p>Notably, <strong>the <em>-R</em> flag is often used with <em>-N</em></strong> to prevent any command from running and just establish the port forwarding (reverse tunnel) for future use.</p>
<h3 id="bd-3-examples" data-id="3-examples">3.3. Examples</h3>
<div class="bd-anchor" id="3-examples"></div>
<p>To understand the idea of reverse tunnels, let&#8217;s see some examples.</p>
<p>First, we establish the basic template:</p>
<pre><code class="language-shell">ssh -N -R [&lt;remote_server_bind_address&gt;]:&lt;remote_server_bind_port&gt;:&lt;client_network_host&gt;:&lt;client_network_host_port&gt; [&lt;remote_server_user&gt;@]&lt;remote_server_address&gt;</code></pre>
<p>Now, let&#8217;s show a fairly simple case:</p>
<pre><code class="language-shell">$ ssh -N -R localhost:8888:localhost:7777 uxer@xost</code></pre>
<p>Here, <strong>any connection to the remote server <em>xost</em> on its <em>localhost</em> interface at port <em>8888</em> goes to the client that ran the command (second <em>localhost</em>) at port <em>7777</em></strong>.</p>
<p>Of course, the first <em>localhost</em> doesn&#8217;t need to be explicit, as it&#8217;s the default:</p>
<pre><code class="language-shell">$ ssh -N -R 8888:localhost:7777 uxer@xost</code></pre>
<p>This command should function in more or less the same way. Naturally, if we want to bind to another network interface on the remote server, we can do so via another address or a socket.</p>
<p>Lastly, when connecting to a client network machine (instead of locally to the client), we can specify the desired address and port in place of <em>client_network_host</em>:<em>client_network_host_port</em>:</p>
<pre><code class="language-shell">$ ssh -N -R 8888:&lt;client_network_host&gt;:&lt;client_network_host_port&gt; uxer@xost</code></pre>
<p>So, let&#8217;s do that:</p>
<pre><code class="language-shell">$ ssh -N -R 8888:192.168.6.66:6666 uxer@xost</code></pre>
<p>Thus, <strong>any connection to the loopback interface on <em>xost</em> at port <em>8888</em> goes to <em>192.168.6.66</em> at port <em>6666</em> in the client network</strong>.</p>
<h2 id="bd-forwarding-port-issues" data-id="forwarding-port-issues">4. Forwarding Port Issues</h2>
<div class="bd-anchor" id="forwarding-port-issues"></div>
<p>Different problems can arise during port forwarding and remote tunneling with SSH. Let&#8217;s explore some of them and see potential solutions.</p>
<h3 id="bd-1-syntax-problems" data-id="1-syntax-problems">4.1. Syntax Problems</h3>
<div class="bd-anchor" id="1-syntax-problems"></div>
<p>It may take some time to understand the exact way each part of the <em>-R</em> flag value works. Considering this, we might end up mixing different addresses or ports, which can lead to unexpected errors and issues with accessibility.</p>
<p>For instance, <strong>if we use the <em>remote_server_bind_address</em> in the place of the <em>client_network_host</em>, we might see an error about an <em>unknown host</em></strong>:</p>
<pre><code class="language-shell">$ ssh -N -R 8888:xost:7777 uxer@xost
[...]
connect_to xost port 7777: failed</code></pre>
<p>Furthermore, as with other networking tools, specifying exact parameters is paramount when constructing the SSH command for port forwarding. <strong>A missing or misplaced colon or bad ports and addresses can be hard to troubleshoot</strong>.</p>
<p>Another example involves the forced use of IPv6 addresses, which might need to be enclosed in square brackets.</p>
<p>To avoid such problems, <strong>checking the syntax and values multiple times is critical</strong>.</p>
<h3 id="bd-2-privileged-ports" data-id="2-privileged-ports">4.2. Privileged Ports</h3>
<div class="bd-anchor" id="2-privileged-ports"></div>
<p>Reverse tunnels are a common occurrence within environments without <em>root</em> access.</p>
<p>To that end, <strong>attempting to use a port number below <em>1024</em> can result in permissions issues</strong>:</p>
<pre><code class="language-shell">$ ssh -N -R 1000:xost:1100 uxer@xost
[...]
bind: Permission denied</code></pre>
<p>The <em>Permission denied</em> error can be especially hard to diagnose when dealing with different machines, networks, passwords, and keys within the context of SSH. However, it usually just means that <em>uxer</em> is unable to bind port <em>1000</em> due to insufficient permissions.</p>
<p>Although the errors may be different, the remedy is the same: <strong>unless strictly necessary to do otherwise, it&#8217;s best to use port numbers above <em>1023</em></strong>.</p>
<h3 id="bd-3-busy-ports" data-id="3-busy-ports">4.3. Busy Ports</h3>
<div class="bd-anchor" id="3-busy-ports"></div>
<p>Commonly, when a forwarding fails, <strong>we see a fairly ambiguous error</strong>:</p>
<pre><code class="language-shell">$ ssh -N -R *:8080:localhost:6666 uxer@xost
[...]
Warning: remote port forwarding failed for listen port 8080</code></pre>
<p>Yet, one very common reason for issues when using reverse tunnels is a busy port.</p>
<p>In fact, checking whether a tunnel is up on the remote side might prove insightful even when there are no errors:</p>
<pre><code class="language-shell">$ ss -tnlp | grep :8080
LISTEN 0      1            0.0.0.0:8080       0.0.0.0:*    users:(("nc",pid=10666,fd=3))</code></pre>
<p>In this case, we see a <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/netcat-command"><em>netcat</em></a> (<em>nc</em>) command uses the port we need for the tunnel. Thus, <strong>we can either stop that process or use a different port or IP version</strong>. However, the latter could happen unintentionally.</p>
<h3 id="bd-4-wrong-internet-protocol-ip-version" data-id="4-wrong-internet-protocol-ip-version">4.4. Wrong Internet Protocol (IP) Version</h3>
<div class="bd-anchor" id="4-wrong-internet-protocol-ip-version"></div>
<p>On top of the tunnel state, <strong>we can also see tunnel configurations by checking the remote server</strong>:</p>
<pre><code class="language-shell">$ ss -tnlp | grep :8080
LISTEN 0      1            0.0.0.0:8080       0.0.0.0:*    users:(("nc",pid=10666,fd=3))                                 
LISTEN 0      128            [::1]:8080          [::]:*    users:(("sshd",pid=10667,fd=5))</code></pre>
<p>In this case, both <em>nc</em> and <em>sshd</em> listen on <em>8080</em> after we&#8217;ve established a reverse tunnel without errors. However, <em>sshd</em> uses <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/ipv6-autoconfiguration-disable#introduction">IP version 6</a> (IPv6), while <em>nc</em> works over IPv4 in this case. This might be desired behavior, but <strong>it could also be misleading when a successfully established tunnel is seemingly inaccessible through a given IP version</strong>.</p>
<h3 id="bd-5-filtered-port" data-id="5-filtered-port">4.5. Filtered Port</h3>
<div class="bd-anchor" id="5-filtered-port"></div>
<p>In specific cases, the <em>remote_server_bind_port</em> might end up being filtered. Let&#8217;s see what this may end up meaning for potential clients.</p>
<p>First, we establish a reverse tunnel:</p>
<pre><code class="language-shell">$ ssh -N -R *:12345:localhost:6666 uxer@xost</code></pre>
<p>Now, from another machine, we attempt to send a request to <em>xost:12345</em>:</p>
<pre><code class="language-shell">$ curl xost:12345
curl: (7) Failed to connect to xost port 12345: Couldn't connect to server</code></pre>
<p>At this point, we might doubt that the initial tunnel is working. However, it could be the case that port <em>12345</em> is inaccessible from outside the remote server <em>xost</em>. For example, <strong>we may need to configure a <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/ssh-access-using-iptables">firewall</a> or router</strong>. Yet, there are some ports that might even be filtered by the Internet Service Provider (ISP).</p>
<p>As already mentioned, <strong>reverse tunnels solve such filtering issues automatically on the client side for the client itself, but not beyond</strong>.</p>
<h3 id="bd-6-remote-ssh-server-configuration" data-id="6-remote-ssh-server-configuration">4.6. Remote SSH Server Configuration</h3>
<div class="bd-anchor" id="6-remote-ssh-server-configuration"></div>
<p>One rare but possible reason for a reverse tunnel setup problem is an incorrect or unexpected configuration of the SSH server.</p>
<p>For instance, <strong>there are a couple of key settings for port forwarding</strong>:</p>
<ul>
<li><em>AllowTcpForwarding</em></li>
<li><em>GatewayPorts</em></li>
</ul>
<p>So, with the value of <em>AllowTcpForwarding</em>, we can enable (<em>yes</em>, default) or disable (<em>no</em>) TCP port forwarding, but we can also force only reverse and no local tunnels (<em>remote</em>) or only local instead of reverse tunnels (<em>local</em>) to be accepted.</p>
<p>On the other hand, <em>GatewayPorts</em> can be enabled (<em>yes</em>), disabled (<em>no</em>, default), or <em>clientspecified</em>. The latter enables the SSH client to specify the address. <strong>Since the default is <em>no</em>, ports usually only bind to <em>127.0.0.1</em> (loopback) on the remote host</strong>.</p>
<p>In case forwarding is disabled or a forbidden bind address is requested, we see the generic error:</p>
<pre><code class="language-shell">$ ssh -N -R 0.0.0.0:8080:localhost:22 uxer@xost
[...]
Warning: remote port forwarding failed for listen port 8080</code></pre>
<p>Still, if we know about these parameters, checking and correcting them in the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/ssh-multiple-ports#ssh-server-setup"><em>sshd_config</em></a> file (with a potential service restart) might solve some issues.</p>
<h2 id="bd-summary" data-id="summary">5. Summary</h2>
<div class="bd-anchor" id="summary"></div>
<p>In this article, we talked about reverse SSH tunnels, i.e., SSH port forwarding, and issues that we might encounter when trying to use them.</p>
<p>In conclusion, although a reverse SSH tunnel is a fairly complex feature with many dependencies, by performing checks and diagnostics, we can deduce the correct reason for any failures and successfully set up stable forwarding.</p><p>The post <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/ssh-reverse-tunnel-port-forward-errors">Resolving the Reverse Tunnel <em>Remote port forwarding failed for listen port</em> SSH Error</a> first appeared on <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><Img align="left" border="0" height="1" width="1" alt="" style="border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;" hspace="0" src="https://feeds.feedblitz.com/~/i/919939007/0/baeldung/linux">
<div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/919939007/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/919939007/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2025%2f01%2fLinux-Administration-Featured-Image-03-1024x536.jpg"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/919939007/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/919939007/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/919939007/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/ssh-scp-password-subprocess">Sending a Password via SSH or SCP Using subprocess.Popen in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/selinux">What Is SELinux?</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/packet-drops-troubleshooting">Finding Out Why a Network Interface is Dropping Packets</a></li></ul>&#160;</div>]]>
</content:encoded>
					
		
		
		<webfeeds:featuredImage>https://www.baeldung.com/wp-content/uploads/sites/2/2025/01/Linux-Administration-Featured-Image-03-150x150.jpg</webfeeds:featuredImage></item>
<item>
<feedburner:origLink>https://www.baeldung.com/linux/makefile-if-else-conditionals</feedburner:origLink>
		<title>Using if-else Conditionals in a Makefile</title>
		<link>https://feeds.feedblitz.com/~/919796387/0/baeldung/linux~Using-ifelse-Conditionals-in-a-Makefile</link>
		
		<dc:creator><![CDATA[baeldung]]></dc:creator>
		<pubDate>Fri, 06 Jun 2025 09:36:00 +0000</pubDate>
				<category><![CDATA[Building]]></category>
		<category><![CDATA[make]]></category>
		<category><![CDATA[popular]]></category>
		<guid isPermaLink="false">https://www.baeldung.com/linux/makefile-if-else-conditionals</guid>
					<description><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-6-1024x536.png" class="webfeedsFeaturedVisual wp-post-image" alt="" style="max-width:100% !important;height:auto !important;float: left; margin-right: 5px;" loading="lazy" /><p>Learn how to use if-else conditional statements inside of Makefiles.</p>
<p>The post <a rel="NOFOLLOW" href="https://feeds.feedblitz.com/~/919796387/0/baeldung/linux~Using-ifelse-Conditionals-in-a-Makefile">Using <em>if-else</em> Conditionals in a Makefile</a> first appeared on <a rel="NOFOLLOW" href="https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/919796387/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/919796387/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2021%2f09%2fFeatured-Linux-6-1024x536.png"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/919796387/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/919796387/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/919796387/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/c-access-struct-other-file">Accessing a Defined struct in Another Source File in C</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/profiling-c-cpp-code">How to Profile C++ Code Running on Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/google-photos-upload-many-images">How to Upload Many Images to Google Photos in Linux</a></li></ul>&#160;</div>]]>
</description>
										<content:encoded><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-6-1024x536.png" class="webfeedsFeaturedVisual wp-post-image" alt="" style="float: left; margin-right: 5px;" decoding="async" loading="lazy" srcset="https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-6-1024x536.png 1024w, https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-6-300x157.png 300w, https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-6-768x402.png 768w, https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-6-100x52.png 100w, https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-6.png 1200w" sizes="auto, (max-width: 580px) 100vw, 580px" /><h2 id="bd-introduction" data-id="introduction">1. Introduction</h2>
<div class="bd-anchor" id="introduction"></div>
<p>In Linux, Makefiles are powerful tools for automating build processes, but their syntax can sometimes be tricky, especially when it comes to conditional statements. The syntax differs significantly from traditional programming languages, leading to confusion and errors. A common issue is attempting to use<em> if-else</em> statements directly within recipe lines, which doesn&#8217;t work as expected.</p>
<p>In this tutorial, we&#8217;ll explore the syntax and examples of using <em>if-else</em> blocks in <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/make-makefile-escape-characters">Makefiles</a>.</p>
<h2 id="bd-conditional-directives-in-makefiles" data-id="conditional-directives-in-makefiles">2. Conditional Directives in Makefiles</h2>
<div class="bd-anchor" id="conditional-directives-in-makefiles"></div>
<p>We use several kinds of conditional directives in a Makefile:</p>
<ul>
<li><em>ifeq</em> &#8211; tests if two strings are equal</li>
<li><em>ifneq</em> &#8211; tests if two strings aren&#8217;t equal</li>
<li><em>ifdef</em> &#8211; the code tests if the variable definition is available</li>
<li><em>ifndef</em> &#8211; the condition tests if a variable definition isn&#8217;t available</li>
</ul>
<p>We&#8217;ll explore examples of the above conditionals in the subsequent sections.</p>
<h2 id="bd-basic-syntax" data-id="basic-syntax">3. Basic Syntax</h2>
<div class="bd-anchor" id="basic-syntax"></div>
<p>Let&#8217;s take a look at the basic syntax for an <em>if-else</em> statement in a Makefile:</p>
<pre><code class="language-c">ifeq (condition)
    # Commands if condition is true
else
    # Commands if condition is false
endif</code></pre>
<p>The above snippet is a basic skeleton on the conditional statements in a Makefile. We&#8217;ll explore the above directives using various examples.</p>
<h3 id="bd-1-testing-if-two-strings-are-equal" data-id="1-testing-if-two-strings-are-equal">3.1. Testing if Two Strings Are Equal</h3>
<div class="bd-anchor" id="1-testing-if-two-strings-are-equal"></div>
<p>The <em>ifeq</em> directive tests if two strings are equal:</p>
<pre><code class="language-c">MODE = debug
ifeq ($(MODE),debug)
    CFLAGS = -g -O0 -DDEBUG
else
    CFLAGS = -O2
endif</code></pre>
<p>In this example, if <em>MODE</em> equals &#8220;debug&#8221;, then <em>CFLAGS</em> takes on debugging flags; otherwise, it takes on optimization flags.</p>
<h3 id="bd-2testing-if-two-strings-arent-equal" data-id="2testing-if-two-strings-arent-equal">3.2. Testing if Two Strings Aren&#8217;t Equal</h3>
<div class="bd-anchor" id="2testing-if-two-strings-arent-equal"></div>
<p>The <em>ifneq</em> directive tests if two strings aren&#8217;t equal to each other:</p>
<pre><code class="language-c">OS = linux
ifneq ($(OS),windows)
    RM = rm -f
else
    RM = del
endif</code></pre>
<p>Here, if <em>OS</em> isn&#8217;t &#8220;windows&#8221;, the <em>RM</em> command takes the <em>Unix</em> version; otherwise, it takes the <em>Windows</em> command.</p>
<h3 id="bd-3-testing-if-a-variable-is-defined" data-id="3-testing-if-a-variable-is-defined">3.3. Testing if a Variable Is Defined</h3>
<div class="bd-anchor" id="3-testing-if-a-variable-is-defined"></div>
<p>The <em>ifdef</em> directive tests whether or not a variable is defined:</p>
<pre><code class="language-c">ifdef DEBUG
    CFLAGS += -g -DDEBUG
endif</code></pre>
<p>In this example, if <em>DEBUG</em> is defined (regardless of its value), the directive adds debugging flags to <em>CFLAGS</em>.</p>
<h3 id="bd-4-testing-if-a-variable-isnt-defined" data-id="4-testing-if-a-variable-isnt-defined">3.4. Testing if a Variable Isn&#8217;t Defined</h3>
<div class="bd-anchor" id="4-testing-if-a-variable-isnt-defined"></div>
<p>The <em>ifndef</em> directive tests if a variable isn&#8217;t defined:</p>
<pre><code class="language-c">ifndef CC
    CC = gcc
endif</code></pre>
<p>In this case, if the <em>CC</em> variable isn&#8217;t defined, the Makefile assigns &#8220;gcc&#8221; as its default value. We commonly use this pattern in Makefiles to provide default values that users can override.</p>
<h2 id="bd-makefile-examples" data-id="makefile-examples">4. Makefile Examples</h2>
<div class="bd-anchor" id="makefile-examples"></div>
<p>In the following sections, we&#8217;ll explore examples that combine various aspects of the above conditionals into a Makefile.</p>
<h3 id="bd-1-checking-the-operating-system" data-id="1-checking-the-operating-system">4.1. Checking the Operating System</h3>
<div class="bd-anchor" id="1-checking-the-operating-system"></div>
<p>Here&#8217;s an example that demonstrates the usage of conditional statements for handling different operating systems:</p>
<pre><code class="language-c"># Define the default target
all: hello
# Check the operating system
ifeq ($(OS),Windows_NT)
    DETECTED_OS := Windows
else
    DETECTED_OS := $(shell uname -s)
endif
# Target that uses the detected OS
hello:
    @echo "Hello from $(DETECTED_OS)"
    @echo "This is a Make example"
# Conditional target behavior
ifeq ($(DETECTED_OS),Windows)
    install:
        @echo "Installing on Windows..."
else ifeq ($(DETECTED_OS),Darwin)
    install:
        @echo "Installing on macOS..."
else
    install:
        @echo "Installing on Linux or other OS..."
endif</code></pre>
<p>In the above example, we initialize the the variable <em>DETECTED_OS</em> based on environment variable <em>OS</em>. The details of the above steps are:</p>
<ul>
<li><em>ifeq ($(OS),Windows_NT)</em> checks if the environment variable <em>OS</em> equals &#8220;Windows_NT&#8221; (which is automatically set on Windows systems).</li>
<li>If <em>true</em>, it sets <em>DETECTED_OS</em> to &#8220;Windows&#8221;.</li>
<li>Otherwise, it runs the Unix command <em>uname -s</em> (which returns the kernel name) and assigns that output to <em>DETECTED_OS</em>.</li>
<li>On macOS, <em>uname -s</em> returns &#8220;Darwin&#8221;; while on Linux, it returns &#8220;Linux&#8221;.</li>
</ul>
<p>Subsequently, we use the variable <em>DETECTED_OS </em>for installing the appropriate software based on the operating system.</p>
<h3 id="bd-2-conditional-expressions" data-id="2-conditional-expressions">4.2. Conditional Expressions</h3>
<div class="bd-anchor" id="2-conditional-expressions"></div>
<p>If we need conditional logic within a recipe, we use shell <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/makefile-conditional-variable-assignment-operator">conditionals</a>:</p>
<pre><code class="language-c">check_file:
    @if [ -f "somefile.txt" ]; then \
        echo "File exists"; \
    else \
        echo "File does not exist"; \
    fi</code></pre>
<p>Let&#8217;s break down the above code:</p>
<ul>
<li><em>check_file</em> defines a target named &#8220;check_file&#8221;.</li>
<li>@ at the beginning suppresses <em>Make</em> from printing the command before executing it (for cleaner output).</li>
<li><em>if [ -f &#8220;somefile.txt&#8221; ]; then \</em> starts a shell conditional statement.</li>
<li><em>echo &#8220;File exists&#8221;</em>; prints &#8220;File exists&#8221; if the file exists.</li>
<li><em>echo &#8220;File does not exist&#8221;</em> prints &#8220;File does not exist&#8221; if the file doesn&#8217;t exist.</li>
</ul>
<p>The entire command is actually a single shell command (a Bash <em>if</em>-statement). In a proper Makefile, this would typically be formatted with proper indentation for readability. <strong>In above example, we&#8217;re executing the command prefixed with <em>@</em> in a Bash subshell.
<br>
</strong></p>
<h3 id="bd-3-inline-conditionals" data-id="3-inline-conditionals">4.3. Inline Conditionals</h3>
<div class="bd-anchor" id="3-inline-conditionals"></div>
<p>We can also use <em>if</em> conditionals for inline functions:</p>
<pre><code class="language-c"># Using $(if) function
MESSAGE := $(if $(DEBUG),Debugging mode, Release mode)
# Using computed variable names
MODE := debug
COMPILER_$(MODE) := gcc -g
COMPILER_release := gcc -O2
compile:
    @echo "Using compiler: $(COMPILER_$(MODE))"</code></pre>
<p>The above example illustrates inline functions using <em>$(if &#8230;) </em>syntax:</p>
<ul>
<li>If the <em>DEBUG</em> variable is defined, the Makefile sets the <em>MESSAGE</em> variable to &#8220;Debugging mode&#8221;</li>
<li>Set the <em>MESSAGE</em> variable to &#8220;Release mode&#8221; if variable <em>DEBUG</em> isn&#8217;t set</li>
</ul>
<p>We also use a computed variable based on the value of the <em>MODE</em> variable. <strong>This allows us to initialize the appropriate compiler parameters and use it for compiling the program.</strong></p>
<h3 id="bd-4-using-the-shell-command" data-id="4-using-the-shell-command">4.4. Using the <em>shell</em> Command</h3>
<div class="bd-anchor" id="4-using-the-shell-command"></div>
<p>We can combine the <em>shell</em> command in Makefile to check the required disk space:</p>
<pre><code class="language-c"># define the minimum disk space required
MIN_SPACE_MB=1000
valid-env:
    @echo "validating build env ..."
ifeq ($(shell if [ $$(df -m . | tail -1 | awk '{print $$4}') -lt $(MIN_SPACE_MB) ];then echo 1; else echo 0; fi),1)
    $(error Insufficient disk space. At least $(MIN_SPACE_MB)MB required)
endif</code></pre>
<p>The above example illustrates a complex example of checking the environment for available disk space. The steps are:</p>
<ul>
<li>Sset the <em>MIN_SPACE_MB</em> variable to &#8220;<em>1000</em>&#8221; assuming the required disk space is <em>1000 MB</em></li>
<li>The command <em>df -m . | tail -1 | awk &#8216;{print $$4}</em> evaluates the disk space on the current file system</li>
<li>Compare the output of the above command with the expected disk size (<em>MIN_SPACE_MB</em>) and echo the result of <em>0</em> or <em>1</em></li>
<li>We compare the result with <em>1</em> to determine if disk space is available</li>
<li>In case of error, the <em>$(error &#8230;)</em> block prints the error</li>
</ul>
<p>One important idea of note is that <strong>in Makefiles, we can&#8217;t directly split a shell command across multiple lines</strong>. A separate shell instance typically executes every recipe. <strong>As a result, the variables defined in one line aren&#8217;t available in the next.</strong> This leads to variable scope limitations.</p>
<h2 id="bd-conclusion" data-id="conclusion">5. Conclusion</h2>
<div class="bd-anchor" id="conclusion"></div>
<p>In this article, we learned various ways of using <em>if-else</em> blocks in a Makefile. Understanding conditional statements in Makefiles is essential for creating versatile and maintainable build systems. By properly implementing <em>if-else</em> logic in our Makefiles, we can ensure our build processes are robust and flexible across different platforms and configurations.</p><p>The post <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/makefile-if-else-conditionals">Using <em>if-else</em> Conditionals in a Makefile</a> first appeared on <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><Img align="left" border="0" height="1" width="1" alt="" style="border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;" hspace="0" src="https://feeds.feedblitz.com/~/i/919796387/0/baeldung/linux">
<div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/919796387/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/919796387/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2021%2f09%2fFeatured-Linux-6-1024x536.png"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/919796387/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/919796387/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/919796387/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/c-access-struct-other-file">Accessing a Defined struct in Another Source File in C</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/profiling-c-cpp-code">How to Profile C++ Code Running on Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/google-photos-upload-many-images">How to Upload Many Images to Google Photos in Linux</a></li></ul>&#160;</div>]]>
</content:encoded>
					
		
		
		<webfeeds:featuredImage>https://www.baeldung.com/wp-content/uploads/sites/2/2021/09/Featured-Linux-6-150x150.png</webfeeds:featuredImage></item>
<item>
<feedburner:origLink>https://www.baeldung.com/linux/usb-fix-read-only-mounts</feedburner:origLink>
		<title>How to Fix USB Sticks Mounted as Read-Only</title>
		<link>https://feeds.feedblitz.com/~/919792943/0/baeldung/linux~How-to-Fix-USB-Sticks-Mounted-as-ReadOnly</link>
		
		<dc:creator><![CDATA[Samuel Njuguna Karanja]]></dc:creator>
		<pubDate>Fri, 06 Jun 2025 07:56:19 +0000</pubDate>
				<category><![CDATA[Administration]]></category>
		<category><![CDATA[Filesystems]]></category>
		<category><![CDATA[Hardware]]></category>
		<category><![CDATA[mount]]></category>
		<category><![CDATA[USB]]></category>
		<guid isPermaLink="false">https://www.baeldung.com/linux/usb-fix-read-only-mounts</guid>
					<description><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2019/11/Featured-Linux-4-k.jpg" class="webfeedsFeaturedVisual wp-post-image" alt="" style="max-width:100% !important;height:auto !important;float: left; margin-right: 5px;" loading="lazy" /><p>Learn how to identify and diagnose undesired USB flash drive read-only mounts.</p>
<p>The post <a rel="NOFOLLOW" href="https://feeds.feedblitz.com/~/919792943/0/baeldung/linux~How-to-Fix-USB-Sticks-Mounted-as-ReadOnly">How to Fix USB Sticks Mounted as Read-Only</a> first appeared on <a rel="NOFOLLOW" href="https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/919792943/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/919792943/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2019%2f11%2fFeatured-Linux-4-k.jpg"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/919792943/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/919792943/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/919792943/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/mount-smb-security-permission-denied">SMB mount Security Modes and Fixing mount error(13): Permission denied in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/selinux">What Is SELinux?</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/cron-logograte-delete-log-files">Removing Log Files with a Cron Job in Linux</a></li></ul>&#160;</div>]]>
</description>
										<content:encoded><![CDATA[<img src="https://www.baeldung.com/wp-content/uploads/sites/2/2019/11/Featured-Linux-4-k.jpg" class="webfeedsFeaturedVisual wp-post-image" alt="" style="float: left; margin-right: 5px;" decoding="async" loading="lazy" srcset="https://www.baeldung.com/wp-content/uploads/sites/2/2019/11/Featured-Linux-4-k.jpg 952w, https://www.baeldung.com/wp-content/uploads/sites/2/2019/11/Featured-Linux-4-k-300x157.jpg 300w, https://www.baeldung.com/wp-content/uploads/sites/2/2019/11/Featured-Linux-4-k-768x402.jpg 768w, https://www.baeldung.com/wp-content/uploads/sites/2/2019/11/Featured-Linux-4-k-100x52.jpg 100w" sizes="auto, (max-width: 580px) 100vw, 580px" /><h2 id="bd-overview" data-id="overview">1. Overview</h2>
<div class="bd-anchor" id="overview"></div>
<p>We commonly use USB flash drives, also known as sticks, to transfer files, store backups, or create bootable media. As a Linux user, there are times we may encounter an issue where the USB stick mounts as read-only, preventing us from editing, copying, or deleting files. This might happen due to filesystem errors, hardware failure, improper unmounting, or permission issues.</p>
<p>In this tutorial, we&#8217;ll explain why USB sticks could mount as read-only, how to identify this as an issue, and ways to resolve it.</p>
<h2 id="bd-why-usb-sticks-mount-as-read-only" data-id="why-usb-sticks-mount-as-read-only">2. Why USB Sticks Mount as Read-Only</h2>
<div class="bd-anchor" id="why-usb-sticks-mount-as-read-only"></div>
<p>When we insert a USB stick into a Linux system, the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/kernel-source-code-headers#the-linux-kernel">kernel</a> usually detects and mounts it automatically. Often, any desktop environment also enables quick access to the mount. In some cases, <strong>if something goes wrong in the process or the system detects any problems, it may mount the USB stick as read-only as the only option, or to prevent further damage</strong>.</p>
<p>In this section, we look at some of the most common causes for this.</p>
<h3 id="bd-1-filesystem-errors" data-id="1-filesystem-errors">2.1. Filesystem Errors</h3>
<div class="bd-anchor" id="1-filesystem-errors"></div>
<p>Linux relies on <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/fhs-filesystem-hierarchy-standard-structure">filesystem</a> drivers and integrity checks to mount drives safely. <strong>If the kernel detects any problems, it may mount the USB stick as read-only to prevent further damage</strong>.</p>
<p>This often occurs when we haven&#8217;t properly ejected that USB stick in the past.</p>
<h3 id="bd-2-mount-options" data-id="2-mount-options">2.2. Mount Options</h3>
<div class="bd-anchor" id="2-mount-options"></div>
<p>When the system or a user mounts the USB stick using the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/mount-permissions-remount-writable#mount-readability-and-writability"><em>ro</em> option</a>, either manually or through <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/etc-fstab-mount-options#etcfstab-filesystem-table"><em>/etc/fstab</em></a>, the mount is read-only.</p>
<p>While this is an implicit configuration, it may be an unwanted side-effect of other processes.</p>
<h3 id="bd-3-hardware-settings" data-id="3-hardware-settings">2.3. Hardware Settings</h3>
<div class="bd-anchor" id="3-hardware-settings"></div>
<p>Some USB sticks have a physical write-protect switch as a feature. <strong>When enabled, the storage hardware itself prevents any write operation</strong>.</p>
<p>The Linux kernel detects this flag and mounts the device in read-only mode.</p>
<h3 id="bd-4-failing-or-low-quality-usb-stick" data-id="4-failing-or-low-quality-usb-stick">2.4. Failing or Low-Quality USB Stick</h3>
<div class="bd-anchor" id="4-failing-or-low-quality-usb-stick"></div>
<p>When the memory of a USB stick starts to degrade, the internal controller may force the device into read-only mode to prevent data loss.</p>
<p>This is common for flash storage, as any failure there usually indicates further issues are imminent and, unlike some magnetic storage failures, flash burnouts are final.</p>
<h2 id="bd-how-to-check-if-a-usb-stick-is-mounted-as-read-only" data-id="how-to-check-if-a-usb-stick-is-mounted-as-read-only">3. How to Check if a USB Stick Is Mounted as Read-Only</h2>
<div class="bd-anchor" id="how-to-check-if-a-usb-stick-is-mounted-as-read-only"></div>
<p>To remedy an undesired read-only USB stick mount, we need to check whether that is indeed the case.</p>
<h3 id="bd-1-using-the-mount-command" data-id="1-using-the-mount-command">3.1. Using the <em>mount</em> Command</h3>
<div class="bd-anchor" id="1-using-the-mount-command"></div>
<p>The <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/mount-unmount-filesystems"><em>mount</em></a> command shows how the system currently mounts storage devices, including their mount options. We can use it to verify whether it has mounted the USB stick in read-only (<em>ro</em>) or read-write (<em>rw</em>) mode:</p>
<pre><code class="language-bash">$ mount | grep /dev/sd
/dev/sdb5 on / type ext4 (rw,relatime,errors=remount-ro)
/dev/sdb5 on /var/snap/firefox/common/host-hunspell type ext4 (ro,noexec,noatime,errors=remount-ro)
/dev/sda1 on /mnt/MyUSB type exfat (ro,relatime,fmask=0022,dmask=0022,iocharset=utf8)</code></pre>
<p>This command filters the list of mounted devices and only shows entries related to block storage, such as USB, solid state, and hard disk drives. <strong>In the output, <em>/dev/sda1</em> represents the USB stick, which contains <em>ro</em> in its options, indicating that the system mounted it in read-only mode</strong>. On the same line, <em>/mnt/MyUSB</em> represents the mounting point.</p>
<h3 id="bd-2-attempt-usb-stick-write" data-id="2-attempt-usb-stick-write">3.2. Attempt USB Stick Write</h3>
<div class="bd-anchor" id="2-attempt-usb-stick-write"></div>
<p>To diagnose, we can try to create a file on the USB storage:</p>
<pre><code class="language-bash">$ touch /mnt/MyUSB/contacts.txt
touch: cannot touch 'contacts.txt': Read-only file system</code></pre>
<p>As expected, the output indicates the USB stick is mounted in read-only mode.</p>
<h2 id="bd-fixing-usb-read-only-issues" data-id="fixing-usb-read-only-issues">4. Fixing USB Read-Only Issues</h2>
<div class="bd-anchor" id="fixing-usb-read-only-issues"></div>
<p>Once we confirm the USB stick is mounted or always mounts as read-only, we can attempt to troubleshoot and resolve the issue.</p>
<h3 id="bd-1-remount-the-usb-stick-as-read-write" data-id="1-remount-the-usb-stick-as-read-write">4.1. Remount the USB Stick as Read-Write</h3>
<div class="bd-anchor" id="1-remount-the-usb-stick-as-read-write"></div>
<p><strong>Sometimes, a USB stick can be mounted as read-only by the system due to the default system behaviour, previous errors, or incorrect mount options that include the read-only (<em>ro</em>) flag</strong>. In such cases, we can try to remount the USB stick manually with write permissions.</p>
<p>First, we need to find the mount point:</p>
<pre><code class="language-bash">$ df -h | grep /dev/sd
/dev/sdb5        73G   61G  8.1G  89% /
/dev/sda1       7.6G  6.6G  951M  88% /mnt/MyUSB</code></pre>
<p>This output shows the USB stick <em>/dev/sda1</em> is at <em>/mnt/MyUSB</em>.</p>
<p>Next, we remount the USB stick with the read-write flag:</p>
<pre><code class="language-bash">$ sudo mount -o remount,rw /dev/sda1 /mnt/MyUSB</code></pre>
<p>This command remounts <em>/dev/sda1</em> with read-write permissions. To explain, the <em>-o</em> flag specifies mount options, while <strong><a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/mount-permissions-remount-writable#remounting"><em>remount</em></a> instructs the system to remount an already-mounted filesystem without unmounting it</strong>, and <em>rw</em> remounts the filesystem as read-write.</p>
<p>Finally, let&#8217;s verify if the system has remounted the USB stick with read-write permissions:</p>
<pre><code class="language-bash">$ mount | grep /dev/sd
/dev/sdb5 on / type ext4 (rw,relatime,errors=remount-ro)
/dev/sdb5 on /var/snap/firefox/common/host-hunspell type ext4 (ro,noexec,noatime,errors=remount-ro)
/dev/sda1 on /mnt/MyUSB type exfat (rw,relatime,fmask=0022,dmask=0022,iocharset=utf8,errors=remount-ro)</code></pre>
<p>From the output, we can see <em>/dev/sda1</em> has an <em>rw</em> flag, indicating that the system successfully remounted the USB stick with read-write permissions.</p>
<h3 id="bd-2-check-for-a-physical-write-protect-switch" data-id="2-check-for-a-physical-write-protect-switch">4.2. Check for a Physical Write-Protect Switch</h3>
<div class="bd-anchor" id="2-check-for-a-physical-write-protect-switch"></div>
<p>Some USB sticks, especially older ones, come with a physical write-protect switch that prevents data deletion or modification by locking the USB stick in read-only mode. When we enable the switch, we cannot write to the USB stick regardless of the operating system or permissions.</p>
<p>The switch is located on the side or bottom of the USB stick, and it signals the operating system to mount the device as read-only when enabled. However, in this state, we can view and copy files from the USB stick, but we can&#8217;t create, edit, or delete them.</p>
<p><strong>To disable the write-protect switch, we usually need to remove the USB stick from the computer and toggle a switch to the side labeled <em>unlock</em> or <em>rw</em></strong>.</p>
<p>Lastly, let&#8217;s test to see if we now have write permissions on the USB stick by attempting to create a file:</p>
<pre><code class="language-bash">$ sudo touch /mnt/MyUSB/passwords.txt</code></pre>
<p>If no errors occur and the file is created successfully, this means the read-only mode was probably due to the hardware write-protect switch.</p>
<h3 id="bd-3-run-filesystem-check" data-id="3-run-filesystem-check">4.3. Run Filesystem Check</h3>
<div class="bd-anchor" id="3-run-filesystem-check"></div>
<p><strong>Filesystem corruption often causes a USB stick to mount as read-only</strong>. This can occur if we remove the USB stick without unmounting it or if a power failure causes an interruption.</p>
<p>To fix filesystem errors, we use the <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/fsck-mounted-partition#what-is-fsck"><em>fsck</em></a> command.</p>
<p>First, we need to unmount the USB stick:</p>
<pre><code class="language-bash">$ sudo umount /dev/sda1</code></pre>
<p>Here, we replace <em>/dev/sda1</em> with the name of the USB stick.</p>
<p>Next, let&#8217;s check the USB stick for filesystem corruption. Since we&#8217;re using an exFAT-formatted USB stick, we employ <em>fsck.exfat</em>:</p>
<pre><code class="language-bash">$ sudo fsck.exfat /dev/sda1</code></pre>
<p>The above command checks and attempts to repair any filesystem errors on <em>/dev/sda1</em>. In case of an error or prompt, we can follow the instructions to fix issues.</p>
<p>Once the scan completes without errors, we can remount the USB stick:</p>
<pre><code class="language-bash">$ sudo mount /dev/sda1 /mnt/MyUSB</code></pre>
<p>Here, we mount the <em>/dev/sda1</em> filesystem to the <em>/mnt/MyUSB</em> directory, enabling us to access its contents from there.</p>
<p>Now, to verify whether the issue is resolved, we can try creating a file on the USB stick:</p>
<pre><code class="language-bash">$ sudo touch /mnt/MyUSB/contacts.txt</code></pre>
<p>In this example, <em>/mnt/MyUSB</em> represents the mounting point of the USB stick. If the file is created successfully, the USB stick is now writable. However, if the USB stick is still mounted as read-only, we can try the next solution.</p>
<h3 id="bd-4-replace-a-failing-usb-stick" data-id="4-replace-a-failing-usb-stick">4.4. Replace a Failing USB Stick</h3>
<div class="bd-anchor" id="4-replace-a-failing-usb-stick"></div>
<p>If none of the above solutions work and the USB stick still mounts as read-only, it could indicate hardware failure. USB flash memory supports a limited number of write cycles, and once it reaches this limit, controllers may switch the device to read-only mode to prevent data loss.</p>
<p>Let&#8217;s see some of the common signs of a failing USB stick:</p>
<ul>
<li>the system repeatedly mounts the USB stick as read-only</li>
<li>the USB stick shows 0 bytes free even though it has space available</li>
<li>the system no longer recognizes the USB stick</li>
<li>when we try to perform file operations such as copy, delete, or create, we get input-output errors</li>
</ul>
<p>In this case, the best option is to copy any available data off the USB stick and replace it with a more reliable one.</p>
<h2 id="bd-conclusion" data-id="conclusion">5. Conclusion</h2>
<div class="bd-anchor" id="conclusion"></div>
<p>In this article, we discussed reasons why a USB stick may be mounted as read-only and how to identify and fix the issue.</p>
<p>By understanding the common causes of this issue, we can take the right steps to fix USB stick read-only issues.</p><p>The post <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux/usb-fix-read-only-mounts">How to Fix USB Sticks Mounted as Read-Only</a> first appeared on <a href="https://feeds.feedblitz.com/~/t/0/0/baeldung/linux/~https://www.baeldung.com/linux">Baeldung on Linux</a>.</p><Img align="left" border="0" height="1" width="1" alt="" style="border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;" hspace="0" src="https://feeds.feedblitz.com/~/i/919792943/0/baeldung/linux">
<div style="clear:both;padding-top:0.2em;"><a title="Like on Facebook" href="https://feeds.feedblitz.com/_/28/919792943/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/fblike20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Pin it!" href="https://feeds.feedblitz.com/_/29/919792943/baeldung/linux,https%3a%2f%2fwww.baeldung.com%2fwp-content%2fuploads%2fsites%2f2%2f2019%2f11%2fFeatured-Linux-4-k.jpg"><img height="20" src="https://assets.feedblitz.com/i/pinterest20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Post to X.com" href="https://feeds.feedblitz.com/_/24/919792943/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/x.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by email" href="https://feeds.feedblitz.com/_/19/919792943/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/email20.png" style="border:0;margin:0;padding:0;"></a>&#160;<a title="Subscribe by RSS" href="https://feeds.feedblitz.com/_/20/919792943/baeldung/linux"><img height="20" src="https://assets.feedblitz.com/i/rss20.png" style="border:0;margin:0;padding:0;"></a>&nbsp;<h3 style="clear:left;padding-top:10px">Related Stories</h3><ul><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/mount-smb-security-permission-denied">SMB mount Security Modes and Fixing mount error(13): Permission denied in Linux</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/selinux">What Is SELinux?</a></li><li><a rel="NOFOLLOW" href="https://www.baeldung.com/linux/cron-logograte-delete-log-files">Removing Log Files with a Cron Job in Linux</a></li></ul>&#160;</div>]]>
</content:encoded>
					
		
		
		<webfeeds:featuredImage>https://www.baeldung.com/wp-content/uploads/sites/2/2019/11/Featured-Linux-4-k-150x150.jpg</webfeeds:featuredImage></item>
</channel></rss>

