Skip to content

Commit 5643e70

Browse files
committed
add Linux sysrq
1 parent d8bae5b commit 5643e70

File tree

1 file changed

+243
-0
lines changed

1 file changed

+243
-0
lines changed

linux/kernel-sysrq.md

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
---
2+
title: "Linux Magic System Request Key Hacks"
3+
date: "2024-12-05T09:44:34+07:00"
4+
tags: ["linux", "tips", "tech"]
5+
comments: true
6+
toc: true
7+
draft: false
8+
---
9+
10+
Source: <https://www.kernel.org/doc/html/latest/admin-guide/sysrq.html>
11+
12+
## Giới thiệu
13+
14+
Trong quá trình vận hành, bạn đã bao giờ gặp tình trạng hệ thống Linux của mình bị "treo" hoặc không phản hồi? Khi đó, hãy sử dụng **Magic System Request Key (SysRq)** để được cứu rỗi. Vậy nó là gì và có thể làm gì?
15+
16+
SysRq là một tính năng của Linux, cho phép người dùng gửi "tín hiệu cầu cứu" trực tiếp đến kernel của hệ điều hành.
17+
18+
{{< figure class="figure" caption="sysrq - from trufflesecurity.com" src="https://framerusercontent.com/images/wjLSwytVCtdnGhq2xK8m6qSo4.png" >}}
19+
20+
## Cấu hình SysRq
21+
22+
Để cấu hình SysRq, bạn có thể sử dụng command sau:
23+
24+
```shell
25+
echo "number" >/proc/sys/kernel/sysrq
26+
```
27+
28+
Giá trị của "number" có thể nằm trong các trường hợp sau:
29+
30+
```shell
31+
0 - disable sysrq completely
32+
1 - enable all functions of sysrq
33+
>1 – bitmask to allow specific sysrq functions
34+
2 = 0x2 - enable control of console logging level
35+
4 = 0x4 - enable control of keyboard (SAK, unraw)
36+
8 = 0x8 - enable debugging dumps of processes etc.
37+
16 = 0x10 - enable sync command
38+
32 = 0x20 - enable remount read-only
39+
64 = 0x40 - enable signalling of processes (term, kill, oom-kill)
40+
128 = 0x80 - allow reboot/poweroff
41+
256 = 0x100 - allow nicing of all RT tasks
42+
```
43+
44+
Kiểm tra giá trị hiện tại của sysrq:
45+
46+
```shell
47+
root@vm1:/home/kien# cat /proc/sys/kernel/sysrq
48+
176
49+
# 176 không match với giá trị nào, well, thực ra 176 ở đây là 16+32+128 = 176
50+
#
51+
# 16 = 0x10 - enable sync command
52+
# 32 = 0x20 - enable remount read-only
53+
# 128 = 0x80 - allow reboot/poweroff
54+
55+
# Để test, bật hết lên cho đơn giản
56+
root@vm1:/home/kien# root@vm1:/home/kien# echo 1 > /proc/sys/kernel/sysrq
57+
```
58+
59+
## Cách sử dụng
60+
61+
Bạn có thể sử dụng tính năng bằng cách ấn tổ hợp phím (tùy thuộc hệ điều hành, đối với x86 là `ALT-SysRq-<command key>`, bàn phím của bạn thường sẽ có SysRq keyboard, để ý nhé) hoặc echo ký tự commands vào `/proc/sysrq-trigger`.
62+
63+
```shell
64+
echo <command key> > /proc/sysrq-trigger
65+
```
66+
67+
{{< details title="Danh sách command key (ấn để show all)" open=false >}}
68+
<table class="docutils align-default">
69+
<thead>
70+
<tr class="row-odd"><th class="head"><p>Command</p></th>
71+
<th class="head"><p>Function</p></th>
72+
</tr>
73+
</thead>
74+
<tbody>
75+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">b</span></code></p></td>
76+
<td><p>Will immediately reboot the system without syncing or unmounting
77+
your disks.</p></td>
78+
</tr>
79+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">c</span></code></p></td>
80+
<td><p>Will perform a system crash and a crashdump will be taken
81+
if configured.</p></td>
82+
</tr>
83+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">d</span></code></p></td>
84+
<td><p>Shows all locks that are held.</p></td>
85+
</tr>
86+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">e</span></code></p></td>
87+
<td><p>Send a SIGTERM to all processes, except for init.</p></td>
88+
</tr>
89+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">f</span></code></p></td>
90+
<td><p>Will call the oom killer to kill a memory hog process, but do not
91+
panic if nothing can be killed.</p></td>
92+
</tr>
93+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">g</span></code></p></td>
94+
<td><p>Used by kgdb (kernel debugger)</p></td>
95+
</tr>
96+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">h</span></code></p></td>
97+
<td><p>Will display help (actually any other key than those listed
98+
here will display help. but <code class="docutils literal notranslate"><span class="pre">h</span></code> is easy to remember :-)</p></td>
99+
</tr>
100+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">i</span></code></p></td>
101+
<td><p>Send a SIGKILL to all processes, except for init.</p></td>
102+
</tr>
103+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">j</span></code></p></td>
104+
<td><p>Forcibly “Just thaw it” - filesystems frozen by the FIFREEZE ioctl.</p></td>
105+
</tr>
106+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">k</span></code></p></td>
107+
<td><p>Secure Access Key (SAK) Kills all programs on the current virtual
108+
console. NOTE: See important comments below in SAK section.</p></td>
109+
</tr>
110+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">l</span></code></p></td>
111+
<td><p>Shows a stack backtrace for all active CPUs.</p></td>
112+
</tr>
113+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">m</span></code></p></td>
114+
<td><p>Will dump current memory info to your console.</p></td>
115+
</tr>
116+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">n</span></code></p></td>
117+
<td><p>Used to make RT tasks nice-able</p></td>
118+
</tr>
119+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">o</span></code></p></td>
120+
<td><p>Will shut your system off (if configured and supported).</p></td>
121+
</tr>
122+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">p</span></code></p></td>
123+
<td><p>Will dump the current registers and flags to your console.</p></td>
124+
</tr>
125+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">q</span></code></p></td>
126+
<td><p>Will dump per CPU lists of all armed hrtimers (but NOT regular
127+
timer_list timers) and detailed information about all
128+
clockevent devices.</p></td>
129+
</tr>
130+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">r</span></code></p></td>
131+
<td><p>Turns off keyboard raw mode and sets it to XLATE.</p></td>
132+
</tr>
133+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">s</span></code></p></td>
134+
<td><p>Will attempt to sync all mounted filesystems.</p></td>
135+
</tr>
136+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">t</span></code></p></td>
137+
<td><p>Will dump a list of current tasks and their information to your
138+
console.</p></td>
139+
</tr>
140+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">u</span></code></p></td>
141+
<td><p>Will attempt to remount all mounted filesystems read-only.</p></td>
142+
</tr>
143+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">v</span></code></p></td>
144+
<td><p>Forcefully restores framebuffer console</p></td>
145+
</tr>
146+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">v</span></code></p></td>
147+
<td><p>Causes ETM buffer dump [ARM-specific]</p></td>
148+
</tr>
149+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">w</span></code></p></td>
150+
<td><p>Dumps tasks that are in uninterruptible (blocked) state.</p></td>
151+
</tr>
152+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">x</span></code></p></td>
153+
<td><p>Used by xmon interface on ppc/powerpc platforms.
154+
Show global PMU Registers on sparc64.
155+
Dump all TLB entries on MIPS.</p></td>
156+
</tr>
157+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">y</span></code></p></td>
158+
<td><p>Show global CPU Registers [SPARC-64 specific]</p></td>
159+
</tr>
160+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">z</span></code></p></td>
161+
<td><p>Dump the ftrace buffer</p></td>
162+
</tr>
163+
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">0</span></code>-<code class="docutils literal notranslate"><span class="pre">9</span></code></p></td>
164+
<td><p>Sets the console log level, controlling which kernel messages
165+
will be printed to your console. (<code class="docutils literal notranslate"><span class="pre">0</span></code>, for example would make
166+
it so that only emergency messages like PANICs or OOPSes would
167+
make it to your console.)</p></td>
168+
</tr>
169+
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">R</span></code></p></td>
170+
<td><p>Replay the kernel log messages on consoles.</p></td>
171+
</tr>
172+
</tbody>
173+
</table>
174+
{{</ details >}}
175+
176+
## Một số command keys hữu ích
177+
178+
> Để dễ hình dung, ví dụ dưới đây sẽ sử dụng phương án echo ký tự vào /proc/sysrq-trigger
179+
180+
### Poweroff
181+
182+
```shell
183+
root@vm1:/home/kien# echo o > /proc/sysrq-trigger
184+
```
185+
186+
### Reboot
187+
188+
```shell
189+
root@vm1:/home/kien# echo b > /proc/sysrq-trigger
190+
```
191+
192+
### Crash
193+
194+
Trigger crashdump thủ công nếu hệ thống bị treo. Đây cũng là một cách hay để giả lập kernel crashdump.
195+
196+
```shell
197+
root@vm1:/home/kien# echo c > /proc/sysrq-trigger
198+
```
199+
200+
### Đồng bộ filesystems
201+
202+
```shell
203+
root@vm1:/home/kien# echo s > /proc/sysrq-trigger
204+
```
205+
206+
### Remount filesystem read-only
207+
208+
```shell
209+
root@vm1:/home/kien# echo u > /proc/sysrq-trigger
210+
root@vm1:/home/kien# touch abc
211+
touch: cannot touch 'abc': Read-only file system
212+
```
213+
214+
### Kill tất cả processes trừ tiến trình init
215+
216+
Command key này đặc biệt hữu ích nếu bạn có tiến trình không thể kill, đặc biệt nếu tiến trình đó liên tục spawning ra các tiến trình khác.
217+
218+
```shell
219+
# Linux gửi SIGTERM đến tất cả các processes, trừ init
220+
root@vm1:/home/kien# echo e > /proc/sysrq-trigger
221+
222+
# Linux gửi SIGKILL đến tất cả các processes, trừ init
223+
root@vm1:/home/kien# echo i > /proc/sysrq-trigger
224+
```
225+
226+
### Gọi OOM Killer
227+
228+
OOM Killer được gọi và hoàn thành nhiệm vụ của nó - kill tiến trình gây high memory usage.
229+
230+
```shell
231+
root@vm1:/home/kien# echo f > /proc/sysrq-trigger
232+
# Check kern.log để kiểm tra log
233+
3585:Dec 5 03:31:40 vm1 kernel: [ 195.899186] sysrq: Manual OOM execution
234+
3586:Dec 5 03:31:40 vm1 kernel: [ 195.901176] kworker/0:1 invoked oom-killer: gfp_mask=0xcc0(GFP_KERNEL), order=-1, oom_score_adj=0
235+
```
236+
237+
### Xem danh sách blocked state processes
238+
239+
```shell
240+
root@vm1:/home/kien# echo w > /proc/sysrq-trigger
241+
# Check kern.log để kiểm tra
242+
Dec 5 03:33:02 vm1 kernel: [ 277.781446] sysrq: Show Blocked State
243+
```

0 commit comments

Comments
 (0)