Peter Bergen is the author of "United States of Jihad: Investigating America’s Homegrown Terrorists", a CNN National Security Analyst and Vice President at New America.

Terrorist events often involve perpetrators who are second-generation citizens of their countries. (Odd Andersen/Agence France-Presse)

Shortly after news broke last week about a possible terrorist attack in London, President Trump tweeted: “We need to be smart, vigilant and tough. We need the courts to give us back our rights. We need the Travel Ban as an extra level of safety!” Afterward, he was mocked for having exposed the seeming unconstitutionality of his ban, which the Supreme Court has been asked to review and which his lawyers have claimed is not a ban.

But as the London attacks made clear: The ban would do little to protect Americans from the kind of terrorism we mostly witness now, because it targets the wrong people. If Trump is concerned with jihadist terrorism, the threat doesn’t come from outsiders but from people who are already rightfully among us.

Over the past week, British authorities released the identities of the three London attackers, all of whom were killed by police. One of the terrorists was of Moroccan or Libyan descent and had a legal permit to live in the European Union. He had lived in Ireland for some time before marrying a British woman five years ago. Another was an Italian citizen of Moroccan heritage, and the third was a British citizen of Pakistani descent. This pattern isn’t uncommon: Over the past several years, deadly jihadist attacks in the West have been more commonly carried out by citizens and children of immigrants than by visitors or immigrants themselves.

Three of the four suicide bombers recruited by al-Qaeda who carried out the most lethal terrorist attack in British history, killing 52 people on the London transportation system on July 7, 2005, were British citizens. So, too, was the suicide bomber who struck an Ariana Grande concert in Manchester last month, killing 22 people; indeed, he was born in Manchester . And the terrorist who fatally rammed or stabbed five people in March on London’s Westminster Bridge was also a British citizen, born in the very English county of Kent.

The same pattern holds true in the United States. According to research by New America, a Washington-based think tank, of the 13 perpetrators of lethal jihadist terrorist attacks in the States since 9/11 (which killed a total of 94 people), all were American citizens or legal permanent residents. Of the 406 cases of jihadist terrorism (nonlethal and otherwise) in this country since Sept. 11, 2001, tracked by New America, more than 80 percent involved U.S. citizens or legal permanent residents.

None of the lethal terrorists were refugees, nor were any of them from any of the six countries the Trump administration wants to suspend travel from, nor were their families from any of those countries. Only one was a relatively recent immigrant (from Pakistan, which is not on the travel ban list).

There is no single pathway that turns someone into a terrorist who kills strangers in the name of Allah. Often, jihadist terrorists in the West are second-generation immigrants who do not feel quite fully British or French or American, but who also do not feel entirely at home in the cultures of their Middle Eastern or South Asian parents. This creates an identity crisis, which a small minority resolves by turning to a militant form of Islam that is usually quite distinct from the religious practices of their families. Some go on to embrace violent jihad. In this twisted strain of Islam, violent radicals are offered acceptance, respect — and even glory.

After the Manchester bombing, Trump characterized terrorists as “losers.” He is right about this: While many Western terrorists have middle-class backgrounds, they often resort to violent jihad because it can turn someone who’s going nowhere fast into a hero in his own story.

Take Tamerlan Tsarnaev, the Chechen American who masterminded the Boston Marathon attack in 2013. At the time of the bombing, Tsarnaev, who once had dreams of being an Olympic-level boxer, was unemployed and unemployable. Similarly, Omar Mateen, an American citizen born in New York who killed 49 at a gay nightclub in Orlando last year, had hoped to become a police officer, but he was kicked out of one police academy and rejected for admission to another. During his attack at the club, Mateen repeatedly told a police negotiator that he was a soldier of the Islamic State, a far more heroic persona than the retirement-community security guard that in fact he was. For individuals between cultures and without significant prospects for success, jihad offers a way to claim an impressive identity they might otherwise never possess.

There isn’t much hope of shutting out the forces that radicalize would-be terrorists. Many of the attackers in New America’s study were radicalized, at least in part, by materials they read online or through communications with other jihadist militants on the Internet; Trump’s travel ban would not, of course, block the Internet.

Consider the case of Nidal Hasan, an Army major born in Arlington, Va., who emailed the notorious U.S.-born cleric Anwar al-Awlaki more than a dozen times seeking religious sanction for an attack on his fellow soldiers. Hasan went on to kill 13 at Fort Hood, Tex., in 2009.

Before he died in a CIA drone strike in 2011, Awlaki declared, “Jihad is becoming as American as apple pie.” That seemingly absurd statement has a grain of truth: It is overwhelmingly American citizens and legal residents who are the perpetrators of jihadist terrorism in the States.

So if the travel ban won’t accomplish much, what might?

Last Sunday, British Prime Minister Theresa May suggested a solution while taking a swipe at social-media companies, declaring: “We cannot allow this ideology the safe space it needs to breed. Yet that is precisely what the Internet — and the big companies that provide Internet-based services — provide. We need to work with allied, democratic governments to reach international agreements that regulate cyberspace to prevent the spread of extremist and terrorism planning.”

That is easier said than done. The dominant social-media companies are based in the United States, where the First Amendment allows for much hateful but protected free speech. And when it comes to terrorism, the line between what is admissible on social-media platforms and what is not is much fuzzier than in cases such as child pornography, which is both illegal and easy to recognize.

Take the ambiguous case of the American cleric Awlaki, who gave countless rather anodyne lectures about Islam, which are available on YouTube, but also delivered a far smaller number of lectures calling for jihadist violence that clearly violate the “terms of use” of most social-media companies. Should all of Awlaki’s lectures be taken down, or just some of them?

Social-media companies such as Twitter have already removed hundreds of thousands of pro-terrorism accounts, and Facebook has hired thousands of employees to help take down problematic content. But purging all jihadist material from the Internet is a pipe dream. There is just too much of it. This problem is compounded because the Islamic State has moved a great deal of its social media to Telegram, an encrypted platform based in Germany that is beyond the reach of British or American laws.

Instead of a travel ban, the best way to deal with the scourge of jihadist terrorism is to enlist rather than alienate Muslim communities, because peers and family members are best positioned to notice radicalization or plotting. Indeed, Muslims already are leading anti-radicalization efforts in their communities, and we should support these efforts.

Still, members of the Muslim community started warning authorities about the radicalization of the Manchester terrorist, Salman Abedi, years ago, and his father was reportedly so concerned about his son’s state of mind that he confiscated his passport before the bombing. None of this, of course, was sufficient to deter Abedi from his deadly attack.

In an open society, there is nothing that can make us absolutely safe. Still, Trump’s travel ban would do nothing to address the most pressing terrorist threat we see today, which is the phenomenon of Western citizens and legal residents becoming radicalized by what they are watching and reading online.

This post has been updated.