Skip to content

Commit 1a4e85a

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

File tree

1 file changed

+193
-19
lines changed

1 file changed

+193
-19
lines changed

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

+193-19
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.
328354

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.
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.
356+
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,9 +602,28 @@ Connected to xx:xx:xx:xx:xx:xx (on wlan0)
569602
beacon int: 100
570603
```
571604

572-
#### Show WiFi Radio Info
605+
#### Show Channels Supported by WiFi Radio/Phy
606+
607+
```Bash
608+
$ iw phy0 channels
609+
Band 1:
610+
* 2412 MHz [1]
611+
Maximum TX power: 30.0 dBm
612+
Channel widths: 20MHz HT40+
613+
...
614+
* 2467 MHz [12] (disabled)
615+
* 2472 MHz [13] (disabled)
616+
* 2484 MHz [14] (disabled)
617+
Band 2:
618+
* 5180 MHz [36]
619+
Maximum TX power: 23.0 dBm
620+
Channel widths: 20MHz HT40+ VHT80
621+
...
622+
```
573623

574-
Includes channels and bands in current regulatory domain, ciphers, MCS rates, and antennas, among other things.
624+
#### Show All WiFi Radio/Phy Info
625+
626+
This information is very verbose and includes channels and bands in current regulatory domain, ciphers, MCS rates, and antennas, and more.
575627

576628
```Bash
577629
# Can also run 'iw phy phy0 info', but 'phy' is optional
@@ -601,16 +653,138 @@ $ iw phy0 info
601653

602654
#### Show Wireless Regulatory Domain Info
603655

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

0 commit comments

Comments
 (0)