Index

joshstock.in / 76ff7a3

Source for serving and static templating/compiling of https://joshstock.in.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
15814 Jan 2023 23:375aaa26aWebsite rewriteJosh Stockin11340G

Blob @ joshstock.in / site / content / blog / recover-linux-shell.md

text/plain5510 bytesdownload raw
1---
2type: none
3identifier: recover-linux-shell
4title: Recovering a Linux desktop with a misconfigured shell
5description: How to fix a custom shell when your terminal can't open.
6datestring: 2022-11-10
7banner_image: /static/images/esp32.jpg
8links:
9 chsh: https://man7.org/linux/man-pages/man1/chsh.1.html
10 xonsh: https://xon.sh/
11 Virtual consoles: https://www.wikiwand.com/en/Virtual_console
12 /etc/passwd: https://man7.org/linux/man-pages/man5/passwd.5.html
13---
14
15Suppose you’ve used the `chsh` command to change your login shell in the past
16and something has gone wrong to the point that your custom shell executable is
17nonexistent or no longer functions properly. Opening a new terminal results in
18the window closing immediately following a fatal error, or you can’t log in to
19your desktop entirely. For me, this happened with `xonsh` (a great amalgam of
20Python and Bash) when upgrading between Ubuntu versions, as the installed
21Python library fell out of the system path. There are a few methods, however,
22to regain access to a functioning shell.
23
24# Gaining access to a functioning shell
25
26## Method 1(a): Application launching
27
28This method assumes the following:
29
30- You are still signed in to your semi-functioning desktop environment (DE)
31- You have a functioning terminal emulator and shell installed on your system
32- Your DE offers application launching
33
34This one is pretty self-explanatory. Terminal emulators can be fed command-line
35arguments to skip shell loading entirely and perform direct file execution. You
36can use this option to load a shell other than your user’s default shell, as
37shells are just executable files. Here are some example commands to load Bash
38with different terminals, bypassing your login shell:
39
40```bash
41xterm -e /usr/bin/env bash
42xfce4-terminal -e /usr/bin/env bash
43gnome-terminal -- /usr/bin/env bash
44```
45
46If your DE offers application launching, you can execute the appropriate
47command for your terminal emulator and (functional) shell of choice. The
48shortcut for application launching on Gnome and XFCE is `<Alt><F2>`. On KDE,
49it’s `<Alt><F1>`, and on i3, which uses dmenu, it’s `<Ctrl>D`. For other
50desktop environments with application launchers, check which you should use in
51the related documentation.
52
53## Method 1(b): GUI-based file execution
54
55This is an alternative method to 1(a), assuming:
56
57- You are still signed in to your semi-functioning desktop environment (DE)
58- Your DE *does not* have application launching, or it doesn’t work for some
59reason
60- You have a functioning terminal emulator and shell installed on your system
61- Your GUI file browser offers direct file execution without terminal
62
63Essentially, you create a file like the following, using the appropriate
64terminal emulator and shell:
65
66```bash
67#!/usr/bin/env bash
68gnome-terminal -- /bin/bash
69```
70
71Using your file browser, give this file permission for user execution and run
72it.
73
74## Method 2: Use another user with a functioning login shell
75
76This method assumes:
77
78- There is a separate user on your system that can be logged into via password
79that uses a functioning login shell
80- The user is a member of the sudo group
81
82Open a virtual console (usually with `<CTRL><ALT><F1..F9>`) and log in as the
83separate user. Then, you can take the necessary actions to fix your broken
84login shell or other configuration. (On my systems I create a sudoer named
85`bash-user` whose sole purpose is to be password-login-able with Bash as its
86login shell.)
87
88If you don’t have this option, then…
89
90## Method 3: Externally mount the filesystem and forcibly change the default login shell
91
92This is the most involved and complicated method, meant to be used as a last
93resort if you don’t feel like reimaging your system (or living without a
94terminal). You will need to find a way to externally mount the filesystem from
95another functioning operating system and change the `/etc/passwd` file
96directly.
97
98For desktops without any other operating systems installed, my go-to method
99looks roughly like this:
100
1011. Create a live USB bootable Linux distribution on a flash drive
102 (Linux-on-a-stick)
1032. UEFI boot into this Linux-on-a-stick
1043. Force mount the hard drive your Linux system is on, with sufficient
105 read-write permissions *(Look for external help on this, so as to not break
106 your filesystem or mess up permissions)*
107
108The full details of this method are a bit out of the scope of this article, so
109I’ll let you figure them out with the help of Google and StackOverflow. Having
110a bootable Linux-on-a-stick is also useful for other reasons, so if you don’t
111have one, I do recommend setting one up and using the steps outlined above.
112
113Once your filesystem is mounted and you have sufficient read/write permissions,
114you can do one of the following:
115
116- Directly change `/etc/passwd` to reflect a working login shell for your user
117- Through the external operating system, take the necessary actions to fix your
118 broken login shell (not recommended)
119
120After completing this, you should be able to log into your system as normal.
121
122# Fixing your custom shell
123
124This will vary widely depending on your setup and the shell you’re trying to
125fix, so unfortunately I can’t cover that extensively here. In my case, for
126fixing `xonsh`, I just had to reinstall it via Python’s package manager with
127this command:
128
129```bash
130$ sudo python3 -m pip install 'xonsh[full]'
131```
132
133Your process for fixing your broken shell will likely be a similar process of
134reinstallation or reconfiguration via `chsh`.
135