With cars being torched and mosques and hotels housing asylum seekers under attack, the riots that swept Britain over the past two weeks have posed the first direct challenge to the new prime minister, Keir Starmer.

But even if the violence has subsided, for now, at least, the shocking scenes of disorder have underscored the scale of the task facing his government.

That, analysts say, includes defusing tensions stoked successfully by far-right groups — over immigration and fraying public services — particularly in areas of Britain that have long been in economic decline.

While opinion polls show the public clearly supports Mr. Starmer’s crackdown on violent protesters, “a lot of those people who see the rioters as thugs want immigration brought down,” said Steven Fielding, an emeritus professor of political history at the University of Nottingham.

Mr. Starmer, who has promised to cut migration numbers, “needs to follow up and do the things he says he’s going to do,” Professor Fielding added, while noting that it was “no accident” that violence erupted in several economically deprived regions.

Concern over immigration, which declined in Britain after Brexit, is on the rise again and, when jobs are scarce and health care and other services are overstretched, immigrants make an easy target for the far right. The campaign leading up to last month’s general election prompted a bitter political dispute over the last government’s plans to send to Rwanda people arriving in Britain on small boats.