Western countries see the unity government as their best bet for stabilizing Libya and mounting a concerted military drive against Islamic State, which has expanded far beyond its base in Colonel Qaddafi’s hometown, Surt. In recent days they have stepped up efforts to force rival groups to accept the new administration.

On Friday the European Union imposed sanctions on three leaders from the country’s two other parliaments, one in Tripoli and the other in the eastern city of Tobruk. Mr. Ghwail, the prime minister of the unrecognized Tripoli government, was among those named.

The United Nations Security Council, meanwhile, focused on the oil sector in a resolution on Thursday that called the unity government Libya’s sole legitimate authority and condemned efforts by “parallel institutions” to export the country’s oil.

That resolution appeared to have an effect on Friday, when the militia that guards many of the country’s oil terminals pledged loyalty to Mr. Serraj’s government.

Analysts and diplomats say the real test is likely to come in the days ahead, when Mr. Serraj’s ministers are expected to try and establish control of key ministries across Tripoli. Mr. Serraj has already started talks with the Central Bank, which controls foreign reserves estimated at up to $85 billion, and the national oil company, which is the source of the country’s dwindling wealth.

Officials at several ministries, contacted by phone, said there has been deep uncertainty in recent days, with little sense of who is in charge.

Libya’s complex civil conflict, which involves an array of militias organized by town, tribe or ideology, burns with less ferocity than others in the Middle East, such as Yemen or Syria. The United Nations documented 32 civilian casualties across the country during the month of March, mostly in the east. Yet the power vacuum greatly worries the West because it has emboldened the Islamic State in its expansion in Libya and helped increase the flow of migrant boats to Europe.