Neuronal connections provide the scaffolding for neuronal function. Revealing the connectivity of functionally identified individual neurons is necessary to understand how activity patterns emerge and support behavior. Yet, the brain-wide presynaptic wiring rules that lay the foundation for the functional selectivity of individual neurons remain largely unexplored. Cortical neurons, even in primary sensory cortex, are heterogeneous in their selectivity, not only to sensory stimuli but also to multiple aspects of behavior. Here, to investigate presynaptic connectivity rules underlying the selectivity of pyramidal neurons to behavioral state 1-12 in primary somatosensory cortex (S1), we used two-photon calcium imaging, neuropharmacology, single-cell based monosynaptic input tracing, and optogenetics. We show that behavioral state-dependent neuronal activity patterns are stable over time. These are minimally affected by neuromodulatory inputs and are instead driven by glutamatergic inputs. Analysis of brain-wide presynaptic networks of individual neurons with distinct behavioral state-dependent activity profiles revealed characteristic patterns of anatomical input. While both behavioral state-related and unrelated neurons had a similar pattern of local inputs within S1, their long-range glutamatergic inputs differed. Individual cortical neurons, irrespective of their functional properties, received converging inputs from the main S1-projecting areas. Yet, neurons that tracked behavioral state received a smaller proportion of motor cortical inputs and a larger proportion of thalamic inputs. Optogenetic suppression of thalamic inputs reduced behavioral state-dependent activity in S1, but this activity was not externally driven. Our results revealed distinct long-range glutamatergic inputs as a substrate for preconfigured network dynamics associated with behavioral state.