Animals coexist in commensal, pathogenic or mutualistic relationships with complex communities of diverse organisms, including microorganisms1. Some bacteria produce bioactive neurotransmitters that have previously been proposed to modulate nervous system activity and behaviours of their hosts2,3. However, the mechanistic basis of this microbiota-brain signalling and its physiological relevance are largely unknown. Here we show that in Caenorhabditis elegans, the neuromodulator tyramine produced by commensal Providencia bacteria, which colonize the gut, bypasses the requirement for host tyramine biosynthesis and manipulates a host sensory decision. Bacterially produced tyramine is probably converted to octopamine by the host tyramine β-hydroxylase enzyme. Octopamine, in turn, targets the OCTR-1 octopamine receptor on ASH nociceptive neurons to modulate an aversive olfactory response. We identify the genes that are required for tyramine biosynthesis in Providencia, and show that these genes are necessary for the modulation of host behaviour. We further find that C. elegans colonized by Providencia preferentially select these bacteria in food choice assays, and that this selection bias requires bacterially produced tyramine and host octopamine signalling. Our results demonstrate that a neurotransmitter produced by gut bacteria mimics the functions of the cognate host molecule to override host control of a sensory decision, and thereby promotes fitness of both the host and the microorganism.