A dozen police officers, one with an assault rifle across his knees, guard the presidential mausoleum in this seaside resort, easily outnumbering the foreign visitors on a recent morning.
It was here, the hometown of Tunisia’s first president, Habib Bourguiba, that a teenager once tried to blow himself up amid a group of tourists. The attack failed — his bomb did not explode and a tour guide tripped up the would-be bomber as he tried to escape — but it was only by luck that disaster was averted.
That was two years ago. More recently, despite ample warnings, Tunisia has had less luck in the face of a growing terrorist threat. Gunmen trained in Libya and linked to the Islamic State and Al Qaeda killed 22 people at a national museum in the heart of Tunis, the capital, in March and 38 tourists along a beach in the nearby town of Sousse in June.
Those attacks have provoked a widening security crackdown, and left Tunisians wondering if their country can withstand the onslaught of terrorism without giving up the tentative freedoms they — alone in the region — earned with their revolution that set off the Arab Spring more than four years ago.
The situation is increasingly precarious, nearly all agree. Last week, President Beji Caid Essebsi extended a state of emergency for two months. He had warned that another attack could cause the state to collapse. He later dialed back that comment, but only slightly.
“I want to reassure Tunisians that the state is not on the point of collapsing, but they must know that the hour is grave and we have to unite against terrorism,” he said in an interview on the Tunisian channel Attasia TV.
Aggravating the situation is the deepening presence of extremist Islamist groups allied to Al Qaeda and the Islamic State in neighboring Libya, government officials, Western diplomats and security analysts say.
Militant groups here, which have been waging a low-level insurgency for several years, have now linked up with them, injecting a new level of violence in Tunisia.
The change in terrorist tactics has left nearly everyone on edge, with the government raising alert levels all around, said Yassine Brahim, minister for development and international cooperation.
“That is a new level, attacking tourists,” he said in an interview. “They were attacking security forces last year, now they put another target. We need to anticipate other levels.”
Tunisia’s security forces have conducted hundreds of raids, and frequently announce arrests and successful operations against insurgents.
Yet as more young people join the militant groups and receive training in Libya, security officials acknowledge that they cannot keep up.
“What the security forces tell us is that, ‘We start, we fight and we win battles, but we find that people go to Libya and come back,’ ” Mr. Brahim said. “So we need more means to follow the speed at which this is moving.”
The violence, which started sporadically, has steadily gained pace against security forces, which have been largely unprepared. It began with a violent demonstration against the American Embassy in 2012, two political assassinations of leftists in 2013, and attacks on police and army units in the western regions along the Algerian border.
As shocking as some of the attacks were — eight soldiers were beheaded in one ambush, 14 killed in another — the problem still seemed remote and, officials insisted, the groups of insurgents were small, 30 to 40 people.
But by last year, the antiterrorism police were discovering cells of armed militants in suburbs of the capital. Now, after the two brazen attacks on foreign tourists this year — which British and Tunisian security officials say were carried out by gunmen who trained together in Libya — the Tunisian authorities are taking the threats more seriously than ever before.
Members of the Tunisian Army and National Guard, long kept poorly equipped and trained for political reasons by the former dictator President Zine el-Abidine Ben Ali, have received special forces training in the United States. The antiterrorism police have dealt swiftly with gunmen in several sieges in the capital, including the museum attack in March.
But the lower ranks remain poorly trained and motivated. The 14 soldiers killed last year were sitting down to break their fast during Ramadan and had failed to post any lookouts. The police units in the beach resort of Sousse took 20 minutes to respond to the attack in June, because they were away from their post at the time.
Of greater concern is the failure of the intelligence services to gain any forewarning of either attack. “They have not managed to infiltrate these groups,” said Ridha Tlili, a political analyst and director of the Ahmed Tlili Foundation for the Culture of Democracy. “They are always surprised.”
The interior minister, Najem Gharsalli, recently announced two successful operations against Okba Ibn Nafaa, a group linked to Al Qaeda in North Africa, but acknowledged that the organization had expanded its territory.
Some of the men killed by security forces had been in the process of carving out a base in southern Tunisia to facilitate communication and supply lines from Libya across Tunisia, he said.
But any steps toward greater security may come at a cost, and could themselves exacerbate the situation. Critics of the government crackdown say it has become too sweeping and threatens to restore harsh methods by the security forces that Tunisians hoped to leave behind after throwing off nearly 60 years of dictatorships.
Defense lawyers and families of some of those arrested complain that innocent people are caught in security sweeps. The Tunisian Organization Against Torture is investigating nearly 70 cases of torture from 2015.
Hundreds of young men are fleeing the country, many of them to Libya where they can find work, but most are joining extremist militant groups, government officials say.
A group of eight human rights organizations, including Human Rights Watch and the Carter Center, have criticized Tunisia’s newly amended counterterrorism law, which grants the police broad monitoring and surveillance powers and the ability to hold suspects incommunicado for up to 15 days.
A recent report by a United Nations working group warned that Tunisians were abandoning the country to join militant groups like the Islamic State, also known as ISIS or ISIL, in a way that was “unprecedented in term of the sheer scale.”
Some 5,500 Tunisians have joined extremist groups, with about 4,000 in Syria and up to 1,500 in Libya, the report said. Smaller numbers are fighting in Iraq, Mali and Yemen, it said.
Women are among those who have joined extremist groups and even entire families have traveled to conflict zones. But most include young people from all socioeconomic backgrounds. They are being recruited through local chapters of extremist groups, in mosques and in prisons. Some of the activity is facilitated by a large inflow of money from abroad, the report said.
“Vulnerability on many dimensions — physical proximity, socioeconomic, psychological and financial — has apparently thereby created fertile ground for manipulation and recruitment,” it said.
Former Prime Minister Hamadi Jebali is among those voicing concern. The strained political and economic conditions of the country, the lack of development in the poorest regions — especially the south, which borders Libya — and the government’s limiting of democracy, will open the way for extremist groups such as the Islamic State, he said.
“Unfortunately when they come here, I am afraid they will find a reservoir,” he said in an interview at his home in Sousse.
“Beware, because the south of Tunisia is totally ready, favorable to that,” he went on. “They will perhaps open the doors to Libya, and then watch out, that is a time bomb.”