Vehicles burn after armed Libyan demonstrators stormed the headquarters of the Raf Allah al-Sahati Brigade, a local militia near Benghazi. Photo: AFP/Getty Images

BENGHAZI, Libya — Libya’s president ordered all of the country’s militias to come under government authority or disband, a move that appeared aimed at harnessing popular anger against the powerful armed groups following the attack last week that killed the US ambassador.

The assault on the US mission in Benghazi, which left Ambassador Chris Stevens and three other Americans dead, has sparked an angry backlash among many Libyans against the myriad armed factions that continue to run rampant across the nation nearly a year after the end of the country’s civil war. On Friday, residents of Benghazi — the cradle of the Libyan revolution last year that toppled dictator Moammar Khadafy — staged a mass demonstration against the militias before storming the compounds of several armed groups in the city in an unprecedented protest to demand the militias dissolve.

Late Saturday, President Mohammed el-Megaref told reporters that the militias, which the weak central government has relied upon since Khadafy’s ouster in October to provide security in neighborhoods and at state facilities across the country, must fall under the umbrella of the national authorities or disband.

El-Megaref said a joint operations room in Benghazi will coordinate between the various authorized armed brigades and the army. Militias operating outside the “legitimacy of the state” will be dissolved, and the military and police will take control over those armed groups’ barracks, he said.

In a statement published by the official LANA news agency, the military asked all armed groups using the army’s camps, outposts and barracks in the capital, Tripoli, and other cities to hand them over. It warned that it will resort to force if the groups refuse.

Since Khadafy’s capture and killing, the government has brought some militias nominally under the authority of the military or Interior Ministry, but even those retain separate commanders and often are only superficially subordinate to the state. Even following el-Megaref’s announcement, it was unclear whether the government had the will — and the firepower — to force the most powerful militias bend to its authority.

Over the past 11 months, a series of interim leaders in Libya has struggled to bring order to a country that was eviscerated during the eccentric dictator’s 42-year rule, with security forces and the military intentionally kept weak and government institutions hollowed of authority.

The militias, which arose as people took up arms to fight Khadafy’s regime and are organized largely along local lines, bristle with heavy weapons, pay little attention to national authorities and are accused by some of acting like gangs, carrying out killings. Islamist militias often push their demands for enforcement of strict Shariah law.

On Friday, some 30,000 people took to the streets of Benghazi for a mass protest against the militias. The crowd marched to the compounds of several armed groups in the city, and thousands overran the headquarters of Ansar al-Shariah, an Islamic extremist group suspected in the Sept. 11 attack on the US Consulate.

The protesters drove out the Ansar gunmen and set fire to cars in the compound — once a major base for Khadafy’s feared security forces — while others stormed into the Jalaa Hospital, driving out Ansar fighters there.

The crowd then moved onto the base of a second Islamist militia, the Rafallah Sahati Brigade. Brigade fighters opened fire to keep the protesters at bay.

The state news agency said four protesters were killed and 70 injured in the violence.

For all the anger with the militias and the government’s move to bring them to heel, the authorities also have desperately needed the armed brigades over the past year to help provide security in a country still struggling to find its feet after an eight-month civil war.

The Rafallah Sahati Brigade, for example, kept security in Benghazi during national elections this year. Its compound, once a Gadhafi residence, contains a prison and protects a large collection of seized weapons. Ansar al-Shariah guarded Benghazi’s main Jalaa Hospital, putting a stop to frequent attacks against it by gunmen.

There are two other major militias in Benghazi that authorities rely on. One is called Libya Shield, led by Wassam Bin Hamaad, an Islamist who has resolved tribal disputes. Another is the Feb. 17 Brigade, led by Fawzi Abu Kataf, who is seen as connected to the Muslim Brotherhood. The militia is believed to be the closest to the state authorities and has helped secure borders.