Type IVa pili are filamentous cell surface structures observed in many bacteria. They pull cells forward by extending, adhering to surfaces, and then retracting. We used cryo–electron tomography of intact Myxococcus xanthus cells to visualize type IVa pili and the protein machine that assembles and retracts them (the type IVa pilus machine, or T4PM) in situ, in both the piliated and nonpiliated states, at a resolution of 3 to 4 nanometers. We found that T4PM comprises an outer membrane pore, four interconnected ring structures in the periplasm and cytoplasm, a cytoplasmic disc and dome, and a periplasmic stem. By systematically imaging mutants lacking defined T4PM proteins or with individual proteins fused to tags, we mapped the locations of all 10 T4PM core components and the minor pilins, thereby providing insights into pilus assembly, structure, and function.