Caenorhabditis elegans is a versatile model organism for exploring complex biological systems. Microbes and the external environment can affect the nervous system and drive behavioral changes in C. elegans. For better survival, C. elegans may develop behavioral immunity to avoid potential environmental pathogens. However, the molecular and cellular mechanisms underlying this avoidance behavior are not fully understood. The dissection of sensorimotor circuits in behavioral immunity may promote advancements in research on the neuronal connectome in uncovering neuronal regulators of behavioral immunity. In this review, we discuss how the nervous system coordinates behavioral immunity by translating various pathogen-derived cues and physiological damage to motor output in response to pathogenic threats in C. elegans. This understanding may provide insights into the fundamental principles of immune strategies that can be applied across species and potentially contribute to the development of novel therapies for immune-related diseases.