Skip to content

Commit 82a2ec2

Browse files
committed
Update Linux command reference post
1 parent 534a08a commit 82a2ec2

File tree

1 file changed

+173
-18
lines changed

1 file changed

+173
-18
lines changed

Diff for: content/blog/linux-cmds.md

+173-18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ date = "2023-12-26"
99
<!-- General commands -->
1010
<!-- TODO: grep w/ regex -->
1111

12+
## Overview
13+
14+
This post serves to document useful Linux commands I use regularly or find useful. I intend to update it periodically as I find more and more useful commands.
15+
16+
Some command explanation is included with each, but Linux and command line experience is assumed.
17+
1218
## General Commands
1319

1420
These commands aren't necessarily Linux machine-specific, but they may come in handy when using one.
@@ -84,6 +90,7 @@ $ kill 921
8490

8591
```Bash
8692
# Run with '-w' to follow in real time (like 'tail -f')
93+
# The 'journalctl -k' command will also show kernel logs and is generally more flexible
8794
$ sudo dmesg
8895
```
8996

@@ -117,6 +124,9 @@ Mar 17 12:29:11 meadowlark systemd[1]: Finished Network Manager Wait Online.
117124

118125
The following assumes an example _system-level_ Systemd unit called `run_periodically.timer`. You can use the same commands for _user-level_ Systemd units like `pulseaudio.service`, e.g. `systemctl status --user pulseaudio.service`.
119126

127+
Active (enabled) systemd unit files are located in `/etc/systemd/system/` and `/etc/systemd/user/` directories.
128+
When modifying systemd units, modify files in these directories. Other unit files exist on Linux systems, primarily in a `/usr/lib/systemd/` directory (depends on distribution). However, generally do not modify these as they are installed by your package manager and are meant as defaults.
129+
120130
```Bash
121131
# Start the unit
122132
$ sudo systemctl start run_periodically.timer
@@ -130,7 +140,7 @@ $ sudo systemctl restart run_periodically.timer
130140
# Enable the unit (start automatically)
131141
$ sudo systemctl enable run_periodically.timer
132142

133-
# Disable the unit (do not start automatically)
143+
# Disable the unit (only starts manually)
134144
$ sudo systemctl disable run_periodically.timer
135145

136146
# Mask the unit
@@ -154,6 +164,8 @@ $ sudo systemctl daemon-reload
154164

155165
#### View Service (Daemon) Logs
156166

167+
The following is a list of `journalctl` commands, each with separate options. Many of these may be combined to perform a specific task. For example, `journalctl -k -b 50 -r` will show the last 50 lines of kernel logs in reverse order (most recent to least recent).
168+
157169
Just as Systemd `systemctl` commands support both system-level and user-level units, `journalctl` does as well using the same `--user` argument.
158170

159171
```Bash
@@ -164,20 +176,34 @@ $ journalctl
164176
$ journalctl -n 100
165177

166178
# Follow logs in real time (similar to 'tail -f')
179+
# Can also substitute '--follow' for '-f'
167180
$ journalctl -f
168181

182+
# Show logs in reverse order (most recent to least recent)
183+
# Cannot use this option with '-f'
184+
$ journalctl -r
185+
186+
# Show all logs that contain the provided pattern, here 'wlan0'
187+
# Can also substitute the '--grep' option for '-g'
188+
$ journalctl -g wlan0
189+
169190
# Only show logs for a specific ID, here 'test_program'.
170191
# For example, the following would create a log message and
171192
# tag it with the 'test_program' tag:
172-
# logger -t test_program "This is a log message"
193+
# echo "This is a warning message" | systemd-cat -t test_program -p warning
173194
#
174195
# Note that this is different from the '-u' option
175196
$ journalctl -t test_program
176197

177198
# Only show logs for a specific service, here, 'wpa_supplicant.service'
199+
# Can also provide full Systemd unit name, e.g. here 'wpa_supplicant.service'
178200
$ journalctl -u wpa_supplicant
179-
# or
180-
$ journalctl -u wpa_supplicant.service
201+
202+
# Show kernel logs (same output as 'dmesg' command)
203+
# Can also substitute the '--dmesg' option for '-k'
204+
#
205+
# This is the same as 'journalctl -t kernel'
206+
$ journalctl -k
181207

182208
# Only show logs for this boot (can use '-b' option instead of '--boot')
183209
# When using the '--boot' and '-b' arguments, '0' and '-0' function the same (this boot)
@@ -324,10 +350,11 @@ $ du -h
324350
You probably want to use Network Manager to configure your networking instead.
325351
See the [next section](#managing-networking-network-manager) for more details.
326352

327-
Most Linux distributions run Network Manager to configure and manage networking nowadays. This is a common point of confusion, as there is still material available online which assumes older, network-scripts based network management.
353+
Most Linux distributions run Network Manager to configure and manage networking nowadays. This is a common point of confusion, as many guides online reference older, network-scripts based network management and the deprecated `ifconfig` command.
354+
355+
It is possible to configure networking with variations of these commands. Generally, though, you'll almost always be better off using a tool like `NetworkManager` or `systemd-networkd`, unless you have a very specific use case.
328356

329-
It is possible to configure networking with variations of these commands. Unless you know
330-
what you're doing, though, you're better off just using Network Manager.
357+
For more information, see this [very detailed guide](https://axil.gitlab.io/iproute2/).
331358

332359
#### Show Network Interface Link-Layer Info</h3>
333360

@@ -346,7 +373,7 @@ wlan0 UP xx:xx:xx:xx:xx:xx <BROADCAST,MULTICAST,UP,LOWER_
346373

347374
#### Show Network Interface IP-Layer Info
348375

349-
Displays IPv4 and IPv6 information in addition to interface status, MAC address, MTU, and interface routing table, among other things (some of which require `-d` option). More detailed output is possible by using the `-d` option (without the `-br` option).
376+
These commands display IPv4 and IPv6 information in addition to other detailed information including interface status, MAC address, MTU, and interface routing table, among other things (some of which require `-d` option). More detailed output is possible by using the `-d` option (without the `-br` option).
350377

351378
```Bash
352379
# Shorthand shown. Full command would be 'ip addr show', but the 'show' is optional.
@@ -526,7 +553,11 @@ $ nmcli c m SSID_NAME \
526553

527554
**NOTE:** Most Linux systems use Network Manager to manage and configure networking, including WiFi. See the [Managing Networking](#managing-networking-network-manager) section for more details.
528555

529-
#### Show WiFi Interface General Info
556+
On Linux WiFi interfaces are created using a parent radio device, referred to as 'phys'. These radios come in a variety of form factors, including single radio, single phy and single radio, multi-phy. To view all system phys, run `ls /sys/class/ieee80211/`, which lists all `ieee80211` devices (WiFi phys). Supported interfaces, combinations, and settings depend on the radio firmware and associated Linux device driver. By default, a single WiFi interface is created per phy on system boot in 'managed' mode (WiFi station).
557+
558+
While most will be content with `NetworkManager` managing their WiFi interface settings, a more advanced user may find the `wpa_supplicant` and `hostapd` programs of interest. The program `wpa_supplicant` configures WiFi clients, whereas `hostapd` configures WiFi access points (APs, what most people refer to as a router). Both live under the `hostap` project and are widely used, including within tools like `NetworkManager` itself and within commercial APs as well. The configuration syntax is notoriously somewhat difficult, especially if you don't know the details of WiFi well.
559+
560+
#### Show WiFi Interface General Information
530561

531562
Includes STA MAC, SSID, phy device, channel, frequency, transmit power.
532563

@@ -548,6 +579,8 @@ Interface wlan0
548579

549580
#### Show WiFi Interface Link Information
550581

582+
**NOTE:** This command will only show meaningful output when the WiFi interface is connected (associated).
583+
551584
Includes AP MAC (if station), SSID, frequency, bandwidth, RSSI (if station), and phy rate (MCS),
552585
among other things. Phy rate may or may not include NSS.
553586

@@ -569,7 +602,7 @@ Connected to xx:xx:xx:xx:xx:xx (on wlan0)
569602
beacon int: 100
570603
```
571604

572-
#### Show WiFi Radio Info
605+
#### Show WiFi Radio/Phy Info
573606

574607
Includes channels and bands in current regulatory domain, ciphers, MCS rates, and antennas, among other things.
575608

@@ -601,16 +634,138 @@ $ iw phy0 info
601634

602635
#### Show Wireless Regulatory Domain Info
603636

637+
You may see a combination of 'global' and per-phy regulatory configuration. The following shows both.
638+
604639
```Bash
640+
# Notice the inclusion of sub-1GHz (WiFi HaLow, 802.11ah) and 60GHz (WiGig) spectrum
605641
$ iw reg get
606642
global
643+
country US: DFS-FCC
644+
(902 - 904 @ 2), (N/A, 30), (N/A)
645+
(904 - 920 @ 16), (N/A, 30), (N/A)
646+
(920 - 928 @ 8), (N/A, 30), (N/A)
647+
(2400 - 2472 @ 40), (N/A, 30), (N/A)
648+
(5150 - 5250 @ 80), (N/A, 23), (N/A), AUTO-BW
649+
(5250 - 5350 @ 80), (N/A, 24), (0 ms), DFS, AUTO-BW
650+
(5470 - 5730 @ 160), (N/A, 24), (0 ms), DFS
651+
(5730 - 5850 @ 80), (N/A, 30), (N/A), AUTO-BW
652+
(5850 - 5895 @ 40), (N/A, 27), (N/A), NO-OUTDOOR, AUTO-BW
653+
(5925 - 7125 @ 320), (N/A, 12), (N/A), NO-OUTDOOR
654+
(57240 - 71000 @ 2160), (N/A, 40), (N/A)
655+
656+
phy#0 (self-managed)
607657
country 00: DFS-UNSET
608-
(2402 - 2472 @ 40), (6, 20), (N/A)
609-
(2457 - 2482 @ 20), (6, 20), (N/A), AUTO-BW, PASSIVE-SCAN
610-
(2474 - 2494 @ 20), (6, 20), (N/A), NO-OFDM, PASSIVE-SCAN
611-
(5170 - 5250 @ 80), (6, 20), (N/A), AUTO-BW, PASSIVE-SCAN
612-
(5250 - 5330 @ 80), (6, 20), (0 ms), DFS, AUTO-BW, PASSIVE-SCAN
613-
(5490 - 5730 @ 160), (6, 20), (0 ms), DFS, PASSIVE-SCAN
614-
(5735 - 5835 @ 80), (6, 20), (N/A), PASSIVE-SCAN
615-
(57240 - 63720 @ 2160), (N/A, 0), (N/A)
658+
(2402 - 2437 @ 40), (6, 22), (N/A), AUTO-BW, NO-HT40MINUS, NO-80MHZ, NO-160MHZ
659+
(2422 - 2462 @ 40), (6, 22), (N/A), AUTO-BW, NO-80MHZ, NO-160MHZ
660+
(2447 - 2482 @ 40), (6, 22), (N/A), AUTO-BW, NO-HT40PLUS, NO-80MHZ, NO-160MHZ
661+
(5170 - 5190 @ 160), (6, 22), (N/A), NO-OUTDOOR, AUTO-BW, IR-CONCURRENT, NO-HT40MINUS, PASSIVE-SCAN
662+
(5190 - 5210 @ 160), (6, 22), (N/A), NO-OUTDOOR, AUTO-BW, IR-CONCURRENT, NO-HT40PLUS, PASSIVE-SCAN
663+
(5210 - 5230 @ 160), (6, 22), (N/A), NO-OUTDOOR, AUTO-BW, IR-CONCURRENT, NO-HT40MINUS, PASSIVE-SCAN
664+
(5230 - 5250 @ 160), (6, 22), (N/A), NO-OUTDOOR, AUTO-BW, IR-CONCURRENT, NO-HT40PLUS, PASSIVE-SCAN
665+
(5250 - 5270 @ 160), (6, 22), (0 ms), DFS, AUTO-BW, NO-HT40MINUS, PASSIVE-SCAN
666+
(5270 - 5290 @ 160), (6, 22), (0 ms), DFS, AUTO-BW, NO-HT40PLUS, PASSIVE-SCAN
667+
...
668+
(5590 - 5610 @ 160), (6, 22), (0 ms), DFS, AUTO-BW, NO-HT40PLUS, PASSIVE-SCAN
669+
(5610 - 5630 @ 160), (6, 22), (0 ms), DFS, AUTO-BW, NO-HT40MINUS, PASSIVE-SCAN
670+
(5650 - 5670 @ 80), (6, 22), (0 ms), DFS, AUTO-BW, NO-HT40MINUS, NO-160MHZ, PASSIVE-SCAN
671+
(5670 - 5690 @ 80), (6, 22), (0 ms), DFS, AUTO-BW, NO-HT40PLUS, NO-160MHZ, PASSIVE-SCAN
672+
(5690 - 5710 @ 80), (6, 22), (0 ms), DFS, AUTO-BW, NO-HT40MINUS, NO-160MHZ, PASSIVE-SCAN
673+
(5710 - 5730 @ 80), (6, 22), (0 ms), DFS, AUTO-BW, NO-HT40PLUS, NO-160MHZ, PASSIVE-SCAN
674+
(5735 - 5755 @ 80), (6, 22), (N/A), AUTO-BW, IR-CONCURRENT, NO-HT40MINUS, NO-160MHZ, PASSIVE-SCAN
675+
(5755 - 5775 @ 80), (6, 22), (N/A), AUTO-BW, IR-CONCURRENT, NO-HT40PLUS, NO-160MHZ, PASSIVE-SCAN
676+
(5775 - 5795 @ 80), (6, 22), (N/A), AUTO-BW, IR-CONCURRENT, NO-HT40MINUS, NO-160MHZ, PASSIVE-SCAN
677+
(5795 - 5815 @ 80), (6, 22), (N/A), AUTO-BW, IR-CONCURRENT, NO-HT40PLUS, NO-160MHZ, PASSIVE-SCAN
678+
(5815 - 5835 @ 20), (6, 22), (N/A), AUTO-BW, IR-CONCURRENT, NO-HT40MINUS, NO-HT40PLUS, NO-80MHZ,NO-160MHZ, PASSIVE-SCAN
679+
```
680+
681+
<!-- Querying DBus Information -->
682+
683+
## Querying DBus Information
684+
685+
#### List DBus Peers
686+
687+
```Bash
688+
# The same output is displayed when just 'busctl' is run
689+
$ busctl list
690+
NAME PID PROCESS USER CONNECTION UNIT SESSION DESCRIPTION
691+
:1.0 719 systemd-resolve systemd-resolve :1.0 systemd-resolved.service - -
692+
:1.1 718 systemd-oomd systemd-oom :1.1 systemd-oomd.service - -
693+
:1.10 821 NetworkManager root :1.10 NetworkManager.service - -
694+
:1.103 4099 busctl lanforge :1.103 session-3.scope 3 -
695+
:1.15 1023 fwupd root :1.15 fwupd.service - -
696+
...
697+
```
698+
699+
#### Show DBus Peer Status
700+
701+
```Bash
702+
# Can specify using either the 'UniqueName' identifier
703+
# or the more human-readable DBus peer name
704+
#
705+
# Run just 'busctl status' to view main DBus socket status
706+
$ busctl status org.freedesktop.ModemManager1
707+
PID=805
708+
...
709+
Comm=ModemManager
710+
CommandLine=/usr/sbin/ModemManager --debug
711+
CGroup=/system.slice/ModemManager.service
712+
Unit=ModemManager.service
713+
Slice=system.slice
714+
...
715+
UniqueName=:1.7
716+
EffectiveCapabilities=cap_net_admin cap_sys_admin
717+
PermittedCapabilities=cap_net_admin cap_sys_admin
718+
InheritableCapabilities=cap_sys_admin
719+
BoundingCapabilities=cap_net_admin cap_sys_admin
720+
```
721+
722+
#### Show DBus Peer Object Tree
723+
724+
```Bash
725+
# Identifier is same as that used by 'busctl status'.
726+
# See above for more information.
727+
#
728+
# Can specify more than one peer to list. For example,
729+
# show trees for both the ':1.7' and ':1.10' peers:
730+
# busctl tree :1.7 :1.10
731+
#
732+
# Run just 'busctl' to view DBus tree for each DBus peer
733+
# active on the system (may take some time to fully complete)
734+
$ busctl tree org.freedesktop.ModemManager1
735+
└─ /org
736+
└─ /org/freedesktop
737+
└─ /org/freedesktop/ModemManager1
738+
├─ /org/freedesktop/ModemManager1/Modem
739+
│ └─ /org/freedesktop/ModemManager1/Modem/0
740+
└─ /org/freedesktop/ModemManager1/SIM
741+
└─ /org/freedesktop/ModemManager1/SIM/0
742+
```
743+
744+
#### Introspect DBus Peer Object
745+
746+
```Bash
747+
# Shows very verbose information on components of DBus peer object
748+
#
749+
# Here the object is '/org/freedesktop/ModemManager1/Modem/0', a cellular modem
750+
# configured by the 'org.freedesktop.ModemManager1' DBus peer
751+
$ busctl introspect org.freedesktop.ModemManager1 /org/freedesktop/ModemManager1/Modem/0
752+
NAME TYPE SIGNATURE RESULT/VALUE FLAGS
753+
...
754+
org.freedesktop.ModemManager1.Modem interface - - -
755+
.Command method su s -
756+
.CreateBearer method a{sv} o -
757+
.DeleteBearer method o - -
758+
.Enable method b - -
759+
...
760+
.CurrentBands property au 57 5 6 7 8 9 10 12 31 32 33 34 35 37 38… emits-change
761+
.CurrentCapabilities property u 204 emits-change
762+
.CurrentModes property (uu) 28 16 emits-change
763+
...
764+
.Drivers property as 2 "option" "qmi_wwan" emits-change
765+
.EquipmentIdentifier property s "XXXXXXXXXXXXXXX" emits-change
766+
.HardwareRevision property s "XXXXX" emits-change
767+
.Manufacturer property s "Quectel" emits-change
768+
...
769+
.MaxBearers property u 1 emits-change
770+
...
616771
```

0 commit comments

Comments
 (0)