The microbiota promotes resistance to respiratory infection, but the mechanistic basis for this is poorly defined. Here, we identify members of the microbiota that protect against respiratory infection by the major human pathogens Streptococcus pneumoniae and Klebsiella pneumoniae. We show that the microbiota enhances respiratory defenses via granulocyte-macrophage colony-stimulating factor (GM-CSF) signaling, which stimulates pathogen killing and clearance by alveolar macrophages through extracellular signal-regulated kinase signaling. Increased pulmonary GM-CSF production in response to infection is primed by the microbiota through interleukin-17A. By combining models of commensal colonization in antibiotic-treated and germ-free mice, using cultured commensals from the Actinobacteria, Bacteroidetes, Firmicutes, and Proteobacteria phyla, we found that potent Nod-like receptor-stimulating bacteria in the upper airway (Staphylococcus aureus and Staphylococcus epidermidis) and intestinal microbiota (Lactobacillus reuteri, Enterococcus faecalis, Lactobacillus crispatus and Clostridium orbiscindens) promote resistance to lung infection through Nod2 and GM-CSF. Our data reveal the identity, location, and properties of bacteria within the microbiota that regulate lung immunity, and delineate the host signaling axis they activate to protect against respiratory infection.