{"id":3806,"date":"2024-02-07T02:17:00","date_gmt":"2024-02-07T07:17:00","guid":{"rendered":"https:\/\/www.both.org\/?p=3806"},"modified":"2024-01-25T20:55:47","modified_gmt":"2024-01-26T01:55:47","slug":"how-i-hacked-my-linux-system-with-a-live-usb-device","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=3806","title":{"rendered":"How I hacked my Linux system with a Live USB device"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"3806\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p class=\"has-small-font-size\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A live Linux USB provides an effective hack to boot and enter a recovery mode.<\/h2>\n\n\n\n<p>I have a dozen or so physical computers in my home lab and even more VMs. I use most of these systems for testing and experimentation. I frequently write about using automation to make sysadmin tasks easier. I have also written in multiple places that I learn more from my own mistakes than I do in almost any other way.<\/p>\n\n\n\n<p>I have learned a lot during a couple of weeks in late 2022.<\/p>\n\n\n\n<p>I created a major problem for myself. Having been a sysadmin for years and written hundreds of articles and five books about Linux, I really should have known better. Then again, we all make mistakes, which is an important lesson: You&#8217;re never too experienced to make a mistake.<\/p>\n\n\n\n<p>I&#8217;m not going to discuss the details of my error. It&#8217;s enough to tell you that it was a mistake and that I should have put a lot more thought into what I was doing before I did it. Besides, the details aren&#8217;t really the point. Experience can&#8217;t save you from every mistake you&#8217;re going to make, but it can help you in recovery. And that&#8217;s literally what this article is about: Using a Live USB distribution to hack into the unbootable systems and fix the problem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The problem<\/h2>\n\n\n\n<p>First, I created the problem, which was essentially a bad configuration for the <code>\/etc\/default\/grub<\/code> file. Next, I used Ansible to distribute the misconfigured file to all my physical computers and run <code>grub2-mkconfig<\/code>. All 12 of them. Really, really fast.<\/p>\n\n\n\n<p>All but two failed to boot. They crashed during the very early stages of Linux startup with various errors indicating that the <code>\/root<\/code> filesystem could not be located.<\/p>\n\n\n\n<p>I could use the root password to get into &#8220;maintenance&#8221; mode, but without <code>\/root<\/code> mounted, it was impossible to access even the simplest tools. Booting directly to the recovery kernel did not work either. The systems were truly broken.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Recovery mode with Fedora<\/h2>\n\n\n\n<p>The only way to resolve this problem was to find a way to get into recovery mode. When all else fails, Fedora provides a really cool tool: The same Live USB thumb drive used to install new instances of Fedora.<\/p>\n\n\n\n<p>After setting the BIOS to boot from the Live USB device, I booted into the Fedora 36 Xfce live user desktop. I opened two terminal sessions next to each other on the desktop and switched to root privilege in both.<\/p>\n\n\n\n<p>I ran <code>lsblk<\/code> in one for reference. I used the results to identify the <code>\/<\/code> root partition and the <code>boot<\/code> and <code>efi<\/code> partitions. I used one of my VMs, as seen below. There is no <code>efi<\/code> partition in this case because this VM does not use UEFI.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># <strong>lsblk<\/strong>\nNAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS\nloop0           7:0    0  1.5G  1 loop\nloop1           7:1    0    6G  1 loop\n\u251c\u2500live-rw     253:0    0    6G  0 dm   \/\n\u2514\u2500live-base   253:1    0    6G  1 dm   \nloop2           7:2    0   32G  0 loop\n\u2514\u2500live-rw     253:0    0    6G  0 dm   \/\nsda             8:0    0  120G  0 disk\n\u251c\u2500sda1          8:1    0    1G  0 part\n\u2514\u2500sda2          8:2    0  119G  0 part\n  \u251c\u2500vg01-swap 253:2    0    4G  0 lvm  \n  \u251c\u2500vg01-tmp  253:3    0   10G  0 lvm  \n  \u251c\u2500vg01-var  253:4    0   20G  0 lvm  \n  \u251c\u2500vg01-home 253:5    0    5G  0 lvm  \n  \u251c\u2500vg01-usr  253:6    0   20G  0 lvm  \n  \u2514\u2500vg01-root 253:7    0    5G  0 lvm  \nsr0            11:0    1  1.6G  0 rom  \/run\/initramfs\/live\nzram0         252:0    0    8G  0 disk &#91;SWAP]<\/code><\/pre>\n\n\n\n<p>The <code>\/dev\/sda1<\/code> partition is easily identifiable as <code>\/boot<\/code>, and the root partition is pretty obvious as well.<\/p>\n\n\n\n<p>In the other terminal session, I performed a series of steps to recover my systems. The specific volume group names and device partitions such as <code>\/dev\/sda1<\/code> will differ for your systems. The commands shown here are specific to my situation.<\/p>\n\n\n\n<p>The objective is to boot and get through startup using the Live USB, then mount only the necessary filesystems in an image directory and run the <code>chroot<\/code> command to run Linux in the chrooted image directory. This approach bypasses the damaged GRUB (or other) configuration files. However, it provides a complete running system with all the original filesystems mounted for recovery, both as the source of the tools required and the target of the changes to be made.<\/p>\n\n\n\n<p>Here are the steps and related commands:<\/p>\n\n\n\n<p>1. Create the directory <code>\/mnt\/sysimage<\/code> to provide a location for the <code>chroot<\/code> directory.<\/p>\n\n\n\n<p>2. Mount the root partition on <code>\/mnt\/sysimage:<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># mount \/dev\/mapper\/vg01-root \/mnt\/sysimage<\/code><\/pre>\n\n\n\n<p>3. Make <code>\/mnt\/sysimage<\/code> your working directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># cd \/mnt\/sysimage<\/code><\/pre>\n\n\n\n<p>4. Mount the <code>\/boot<\/code> and <code>\/boot\/efi<\/code> filesystems.<\/p>\n\n\n\n<p>5. Mount the other main filesystems. Filesystems like <code>\/home<\/code> and <code>\/tmp<\/code> are not needed for this procedure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># mount \/dev\/mapper\/vg01-usr usr\n\n# mount \/dev\/mapper\/vg01-var var<\/code><\/pre>\n\n\n\n<p>6. Mount important but already mounted filesystems that must be shared between the chrooted system and the original Live system, which is still out there and running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># mount --bind \/sys sys\n\n# mount --bind \/proc proc<\/code><\/pre>\n\n\n\n<p>7. Be sure to do the <code>\/dev<\/code> directory last, or the other filesystems won&#8217;t mount:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># mount --bind \/dev dev<\/code><\/pre>\n\n\n\n<p>8. Chroot the system image:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># chroot \/mnt\/sysimage<\/code><\/pre>\n\n\n\n<p>The system is now ready for whatever you need to do to recover it to a working state. However, one time I was able to run my server for several days in this state until I could research and test real fixes. I don&#8217;t really recommend that, but it can be an option in a dire emergency when things just need to get up and running\u2013now!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The solution<\/h2>\n\n\n\n<p>The fix was easy once I got each system into recovery mode. Because my systems now worked just as if they had booted successfully, I made the necessary changes to <code>\/etc\/default\/grub<\/code> and <code>\/etc\/fstab<\/code> and ran the <code>grub2-mkconfig > boot\/grub2\/grub.cfg<\/code> command. I used the <code>exit<\/code> command to exit from chroot and then rebooted the host.<\/p>\n\n\n\n<p>Of course, I could not automate the recovery from my mishap. I had to perform this entire process manually on each host\u2014a fitting bit of karmic retribution for using automation to quickly and easily propagate my own errors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Lessons learned<\/h2>\n\n\n\n<p>Despite their usefulness, I used to hate the &#8220;Lessons Learned&#8221; sessions we would have at some of my sysadmin jobs, but it does appear that I need to remind myself of a few things. So here are my &#8220;Lessons Learned&#8221; from this self-inflicted fiasco.<\/p>\n\n\n\n<p>First, the ten systems that failed to boot used a different volume group naming scheme, and my new GRUB configuration failed to consider that. I just ignored the fact that they might possibly be different.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Think it through completely.<\/li>\n\n\n\n<li>Not all systems are alike.<\/li>\n\n\n\n<li>Test everything.<\/li>\n\n\n\n<li>Verify everything.<\/li>\n\n\n\n<li>Never make assumptions.<\/li>\n<\/ul>\n\n\n\n<p>Everything now works fine. Hopefully, I am a little bit smarter, too.<\/p>\n\n\n\n<hr class=\"wp-block-separator aligncenter has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<p>Author&#8217;s note: This article first appeared on Opensource.com. It has been modified slightly for this site.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I used a Fedora Live USB to hack into my systems, enter recovery mode, and resolve a problem that I&#8217;d created on multiple computers.<\/p>\n","protected":false},"author":2,"featured_media":3811,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[78,5],"tags":[240,185,239],"class_list":["post-3806","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fixing-problems","category-linux","tag-fedora","tag-live-usb","tag-system-recovery"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/3806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3806"}],"version-history":[{"count":9,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/3806\/revisions"}],"predecessor-version":[{"id":3816,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/3806\/revisions\/3816"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/3811"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}