TUNIS, Tunisia — Libya’s prime minister, Ali Zeidan, was hauled from his bed at 2:30 a.m. Thursday by a group of militiamen who stormed into the luxury hotel where he lives in downtown Tripoli, a kidnapping that would be extraordinary by almost any standard.

But this was Libya, where militias have unrivaled authority. A few days earlier, a group of armed men barged into Zeidan’s office to demand back pay. They refused to leave for hours and ransacked an office when they did leave.

Other militias have hampered production of oil, shut down the water running to the capital, forced power cuts, and participated in gun-running and drug trafficking — all with impunity.

Two years after the fall of Moammar Khadafy, the long-reigning dictator, an air of revolution still hangs over the streets of the nation’s capital, Tripoli, as a weak state struggles to build any sense of national unity, let alone control the unruly, heavily armed militias. It initially seemed that the gunmen may have grabbed the prime minister because they were angry about Washington’s claim that his government approved a commando raid to capture a Libyan citizen suspected of links to Al Qaeda and terrorist attacks.

But these same militiamen had been pressing the government for the prime minister’s resignation, and many in Tripoli believe the commando raid was simply a pretense. When he was freed unharmed a short time later, Zeidan demonstrated just how vulnerable the central government is, thanking the “true revolutionaries” who worked for his release, and appealing to armed groups to help build the state.

“I hope that they would be a part of the state, and have an effective role through its civil and military institutions,” he said in a televised speech. “I hope that we deal with this situation wisely, using our brains, away from worries and magnifying the situation, and we try to mend what we can.”

All across Tripoli, militia members fill the gap left by a weak central government. The men guarding government buildings and the main intersections still wear a variety of US military fatigues and man antiaircraft guns mounted on the back of pickup trucks. Graffiti from the moment of liberation is still scrawled on walls and buildings. “Game over Khadafy,” one reads in English.

Weapons from Khadafy’s enormous arsenals are now in private hands. Every household has a gun. There are about 200,000 armed militiamen in the country, all on the government payroll, yet many are loyal only to their own commanders, not the central government.

Formed during and after the uprising against Khadafy, Libya’s militias have evolved into a patchwork of often-competing regional and political allegiances. They reflect two emerging political blocs: one drawn from prominent families from the Western region around Tripoli, based in the Nafusa mountain town of Zintan, and the other in the bustling mid-coast city of Misurata, known for its strong revolutionary record.

Militias from the two regions clashed in Tripoli during Ramadan in what residents said was a grab for government property. Zintan militias have refused to turn over to the central authorities a son of Khadafy, Seif al-Islam, and temporarily shut down the oil pipeline that flows from Libya’s southern oil fields to the port of Zuwara to press demands with the central government.

In eastern Libya, around Benghazi, the birthplace of the uprising, some of the largest militias are loosely or explicitly Islamist, and they often clash with former Khadafy army units that defected in the revolt. Other militias push for a greater degree of autonomy from the capital.

Libya’s tribes have also emerged as powerful — and disruptive — players. When a militia in Tripoli kidnapped the daughter of Khadafy’s former intelligence chief after she visited her father in prison, his powerful tribe in the south retaliated by cutting the water to the capital for a week, an extraordinary burden for residents in the desert heat of late summer.

The government has tried to bind these disparate and competing forces into the ranks of the national army and police, placing them on the government payroll and ordering regional militias to leave the capital. Those moves have been only half successful. Many Libyans criticize the government for paying the militiamen in the first place, because it has caused thousands more men to join the militias.

“They turned what should be a national contribution into a job,” said Mustafa Almanen, a member of Libya’s first governing body, the National Transitional Council. The original forces that fought against Khadafy numbered about 20,000. The real revolutionaries complain that many of the militiamen are former Khadafy loyalists seeking protection.

BostonGlobe.com complimentary digital access has been provided to you, without a subscription, for free starting today and ending in 14 days. After the free trial period, your free BostonGlobe.com digital access will stop immediately unless you sign up for BostonGlobe.com digital subscription. Current print and digital subscribers are not eligible for the free trial.

Thanks & Welcome to Globe.com

You now have unlimited access for the next two weeks.

BostonGlobe.com complimentary digital access has been provided to you, without a subscription, for free starting today and ending in 14 days. After the free trial period, your free BostonGlobe.com digital access will stop immediately unless you sign up for BostonGlobe.com digital subscription. Current print and digital subscribers are not eligible for the free trial.