Skip to content

Commit bd6fb26

Browse files
committed
Update/add to Linux commands post
1 parent c038660 commit bd6fb26

File tree

1 file changed

+235
-20
lines changed

1 file changed

+235
-20
lines changed

content/blog/2023-12-linux-cmds.md

Lines changed: 235 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ These commands aren't necessarily Linux machine-specific, but they may come in h
2525
```Bash
2626
# Prints 'Vale la pena' once every half second
2727
$ watch -n 0.5 echo 'Vale la pena'
28+
29+
# For when 'watch' is not available
30+
$ while true; do echo 'Vale la pena'; sleep 1; clear; done
2831
```
2932

3033
#### View File Contents as File Updates
3134

3235
```Bash
33-
# The '-F' option may be useful as well. See 'man tail'
36+
# I usually run with '-F' instead of '-f', as '-f' will terminate
37+
# when a file is deleted (e.g. log file rotates). See 'man tail'.
3438
$ tail -f lanforge_log_1.txt
3539
```
3640

@@ -108,6 +112,12 @@ ssh-keygen -t ed25519
108112
# but this does it for you. Using 'scp':
109113
# cat .ssh/id_ed25519.pub | ssh user@server 'cat >> ~/.ssh/authorized_keys'
110114
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
115+
116+
# Port forwards the remote port to local system through a jumphost.
117+
# Here, the remote port is port 80 (generally HTTP) and the local
118+
# port is 8080. This would permit accessing the 'target-system'
119+
# webserver using the 'http://localhost:8080' URL.
120+
ssh -N -L 8080:target-system:80 user@jump-system
111121
```
112122

113123
<!-- System Administration: Non-Systemd -->
@@ -414,17 +424,33 @@ $ du -h
414424

415425
## Querying Network Information
416426

417-
**NOTE:** These commands are listed for diagnostic purposes.
418-
You probably want to use Network Manager to configure your networking instead.
419-
See the [next section](#managing-networking-network-manager) for more details.
427+
**NOTE:** These commands are listed for diagnostic purposes. You probably want to use NetworkManager to
428+
configure your system's networking instead. See the [next section](#managing-networking-networkmanager) for more details.
420429

421-
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.
430+
Most Linux distributions run NetworkManager 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.
422431

423432
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.
424433

425434
For more information, see this [very detailed guide](https://axil.gitlab.io/iproute2/).
426435

427-
#### Show Network Interface Link-Layer Info</h3>
436+
#### Show Network Interface Driver and Firmware
437+
438+
```Bash
439+
# Generally 'ethtool' is not installed on Linux systems by default
440+
$ ethtool -i enp2s0
441+
driver: r8169
442+
version: 6.14.8
443+
firmware-version: rtl8168h-2_0.0.2 02/26/15
444+
expansion-rom-version:
445+
bus-info: 0000:02:00.0
446+
supports-statistics: yes
447+
supports-test: no
448+
supports-eeprom-access: no
449+
supports-register-dump: yes
450+
supports-priv-flags: no
451+
```
452+
453+
#### Show Network Interface Link-Layer Info
428454

429455
Displays interface status and MAC address. More detailed output is possible by
430456
using the `-d` option (without the `-br` option).
@@ -439,6 +465,29 @@ enp2s0 DOWN xx:xx:xx:xx:xx:xx <NO-CARRIER,BROADCAST,MULTICAS
439465
wlan0 UP xx:xx:xx:xx:xx:xx <BROADCAST,MULTICAST,UP,LOWER_UP>
440466
```
441467

468+
#### Show Network Interface Link-Layer Stats
469+
470+
Statistics available depend on the device.
471+
472+
```Bash
473+
# Generally 'ethtool' is not installed on Linux systems by default
474+
$ ethtool -S enp2s0
475+
NIC statistics:
476+
tx_packets: 0
477+
rx_packets: 0
478+
tx_errors: 0
479+
rx_errors: 0
480+
rx_missed: 0
481+
align_errors: 0
482+
tx_single_collisions: 0
483+
tx_multi_collisions: 0
484+
unicast: 0
485+
broadcast: 0
486+
multicast: 0
487+
tx_aborted: 0
488+
tx_underrun: 0
489+
```
490+
442491
#### Show Network Interface IP-Layer Info
443492

444493
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).
@@ -493,6 +542,28 @@ cache
493542
$ ip route show table 10
494543
```
495544

545+
```Bash
546+
# Shorthand is 'ip r'. Will show all IPv4 routing tables.
547+
# The '169.254.0.0/16' route is link-local IPv4 and is mostly default.
548+
$ ip route
549+
default via 10.199.0.1 dev wlan0 proto dhcp metric 600
550+
10.199.0.0/20 dev wlan0 proto kernel scope link src 10.199.4.142 metric 600
551+
169.254.0.0/16 dev wlan0 scope link metric 1000
552+
553+
# Show IPv6 routing tables only
554+
$ ip -6 route
555+
::1 dev lo proto kernel metric 256 pref medium
556+
fe80::/64 dev wlan0 proto kernel metric 1024 pref medium
557+
558+
# Determine what route traffic will take for a specific address, here '8.8.8.8'
559+
$ ip route get 8.8.8.8
560+
8.8.8.8 via 10.199.0.1 dev wlan0 src 10.199.4.142 uid 1000
561+
cache
562+
563+
# Show routes in routing table 10
564+
$ ip route show table 10
565+
```
566+
496567
#### Show Open Sockets (Existing Connections)
497568

498569
I typically use this as `ss -tulpn` and `grep` for what I want. If you run as root (e.g. with `sudo`), you will also see the program that is using the socket.
@@ -514,19 +585,19 @@ udp UNCONN 0 0 0.0.0.0:631 0.0.0.0:* use
514585

515586
<!-- Managing Networking -->
516587

517-
## Managing Networking (Network Manager)
588+
## Managing Networking (NetworkManager)
518589

519-
At a high level, Network Manager configures 'connections' which are established using a backing network interface, or in Network Manager terms, 'device'. These network interfaces are either managed or unmanaged from Network Manager's perspective.
590+
At a high level, NetworkManager configures 'connections' which are established using a backing network interface, or in NetworkManager terms, 'device'. These network interfaces are either managed or unmanaged from NetworkManager's perspective.
520591

521-
Generally, most users will be fine with Network Manager configuring all of their
522-
network interfaces, typically only WiFi and Ethernet (although Network Manager can do much more). There are times, though, where it is appropriate to have Network Manager [ignore specific interfaces](#set-device-management), for example, when doing [WiFi packet capture](@/blog/2023-10-wifi-packet-capture.md).
592+
Generally, most users will be fine with NetworkManager configuring all of their
593+
network interfaces, typically only WiFi and Ethernet (although NetworkManager can do much more). There are times, though, where it is appropriate to have NetworkManager [ignore specific interfaces](#set-device-management), for example, when doing [WiFi packet capture](@/blog/2023-10-wifi-packet-capture.md).
523594

524-
This section details some of the most basic and more-useful (from my perspective) NetworkManager CLI commands. See `man nmcli-examples` for more examples and more advanced usage of Network Manager.
595+
This section details some of the most basic and more-useful (from my perspective) NetworkManager CLI commands. See `man nmcli-examples` for more examples and more advanced usage of NetworkManager.
525596

526597
#### Show All Devices
527598

528-
Shows all Network Manager-tracked network devices, including both devices
529-
that are managed and unmanaged by Network Manager.
599+
Shows all NetworkManager-tracked network devices, including both devices
600+
that are managed and unmanaged by NetworkManager.
530601

531602
```Bash
532603
# Shorthand shown. Full command is 'nmcli device'
@@ -619,7 +690,7 @@ $ nmcli c m SSID_NAME \
619690

620691
## Querying WiFi Information
621692

622-
**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.
693+
**NOTE:** Most Linux systems use NetworkManager to manage and configure networking, including WiFi. See the [Managing Networking](#managing-networking-networkmanager) section for more details.
623694

624695
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).
625696

@@ -630,7 +701,6 @@ While most will be content with `NetworkManager` managing their WiFi interface s
630701
Includes STA MAC, SSID, phy device, channel, frequency, transmit power.
631702

632703
```Bash
633-
# Can also run 'iw dev wlan0 info', but 'dev' is optional
634704
$ iw wlan0 info
635705
Interface wlan0
636706
ifindex 3
@@ -652,8 +722,9 @@ Interface wlan0
652722
Includes AP MAC (if station), SSID, frequency, bandwidth, RSSI (if station), and phy rate (MCS),
653723
among other things. Phy rate may or may not include NSS.
654724

725+
I often run this command in a loop while doing other testing using the commands in [this section](#run-command-every-n-seconds).
726+
655727
```Bash
656-
# Can also run 'iw dev wlan0 link', but 'dev' is optional
657728
# Anonymized MAC here is the AP's BSSID
658729
$ iw wlan0 link
659730
Connected to xx:xx:xx:xx:xx:xx (on wlan0)
@@ -670,6 +741,54 @@ Connected to xx:xx:xx:xx:xx:xx (on wlan0)
670741
beacon int: 100
671742
```
672743

744+
#### Show WiFi Interface Station Information
745+
746+
**NOTE:** This command will only show meaningful output when the WiFi interface is connected (associated).
747+
748+
This is effectively a combination of the `iw wlan0 info` and `iw wlan0 link` commands. For station interfaces,
749+
this lists information related to the AP the station is associated with. In 802.11 terminology,
750+
the station and AP are both stations, with the former (e.g. phone, laptop) being a non-AP station.
751+
752+
For AP interfaces, this list information for all stations associated to that AP interface.
753+
754+
```Bash
755+
# Anonymized MAC here is the AP's BSSID
756+
$ iw wlan0 station dump
757+
Station xx:xx:xx:xx:xx:xx (on wlan0)
758+
inactive time: 2682 ms
759+
rx bytes: 47808905
760+
rx packets: 45274
761+
tx bytes: 11417769
762+
tx packets: 25222
763+
tx retries: 3001
764+
tx failed: 0
765+
beacon loss: 0
766+
rx drop misc: 0
767+
signal: -42 [-43, -46] dBm
768+
signal avg: -40 [-35, -45] dBm
769+
tx bitrate: 51.6 MBit/s 40MHz HE-MCS 2 HE-NSS 1 HE-GI 0 HE-DCM 0
770+
tx duration: 3694323 us
771+
rx bitrate: 458.8 MBit/s 40MHz HE-MCS 9 HE-NSS 2 HE-GI 0 HE-DCM 0
772+
rx duration: 3871820 us
773+
last ack signal:-39 dBm
774+
avg ack signal: -39 dBm
775+
airtime weight: 256
776+
authorized: yes
777+
authenticated: yes
778+
associated: yes
779+
preamble: long
780+
WMM/WME: yes
781+
MFP: yes
782+
TDLS peer: no
783+
DTIM period: 3
784+
beacon interval:100
785+
short slot time:yes
786+
connected time: 777 seconds
787+
associated at [boottime]: 13.231s
788+
associated at: 1760385417521 ms
789+
current time: 1760386194555 ms
790+
```
791+
673792
#### Show Channels Supported by WiFi Radio/Phy
674793

675794
```Bash
@@ -694,8 +813,7 @@ Band 2:
694813
This information is very verbose and includes channels and bands in current regulatory domain, ciphers, MCS rates, and antennas, and more.
695814

696815
```Bash
697-
# Can also run 'iw phy phy0 info', but 'phy' is optional
698-
# Will show a 'Band 3', if supports 2.4 GHz, 5 GHz, and 6 GHz
816+
# Will show a 'Band 3', if supports 2.4 GHz, 5 GHz, and 6 GHz (varies by radio)
699817
$ iw phy0 info
700818
...
701819
Supported Ciphers:
@@ -721,10 +839,16 @@ $ iw phy0 info
721839

722840
#### Show Wireless Regulatory Domain Info
723841

724-
You may see a combination of 'global' and per-phy regulatory configuration. The following shows both.
842+
You may see a combination of system ('global') and per-radio ('self-managed') regulatory configuration.
843+
The following shows both.
844+
845+
Per-radio regulatory configuration refers to regulatory domain configuration that the radio configures
846+
on its own in the firmware/driver. Based on present conditions (e.g. beacons), the radio will override
847+
the system regulatory domain, restricting the user to what the radio permits. In my experience, this
848+
generally affects Intel radios (e.g. AX210, BE200).
725849

726850
```Bash
727-
# Notice the inclusion of sub-1GHz (WiFi HaLow, 802.11ah) and 60GHz (WiGig) spectrum
851+
# Note the inclusion of sub-1GHz (WiFi HaLow, 802.11ah) and 60GHz (WiGig) spectrum
728852
$ iw reg get
729853
global
730854
country US: DFS-FCC
@@ -765,6 +889,97 @@ country 00: DFS-UNSET
765889
(5815 - 5835 @ 20), (6, 22), (N/A), AUTO-BW, IR-CONCURRENT, NO-HT40MINUS, NO-HT40PLUS, NO-80MHZ,NO-160MHZ, PASSIVE-SCAN
766890
```
767891

892+
## WiFi Configuration (Manual)
893+
894+
This section is more advanced and the commands here will almost certainly interfere
895+
with any running network configuration daemons (e.g. NetworkManager).
896+
897+
#### Add Virtual WiFi Interface
898+
899+
The number and type of virtual interfaces varies both in terms of radio as well as driver/kernel.
900+
901+
```Bash
902+
# Add station
903+
$ iw phy0 interface add wlan0 type managed
904+
905+
# Add monitor
906+
$ iw phy0 interface add moni0 type monitor
907+
908+
# Add AP
909+
# When using 'hostapd', the interface to use can be a station (managed).
910+
# The program will change the type as needed.
911+
$ iw phy0 interface add moni0 type __ap
912+
```
913+
914+
#### Change Virtual WiFi Interface Type
915+
916+
```Bash
917+
# Change to station
918+
$ iw wlan0 set type managed
919+
920+
# Change to monitor
921+
$ iw wlan0 set type monitor
922+
923+
# Change to AP
924+
$ iw wlan0 set type __ap
925+
```
926+
927+
#### Delete Virtual WiFi Interface
928+
929+
```Bash
930+
$ iw wlan0 del
931+
```
932+
933+
#### Trigger WiFi Interface Scan
934+
935+
Combinations of the following are permissible. See the help output of `iw` for more info/options.
936+
937+
```Bash
938+
# General scan, will send probe requests
939+
$ iw wlan0 scan
940+
941+
# Scan on specific frequency/frequencies
942+
$ iw wlan0 scan freq 2412 5180
943+
944+
# Directed scan for specific SSID (exclusive with passive scan)
945+
$ iw wlan0 scan ssid SSIDNAME
946+
947+
# Passive scan, no probe requests (exclusive with specific SSID)
948+
$ iw wlan0 scan passive
949+
```
950+
951+
#### Rename WiFi Radio/Phy
952+
953+
```Bash
954+
$ iw phy0 set name wiphy0
955+
```
956+
957+
#### Move WiFi Radio/Phy to Network Namespace
958+
959+
**Exercise caution here**, as this will make the radio (and any virtual interfaces, e.g. 'wlan0')
960+
visible only within the context of the network namespace. In other words, any WiFi interfaces
961+
for the radio will no longer be visible via 'ip' unless you're running within the network namespace.
962+
To move the radio back to the non-network namespace use, you will need to delete the network namespace.
963+
964+
Interestingly, network namespaces names don't exist in the kernel, only ID. Named network namespaces
965+
appear to be a construct introduced by the 'iproute2' package (`ip` command) that is now defacto standard.
966+
When creating named network namespaces, `ip` bind mounts to `/var/run/netns/` as part of the process.
967+
More details [here](https://7bits.nl/journal/posts/what-does-ip-netns-add-actually-do/).
968+
969+
```Bash
970+
# Specify netns by name
971+
$ iw phy0 set netns name mynetns
972+
973+
# Specify netns by ID
974+
$ iw phy0 set netns $NETNS_ID
975+
976+
# Show interfaces in network namespace (generally requires root permissions)
977+
$ ip netns exec $NETNS_NAME ip link show
978+
979+
# Make radio available for non-network namespace use (requires deleting network namespace)
980+
$ ip netns delete $NETNS_NAME
981+
```
982+
768983
<!-- Querying DBus Information -->
769984

770985
## Querying DBus Information

0 commit comments

Comments
 (0)