Dozens of residents in Misurata pray after attending a protest organized to voice their discontent with what they perceive is Gen. Khalifa Hifter's alliance with Egypt's government. (Javier Manzano/For The Washington Post)

— Abdullah al-Thinni is Libya’s prime minister. From his office in the eastern city of Bayda, the 61-year-old former defense minister boasts an army, a suite of ministers and a host of challenges, including an Islamist insurgency and an economy in free fall.

But Khalifa al-Ghwell, who sits in the capital, also is Libya’s prime minister. A civil engineer who took over when his predecessor was ousted earlier this year, Ghwell may not enjoy the backing of world powers, but he does have the advantage of support from the country’s most powerful militias.

Libya finds itself today in the odd position of having two rival governments, each of them vying for international legitimacy and dwindling resources as the North African nation slips deeper into civil war. The country has two central banks, two national oil companies, and three anti-corruption commissions. British courts are now mulling which of the two Libyan investment chairmen has authority over the country’s $67 billion sovereign wealth fund.

But soon Libya may have no recognized government at all, if negotiators fail to meet a Sept. 20 deadline for clinching a United Nations-brokered political deal to create a unity government that, officials hope, can end a tangled conflict that has nearly bankrupted this oil-rich nation in the four years since the NATO-backed ouster of Moammar ­Gaddafi.

Even if an agreement can be reached, Libyans will face major obstacles to implementing that deal and, more fundamentally, resolving the issues that have made the post-revolution era so turbulent: replacing defiant militias with a cohesive national security force and ending the lawlessness that has made the country a haven for Islamic State extremists.

Despite failed attempts to foster stability, Western officials say the time is right to reconcile the internationally recognized legislature in the city of Tobruk, which supports Thinni, and its rival in Tripoli, the Islamist-
dominated General National Congress.

“Libyans are finally coming to terms with the fact that no one can win this conflict,” Bernardino León, the U.N. envoy to Libya, said in an interview. “This is the reality.”

Leon hopes that months of talks will result in a deal by Sunday, giving Libyans a month to form a new government before the Tobruk parliament’s mandate lapses on Oct. 20.

Mohamed Benruwin, an academic who is helping draft Libya’s new constitution, said Libyans allowed their inexperience at governing and their internal divisions to undermine the country’s natural advantages: oil, a small population and proximity to Europe.

“We’re in a position where either we’re going to win, or we lose it all,” he said in an interview in the city of Misurata.

If a deal is struck, Libyans will face a host of potential land mines in the first few weeks. They must create consensus on a new cabinet, including coveted military and financial positions, and sort through legislation passed by each side after the schism took place.

An agreement may also herald the return of Western military involvement in Libya. American and European officials have been discussing options for sending a mix of civilian and military personnel, possibly from Italy, Britain and the United States, to help Libyans begin the process of establishing a functional national force.

Western officials recognize that foreign troops could further polarize Libyans or fan the jihadist cause. But they also fear that history will repeat itself if the new government can’t establish a minimum of security during a crucial transition period.

“It’s fundamental that we build the capability as swiftly as possible,” a U.S. military official said.

In another illustration of the difficulties ahead, armed men stormed the Tripoli legislature Thursday as its members were deliberating the terms of a possible U.N.-backed deal, a suspected show of force from hard-liners who oppose an agreement.

Islamist groups, meanwhile, are growing bolder. On Friday, a team of Islamic State suicide bombers killed three militia guards when they attacked the Tripoli airport, possibly in an attempt to free prisoners held on the airport grounds.

In 2013, world leaders huddled at a Group of Eight summit in Northern Ireland were growing increasingly worried by insecurity in Libya. Joining them was Ali Zeidan, the then-Libyan prime minister who was struggling to rein in militias and tame a separatist movement in the east.

President Obama turned to Zeidan. “Tell me how many soldiers are loyal to you . . . and I’ll tell you if you have a government or not,” the president said, according to Libyan and Western officials who, like others, spoke on the condition of anonymity to discuss diplomatic deliberations.

Obama’s question remains equally important today. Since 2011, no Libyan leader has been able to tame a vast array of armed groups, including rival military forces, tribal factions and renegade oil police, on both sides of the country.

“Libya will continue to remain unstable, polarized and marred by violence for as long as the issue of widespread armed militias having de facto full control over discrete pockets of territory remains untackled,” said Jason Pack, a researcher of Libyan history at Cambridge University and president of Libya­Analysis.com.

Tripoli, the seaside city where Ghwell’s government is based, is today controlled by a number of militias loosely organized under the banner of “Libya Dawn.” In July 2014, a militia war erupted over control of the international airport here, prompting an evacuation of the U.S. Embassy and the newly elected parliament’s flight to Tobruk.

Many Libyans agree that the biggest challenge will be persuading well-armed militias to disband or allow their members to join regular armed forces.

“You’re talking about a very large number of young men who have no other source of income and have been doing nothing for the last four years but working for one of these hybrid security . . . groups,” said Hanan Salah, Libya researcher for Human Rights Watch. “How is the future government going to cut them out?”

Since 2011, when Gaddafi was ousted, militia commanders have grown rich, not just in influence but also in cash. Now they must surrender lucrative revenue sources such as airports or oil facilities.

“The real problem is that, as always in Libya, it comes down to . . . who thinks they’re losing out on something and who thinks they can gain something,” a senior U.S. official said. “It’s all about their ‘stuff’ again.”

Of all the assets that armed factions may be reluctant to give up, the most prized is oil.

In 2014, guards loyal to Ibrahim Jathran, the young leader of an oil protection force who launched a campaign to secure greater power for eastern Libya, commandeered an oil tanker and tried to sell its load of Libyan oil on the global market. The maneuver was blocked only when the United States dispatched a team of Navy SEALs to recover the ship.

Today, Jathran says his guards need additional weapons and support to fend off attacks by Islamist groups and rival forces and to lure back foreign oil firms spooked by insecurity.

“Libya needs oil revenues; it needs the presence of oil companies,” he said by phone.

But Jathran, with his loyal fighting force and his locally focused agenda, represents just the sort of unruly faction that could impair the credibility of a new government. Western officials are also worried about other potential spoilers, including Khalifa Hifter, the powerful general who heads armed forces in the east.

“You can’t have individual militia leaders or petroleum guards successfully shaking down the government,” another senior U.S. official said.

Western officials hope a deal, even if it is not universally accepted, will garner enough support on both sides to ensure a fragile transitional period. Once a unity government is established, partner nations can resume aid programs, investors can sign contracts, and oil firms can move ahead with exploration projects.

Abu Bakr al-Hureishi, a member of Misurata’s local council, said success will require a more fundamental shift for Libyans.

“Every country has political conflicts,” he said. “The difference in the new Libya was that it was: ‘If you’re not with me, you’re my enemy.’­ ”

Read more:

Libyan gains may offer Islamic State a base for new attacks

How the Obama White House runs foreign policy

Oil-rich Libya, torn by conflict, may be going broke