The bacterial flagellum, which is responsible for motility, is a biological nanomachine consisting of a reversible rotary motor, a universal joint, a helical screw, and a protein export apparatus dedicated for flagellar assembly. The motor is fueled by an inward-directed electrochemical gradient of protons or sodium ions across the cytoplasmic membrane. The motor consists of a rotor, a drive shaft, a bushing, and about a dozen stator units. The flagellar protein export apparatus is located at the cytoplasmic side of the rotor. Interactions between the rotor and the stators and those between soluble and membrane components of the export apparatus are highly dynamic. The structures of flagellar basal body components including those of the export apparatus, being revealed at high resolution by X-ray crystallography and electron cryomicroscopy and cryotomography, are giving insights into their mechanisms.