Jump to content

Terminal cheat sheet: Difference between revisions

From postmarketOS Wiki
Log files: add tinydm log path
Jane400 (talk | contribs)
Add "Rebooting to fastboot" section
 
(26 intermediate revisions by 13 users not shown)
Line 1: Line 1:
[[File:postmarketOS_cheatsheet.png|thumb|right|Another postmarketOS cheat sheet with similar content, in poster format.]]
[[File:postmarketOS_cheatsheet.png|thumb|right|Another postmarketOS cheat sheet with similar content, in poster format.]]


Here's a small reference for working with postmarketOS/Alpine Linux.
Here's a small reference for working with postmarketOS/Alpine Linux.  


__TOC__  
__TOC__  


== Package management ==
== Package management ==
The package manager in postmarketOS is called <code>apk</code>, the Alpine Package Keeper. The list of packages can be found at [https://pkgs.alpinelinux.org/packages pkgs.alpinelinux.org] and [https://pkgs.postmarketos.org/packages pkgs.postmarketos.org].
The package manager in postmarketOS is called <code>apk</code>, the Alpine Package Keeper. The list of packages can be found at [https://pkgs.alpinelinux.org/packages pkgs.alpinelinux.org] and [https://pkgs.postmarketos.org/packages pkgs.postmarketos.org].


Installing a package
The Alpine Linux wiki has a page '''[https://wiki.alpinelinux.org/wiki/Comparison_with_other_distros comparing apk to other popular package managers]''', with many helpful examples.


<source lang="shell-session">
=== Installing a package ===
<syntaxhighlight lang="shell-session">
$ sudo apk add neovim
$ sudo apk add neovim
</source>
</syntaxhighlight>


Uninstalling a package
=== Uninstalling a package ===
 
<syntaxhighlight lang="shell-session">
<source lang="shell-session">
$ sudo apk del neovim
$ sudo apk del neovim
</source>
</syntaxhighlight>


Upgrading the system. The <code>-a</code> option allows downgrading packages to what's available in the repository. This is sometimes needed when packages are moved from a git version to a stable release.
=== Upgrading the system ===
The <code>-a</code> option allows downgrading packages to what's available in the repository. It is generally recommended to always use this option, especially on stable releases, to ensure that any package reverts make it to your device.


<source lang="shell-session">
<syntaxhighlight lang="shell-session">
$ sudo apk upgrade -a
$ sudo apk upgrade -a
</source>
</syntaxhighlight>


=== Repairing packages ===
Run various package repair strategies, e.g. running failed installation scripts again:
Run various package repair strategies, e.g. running failed installation scripts again:


<source lang="shell-session">
<syntaxhighlight lang="shell-session">
$ sudo apk fix
$ sudo apk fix
$ sudo apk fix name-of-package  # will also reinstall name-of-package
$ sudo apk fix name-of-package  # will also reinstall name-of-package
</source>
</syntaxhighlight>


=== Listing installed packages ===
Listing installed package versions (add <code>| grep package-name</code> to filter for a specific package):
Listing installed package versions (add <code>| grep package-name</code> to filter for a specific package):


<source lang="shell-session">
<syntaxhighlight lang="shell-session">
$ apk info -vv
$ apk info -vv
</source>
$ apk info -vv | grep package-name
</syntaxhighlight>


Installing build dependencies:
=== Installing build dependencies ===
 
<syntaxhighlight lang="shell-session">
<source lang="shell-session">
$ sudo apk add build-base install-makedepends
$ sudo apk add build-base install-makedepends
$ install-makedepends neovim
$ install-makedepends neovim
</source>
</syntaxhighlight>


The repositories are stored in <code>/etc/apk/repositories</code> as one repository url per line. There's also <code>/etc/apk/world</code> which is a list of packages that are explicitly installed. It's possible to add/remove packages from this list and then running <code>apk fix</code> to apply those changes. The Alpine Linux wiki has a page '''[https://wiki.alpinelinux.org/wiki/Comparison_with_other_distros comparing apk to other popular distros]''', with many helpful examples.
=== Config files ===
The repositories are stored in <code>/etc/apk/repositories</code> as one repository url per line. There's also <code>/etc/apk/world</code> which is a list of packages that are explicitly installed. It's possible to add/remove packages from this list and then running <code>apk fix</code> to apply those changes.
 
New installations of postmarketOS let apk ask for confirmation by default. If you don't want this, delete <code>/etc/apk/interactive</code> ({{MR|3444|pmaports}}).
 
=== Logging with apk ===
A feature to log each apk action, especially which packages have been installed with an upgrade [https://gitlab.alpinelinux.org/alpine/apk-tools/-/merge_requests/55 will be available in apk3]. In the meantime, similar functionality can be achieved by installing <code>etckeeper</code>. It turns your <code>/etc</code> directory into a git repository, tracking changes to all files in there, and it also records package changes and related versions in the git log.
 
<syntaxhighlight lang="shell-session">
$ sudo apk add etckeeper
$ sudo apk add hello-world
$ sudo git -C /etc log
commit e8dc072cbe037eeaeec3adb8649680b2fcd10130 (HEAD -> master)
Author: root <root@nokia-n900>
Date:  Tue Jan 9 15:43:28 2024 +0000
 
    committing changes in /etc after apk run
   
    Package changes:
    +hello-world-1-r6
</syntaxhighlight>


== Service management ==
== Service management ==


The service manager in postmarketOS is OpenRC. The basic service management is done with the familiar <code>service</code> command.
=== systemd ===
 
systemd is the default service manager for GNOME (Desktop and Mobile), Plasma (Desktop and Mobile) and Phosh.
 
Basic service management is done with <code>systemctl</code>.
 
<syntaxhighlight lang="shell-session">
$ sudo systemctl status NetworkManager
$ sudo systemctl start NetworkManager
$ sudo systemctl stop NetworkManager
$ sudo systemctl restart NetworkManager
</syntaxhighlight>


<source lang="shell-session">
To enable or disable services on boot:
$ service networkmanager status
 
$ sudo service networkmanager start
<syntaxhighlight lang="shell-session">
$ sudo service networkmanager stop
$ sudo systemctl enable NetworkManager
$ sudo service networkmanager restart
$ sudo systemctl disable NetworkManager
</source>
</syntaxhighlight>
 
=== OpenRC ===
 
OpenRC is the default service manager for all other UIs.
 
The basic service management is done with the familiar <code>rc-service</code> command.
 
{{note|Tip: <code>rc-service</code> is also symlinked to <code>service</code> which is shorter to type.}}
 
<syntaxhighlight lang="shell-session">
$ rc-service networkmanager status
$ sudo rc-service networkmanager start
$ sudo rc-service networkmanager stop
$ sudo rc-service networkmanager restart
</syntaxhighlight>


To enable or disable services on boot you use the <code>rc-update</code> command.
To enable or disable services on boot you use the <code>rc-update</code> command.


<source lang="shell-session">
<syntaxhighlight lang="shell-session">
List the services that are added to a runlevel
List the services that are added to a runlevel
$ rc-update
$ rc-update
Line 82: Line 130:
Stop NetworkManager starting on boot
Stop NetworkManager starting on boot
$ sudo rc-update del networkmanager default
$ sudo rc-update del networkmanager default
</source>
</syntaxhighlight>
 
==== Log files ====
 
By default, postmarketOS with OpenRC uses [https://git.sr.ht/~martijnbraam/logbookd logbookd] for the syslog and it's configured to log to memory only. The <code>logread</code> command is used to read the in-memory log. Writing standard output and standard error of a system service to the filesystem can be achieved by setting <code>output_log</code> and <code>error_log</code> to file paths in its service script, as described in the manual page <code>openrc-run</code>.


== Log files ==
ModemManager logs to syslog by default as well, but prints almost nothing until manually increasing the log level with <code>sudo mmcli -G DEBUG</code>. Be aware that especially ModemManager logs a lot of sensitive information, such as phone numbers, text messages, IMEI, cell towers, etc!


By default postmarketOS uses the busybox logging daemon for the syslog and it's configured to log to memory only. The <code>logread</code> command is used to read the in-memory log. Writing standard output and standard error of a system service to the filesystem can be achieved by setting <code>output_log</code> and <code>error_log</code> to file paths in its service script, as described in the manual page <code>openrc-run</code>.
Most user interfaces use [https://gitlab.postmarketos.org/postmarketOS/tinydm tinydm] to start the session in postmarketOS, which logs to <code>~/.local/state/tinydm.log</code>.


Most user interfaces use [https://gitlab.com/postmarketOS/tinydm tinydm] to start the session in postmarketOS, which logs to <code>~/.local/state/tinydm.log</code>.
A discussion to possibly change logging defaults is in {{issue|1574|pmaports}}.


== Man pages ==
== Man pages ==
Line 95: Line 147:
but installing the specific documentation subpackage. For example for vim documentation you can install <code>vim-doc</code>
but installing the specific documentation subpackage. For example for vim documentation you can install <code>vim-doc</code>


<source lang="shell-session">
<syntaxhighlight lang="shell-session">
the mandoc package is the man page reader itself
the mandoc package is the man page reader itself
the man-apropos adds the apropos manpage search tool
the man-apropos adds the apropos manpage search tool
the man-pages package will add the pages for core software like chmod but also linux headers
the man-pages package will add the pages for core software like chmod but also linux headers
the docs package is a metapackage that pulls in the -doc package for everything you install
the docs package is a metapackage that pulls in the -doc package for everything you install
$ sudo apk add mandoc mandoc-apropos man-pages docs
$ sudo apk add -i mandoc mandoc-apropos man-pages docs
$ man nvim
$ man nvim
</source>
</syntaxhighlight>


== Changing your shell ==
== Changing your shell ==


Since the pmos installations are based on busybox the default shell is <code>ash</code>, which is comparible to <code>sh</code>. There are multiple shells packaged in Alpine if you want something better:
Since the pmos installations are based on busybox the default shell is <code>ash</code>, which is comparable to <code>sh</code>. There are multiple shells packaged in Alpine if you want something better:


<source lang="shell-session">
<syntaxhighlight lang="shell-session">
pick your favorite shell
pick your favorite shell
$ sudo apk add bash zsh fish dash yash oksh elvish
$ sudo apk add -i bash zsh fish dash yash oksh elvish


set your login shell
set your login shell
Line 116: Line 168:


some config distributions are packaged
some config distributions are packaged
$ sudo apk add grml-zsh-config oh-my-zsh
$ sudo apk add -i grml-zsh-config oh-my-zsh
</source>
</syntaxhighlight>
 
== Getting session environment ==
 
When working on things like pipewire or other software that needs access to the user session, it can be frustrating to do over ssh. The following one-liner can be used to set up the environment
 
<syntaxhighlight lang="shell-session">
# Replace with your DE (phosh, sway, etc)
$ export TARGET="gnome-shell"
$ eval $(cat /proc/$(pidof $TARGET)/environ | tr '\0' '\n' | grep -v '^TERM=' | awk '{ print "export \"" $0 "\"" }')
</syntaxhighlight>
 
This can be installed in your <code>~/.bashrc</code> or <code>~/.zshrc</code> as follows:
 
<pre>
yoinkenv () {
TARGET=${1:-gnome-shell}
echo "Yoinking environment from $APP"
eval $(cat /proc/$(pidof $TARGET)/environ | tr '\0' '\n' | grep -v '^TERM=' | awk '{ print "export \"" $0 "\"" }')
}
</pre>
 
Note that in order to have <code>~/.bashrc</code> being sourced in a ssh session, you need something like the following in your <code>~/.bash_profile</code>:
 
<pre>
if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi
</pre>
 
== Pushing source changes to a target ==
 
For some development, it might be easiest to edit the source code on your laptop/PC but compile on the device. This can be made easy with a small one-liner you can run from your project checkout to sync changes to the device every time you save. You will need to create the target directory on the device manually.
 
<syntaxhighlight lang="shell">
while true; do inotifywait -e modify src/**; rsync --exclude-from=.gitignore -a . pmos:projects/$(basename "$PWD")/; done
</syntaxhighlight>
 
Rsync will ignore all paths specified in the local <code>.gitignore</code>.
 
This expects you to have a <code>pmos</code> entry in your SSH config, see [[SSH#Config]] for more info.
 
== Rebooting to fastboot ==
 
<syntaxhighlight lang="shell-session">
On systemd installs:
# systemctl reboot --reboot-argument=bootloader
 
On OpenRC installs (this doesn't properly shutdown your device):
# reboot-mode bootloader
 
</syntaxhighlight>
 
== See also ==
* {{issue|1574|pmaports}} Make logging consistent
 
[[Category:Guide]]

Latest revision as of 12:51, 3 February 2025

Another postmarketOS cheat sheet with similar content, in poster format.

Here's a small reference for working with postmarketOS/Alpine Linux.

Package management

The package manager in postmarketOS is called apk, the Alpine Package Keeper. The list of packages can be found at pkgs.alpinelinux.org and pkgs.postmarketos.org.

The Alpine Linux wiki has a page comparing apk to other popular package managers, with many helpful examples.

Installing a package

$ sudo apk add neovim

Uninstalling a package

$ sudo apk del neovim

Upgrading the system

The -a option allows downgrading packages to what's available in the repository. It is generally recommended to always use this option, especially on stable releases, to ensure that any package reverts make it to your device.

$ sudo apk upgrade -a

Repairing packages

Run various package repair strategies, e.g. running failed installation scripts again:

$ sudo apk fix
$ sudo apk fix name-of-package  # will also reinstall name-of-package

Listing installed packages

Listing installed package versions (add | grep package-name to filter for a specific package):

$ apk info -vv
$ apk info -vv | grep package-name

Installing build dependencies

$ sudo apk add build-base install-makedepends
$ install-makedepends neovim

Config files

The repositories are stored in /etc/apk/repositories as one repository url per line. There's also /etc/apk/world which is a list of packages that are explicitly installed. It's possible to add/remove packages from this list and then running apk fix to apply those changes.

New installations of postmarketOS let apk ask for confirmation by default. If you don't want this, delete /etc/apk/interactive (pmaports!3444).

Logging with apk

A feature to log each apk action, especially which packages have been installed with an upgrade will be available in apk3. In the meantime, similar functionality can be achieved by installing etckeeper. It turns your /etc directory into a git repository, tracking changes to all files in there, and it also records package changes and related versions in the git log.

$ sudo apk add etckeeper
$ sudo apk add hello-world
$ sudo git -C /etc log
commit e8dc072cbe037eeaeec3adb8649680b2fcd10130 (HEAD -> master)
Author: root <root@nokia-n900>
Date:   Tue Jan 9 15:43:28 2024 +0000

    committing changes in /etc after apk run
    
    Package changes:
    +hello-world-1-r6

Service management

systemd

systemd is the default service manager for GNOME (Desktop and Mobile), Plasma (Desktop and Mobile) and Phosh.

Basic service management is done with systemctl.

$ sudo systemctl status NetworkManager
$ sudo systemctl start NetworkManager
$ sudo systemctl stop NetworkManager
$ sudo systemctl restart NetworkManager

To enable or disable services on boot:

$ sudo systemctl enable NetworkManager
$ sudo systemctl disable NetworkManager

OpenRC

OpenRC is the default service manager for all other UIs.

The basic service management is done with the familiar rc-service command.

Note Tip: rc-service is also symlinked to service which is shorter to type.
$ rc-service networkmanager status
$ sudo rc-service networkmanager start
$ sudo rc-service networkmanager stop
$ sudo rc-service networkmanager restart

To enable or disable services on boot you use the rc-update command.

List the services that are added to a runlevel
$ rc-update
             bootmisc | boot                                   
              chronyd |      default                           
                 dbus |      default                           
                devfs |                                 sysinit
                dmesg |                                 sysinit
                  gdm |      default                           
              haveged |      default                           
             hostname | boot                                   
              hwclock | boot                                   
            killprocs |                        shutdown        


Start NetworkManager on boot (in the default runlevel)
$ sudo rc-update add networkmanager default

Stop NetworkManager starting on boot
$ sudo rc-update del networkmanager default

Log files

By default, postmarketOS with OpenRC uses logbookd for the syslog and it's configured to log to memory only. The logread command is used to read the in-memory log. Writing standard output and standard error of a system service to the filesystem can be achieved by setting output_log and error_log to file paths in its service script, as described in the manual page openrc-run.

ModemManager logs to syslog by default as well, but prints almost nothing until manually increasing the log level with sudo mmcli -G DEBUG. Be aware that especially ModemManager logs a lot of sensitive information, such as phone numbers, text messages, IMEI, cell towers, etc!

Most user interfaces use tinydm to start the session in postmarketOS, which logs to ~/.local/state/tinydm.log.

A discussion to possibly change logging defaults is in pmaports#1574.

Man pages

Man pages are not installed by default and are seperate from the normal packages. The docs package is a metapackage that will pull in all the documentation for the software you have installed. You can also install only the documentation you need by not installing the docs package but installing the specific documentation subpackage. For example for vim documentation you can install vim-doc

the mandoc package is the man page reader itself
the man-apropos adds the apropos manpage search tool
the man-pages package will add the pages for core software like chmod but also linux headers
the docs package is a metapackage that pulls in the -doc package for everything you install
$ sudo apk add -i mandoc mandoc-apropos man-pages docs
$ man nvim

Changing your shell

Since the pmos installations are based on busybox the default shell is ash, which is comparable to sh. There are multiple shells packaged in Alpine if you want something better:

pick your favorite shell
$ sudo apk add -i bash zsh fish dash yash oksh elvish

set your login shell
$ chsh -s /bin/zsh

some config distributions are packaged
$ sudo apk add -i grml-zsh-config oh-my-zsh

Getting session environment

When working on things like pipewire or other software that needs access to the user session, it can be frustrating to do over ssh. The following one-liner can be used to set up the environment

# Replace with your DE (phosh, sway, etc)
$ export TARGET="gnome-shell"
$ eval $(cat /proc/$(pidof $TARGET)/environ | tr '\0' '\n' | grep -v '^TERM=' | awk '{ print "export \"" $0 "\"" }')

This can be installed in your ~/.bashrc or ~/.zshrc as follows:

yoinkenv () {
	TARGET=${1:-gnome-shell}
	echo "Yoinking environment from $APP"
	eval $(cat /proc/$(pidof $TARGET)/environ | tr '\0' '\n' | grep -v '^TERM=' | awk '{ print "export \"" $0 "\"" }')
}

Note that in order to have ~/.bashrc being sourced in a ssh session, you need something like the following in your ~/.bash_profile:

if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi

Pushing source changes to a target

For some development, it might be easiest to edit the source code on your laptop/PC but compile on the device. This can be made easy with a small one-liner you can run from your project checkout to sync changes to the device every time you save. You will need to create the target directory on the device manually.

while true; do inotifywait -e modify src/**; rsync --exclude-from=.gitignore -a . pmos:projects/$(basename "$PWD")/; done

Rsync will ignore all paths specified in the local .gitignore.

This expects you to have a pmos entry in your SSH config, see SSH#Config for more info.

Rebooting to fastboot

On systemd installs:
# systemctl reboot --reboot-argument=bootloader

On OpenRC installs (this doesn't properly shutdown your device): 
# reboot-mode bootloader

See also