As always reference the LinuxCNC manuals and the LinuxCNC Wiki for more information.

G code

G-code is the programming language of CNC machines, it is called that because many of the the commands take the form of a letter G followed by a cryptic number. Each number does a different thing. Despite being called G-code, every letter of the alphabet is a command in G-code. A G-code program takes the form of a list of single-character commands and associated numbers which may be on separate lines or combined on the same line (called a "block")


The very first thing you need to do after turning on LinuxCNC is home the machine even if you don’t have homing switches.

Homing a machine sets up the G53 Machine Origin. LinuxCNC uses this information along with your limits set in your ini configuration to enforce the soft limits you have set up for your machine. This is also used for tool offsets.

Most hobby sized mill and lathes don’t have home switches. What you do is make some match marks for each axis. Jog the axis each direction to the position you want the soft limits to be and note the DRO readout. Set up your soft limits in your ini using the information you gathered so when you home the axis at the match marks you can not jog into a hard stop. When starting up LinuxCNC jog each axis to the match marks and home that axis. Before shutting down LinuxCNC (provided there are no obstructions) issue G53 G0 X0 Y0 Z0 (G53 G0 X0 Z0 for lathes) to move to the machine origin. Next time you start LinuxCNC you should be close to the match marks if you didn’t manually move the machine while it was off.

G code Preamble

The G code preamble sets up a known working environment for the machine. It is important to specify modal codes in the preamble that might be left in a different state than desired from a previous G code file. The preamble is usually the first line in your G code file after any file description comments.


The general form for comments is text surrounded by parentheses or preceded by a semi-colon.

G0 X0 (a comment in a line with G code)
; a comment line where everything after the semi-colon is a comment.

Lathe Diameter Mode

If this is a Lathe the diameter mode should be set in the preamble. G7 is for Diameter measurements and G8 is for radius measurements for the X axis. This setting only affects the X axis on lathes.


No matter what units your configuration was made in you should specify the units used by the interpreter in the G code file. G20 is for inch measurements and G21 is for millimeter measurements.

Active Plane

The active plane should be specified in the preamble. Normally for a mill you use G17 the X-Y plane and for lathes G18 the X-Z plane. There are some exceptions for using the G18 plane on a lathe. Currently canned cycles only work in the G17 plane and the only ones that make sense are drilling, boring and rigid tapping cycles done at X0 on a lathe.

Tool Diameter Compensation

Tool diameter compensation should be turned off in the preamble in case it was left on by a poorly written G code file. G40 turns off tool diameter compensation.

Tool Length Compensation

Tool length compensation should be turned off in the preamble in case it was left on by a poorly written G code file. G49 turns off tool length compensation.

Coordinate System

The coordinate system desired should be specified in the preamble. Normally it is the G54 coordinate system.

Path Blending

The path blending desired should be set in the preamble. The default of G64 may have large deviations in programmed path and should be avoided. Using G64 Pn.n will give you the best possible path and stay within the P value.

Canned Cycles

Canned cycles should be turned off in the preamble in case they were left on by a poorly written G code file. G80 turns off canned cycles.

Distance Mode

The Distance Mode defines how the axis words are interpreted. G90 means distance from the axis zero. G91 means distance from the current point. Leaving this out could result in a wildly different interpretation of the G code file.

Coordinate System Offsets

The G92 coordinate system offset is persistent between runs of LinuxCNC. Resetting the G92 coordinate system in the preamble will prevent surprises.

Feed Rate Mode

There are three possible Feed Rate Modes and typically G94 Units per Minute is the mode used.

Preamble Examples

Sample Mill Preamble
G20 G17 G40 G49 G64 P0.005 G80 G90 G92.1 G94
Sample Lathe Preamble
G7 G20 G18 G40 G49 G64 P0.005 G80 G90 G92.1 G94

Modal G codes

Most G codes are modal meaning if you have the G code in effect it will remain in effect until you change it. In the following bit of code the linear feed G1 and the feed rate F will remain in effect until changed. In the example the first two lines will be at a feed rate of 25 and the second two lines will be at the feed rate of 10.

Sample Modal G code
G1 X1.2 F25
X0.0 F10