The bacterial flagellum is a macromolecular protein complex that enables motility in many species. Bacterial flagella self-assemble a strong, multicomponent drive shaft that couples rotation in the inner membrane to the micrometre-long flagellar filament that powers bacterial swimming in viscous fluids1-3. Here, we present structures of the intact Salmonella flagellar basal body4, encompassing the inner membrane rotor, drive shaft and outer-membrane bushing, solved using cryo-electron microscopy to resolutions of 2.2-3.7 Å. The structures reveal molecular details of how 173 protein molecules of 13 different types assemble into a complex spanning two membranes and a cell wall. The helical drive shaft at one end is intricately interwoven with the rotor component with both the export gate complex and the proximal rod forming interactions with the MS-ring. At the other end, the drive shaft distal rod passes through the LP-ring bushing complex, which functions as a molecular bearing anchored in the outer membrane through interactions with the lipopolysaccharide. The in situ structure of a protein complex capping the drive shaft provides molecular insights into the assembly process of this molecular machine.