# LinuxCNC M2

Instructions · `M2` · verification: source checked

M2 is a G-code program end command in LinuxCNC. It terminates the program and returns the machine to a defined modal state: Auto mode changes to MDI, offsets and plane reset to defaults, distance mode becomes absolute, feed mode becomes units per minute, overrides are enabled, cutter compensation is cancelled, the spindle stops, motion mode becomes feed, and coolant turns off. Pressing Cycle Start restarts the program from the beginning of the file.

## Specification

| Property | Value |
|---|---|
| Language | gcode |
| Code | M2 |
| Group | Program End |
| Syntax | M2 |
| Dialect | LinuxCNC |

## Connected

- Related: [LinuxCNC M73 Save and Autorestore Modal State](https://machineschema.com/instruction/mcode-m73.md), [LinuxCNC G1 Linear Move](https://machineschema.com/instruction/gcode-g01.md)

## Safety

- Lines of code after M2/M30 will not be executed; pressing Cycle Start will start the program at the beginning of the file.
- Using % to wrap the G-code does not do the same thing as a Program End; see the File Requirements section.
- The spindle is stopped by M2 (like M5).

## Study notes

- Pressing Cycle Start ("R" in the Axis GUI) restarts the program at the beginning of the file.
- M2 changes the machine from Auto mode to MDI mode.
- Origin offsets are set to the default (like G54).
- Selected plane is set to the XY plane (like G17).
- Distance mode is set to absolute mode (like G90).
- Feed rate mode is set to units per minute (like G94).
- Feed and speed overrides are set to ON (like M48).
- Cutter compensation is turned off (like G40).
- The current motion mode is set to feed (like G1).
- Coolant is turned off (like M9).

## Sources

1. [M-Codes](https://linuxcnc.org/docs/html/gcode/m-code.html#mcode:m2-m30) (accessed 2026-10-26)

---

Source page: https://machineschema.com/instruction/mcode-m02  
JSON: https://machineschema.com/instruction/mcode-m02.json  
License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Cite Machine Schema and the original sources.
