How complex, multi-component macromolecular machines evolved remains poorly understood. We will discuss the evolutionary origins of the chemosensory machinery that controls flagellar motility in Escherichia coli. We first identified ancestral forms still present in Vibrio cholerae, Pseudomonas aeruginosa, Shewanella oneidensis and Methylomicrobium alcaliphilum, characterizing their structures by electron cryotomography and finding evidence that they function in a stress response pathway. Using bioinformatics, we then traced the evolution of the system through γ-Proteobacteria, pinpointing key evolutionary events that led to the machine now seen in E. coli. Our results suggest that two ancient chemosensory systems with different inputs and outputs (F6 and F7) existed contemporaneously, with one (F7) ultimately taking over the inputs and outputs of the other (F6), which was subsequently lost.