“Many of them just take their grievances and dress them up in the garb of Islam,” Mr. Bergen said.

That has become easy in the age of the internet. The attackers in San Bernardino, Orlando and New York all had expressed support for the Islamic State, and they and the Boston bombers were devotees of the voluminous online work of Mr. Awlaki, who was killed in an American drone strike in 2011. His arguments remain highly popular on the web, where he urged Western Muslims to reject even the friendliest non-Muslim neighbors, whom he called “Sally Soccer Mom and Joe Six-Pack.” Mr. Rahami wrote in his journal that “Sheikh Anwar,” as well as Mr. Adnani of the Islamic State, had “said it clearly”: “Attack the kuffar,” or non-Muslims, “in their backyard.”

Farhad Khosrokhavar, a sociologist at the École des Hautes Études en Sciences Sociales in Paris and the author of “Radicalization,” said Muslims in Europe more often than those in America felt “frontally rejected” by the larger society. He said he had often seen in his research individuals who felt neither French nor Arab.

Image Anwar al-Awlaki, the American-born recruiter for Al Qaeda, remains highly popular on the web, where he has argued that Western Muslims must reject even the friendliest non-Muslim neighbors. Credit... Site Intelligence Group, via Associated Press

“In France, they are blamed for not being French enough, and when they go to their parents’ country of origin, they are blamed for not being Arab enough,” Mr. Khosrokhavar said. “That double denial can push them to adhere to a radical version of Islam, as a kind of lifeline: Since I am neither French nor Arab, neither American nor Afghan, I am Muslim and to hell with you all,” he added.

These roots of radicalization do not make immigrants in general a danger. In the United States, immigrants have a lower rate of crime and violence than other Americans. Converts to Islam are disproportionately represented among Americans and Europeans drawn to extremism, and other ideologies also motivate mass violence — as in the case of Dylann Roof, who was 21 when he fatally shot nine black people last year at a church in Charleston, S.C., in the name of white supremacy.

“The actual content of the ideology is secondary,” said Mr. Meloy, the psychologist. “What’s important is the identification and fixation.”

But the Islamist terrorist groups target the particular anxieties of Western Muslims from immigrant backgrounds, posing recruitment as a religious loyalty test. They call on supporters to reject the nations where they live and embrace instead a devotion to the ummah, the global community of Muslims. The West is at war with Islam, they say, and you must strike out to defend your fellow Muslims.