TRIPOLI/BENGHAZI, Libya (Reuters) - Libyan rebels occupying four eastern oil ports agreed with the government on Sunday to gradually end their eight-month petroleum blockade, which has cost the North African state billions in lost revenues.

Zueitina and Hariga ports, held by federalist rebels demanding more autonomy from Tripoli, will open immediately while the larger ports, Ras Lanuf and Es Sider, will be freed in two to four weeks after more talks, the government said.

Ending the oil port standoff will be a major advance for Libya's fragile government, which has struggled to impose its authority over an unruly nation still in flux nearly three years after the fall of Muammar Gaddafi.

"The ports Zueitina and Hariga will be handed over to the state with the signature of this agreement. The protesters are banned from returning or obstructing work at the ports," Justice Minister Salah al-Marghani said, reading out the agreement.

He said the two larger ports will be reopened in no more than four weeks, after more discussions with the rebels.

Top rebel leader Ibrahim Jathran confirmed the blockage of Zueitina and Hariga ports had ended. "We did this out of goodwill to build trust, have a dialogue and solve all problems between Libyans by peaceful means," he said in a short speech broadcast by a rebel television station from Zueitina terminal.

"We will undertake more measures to strengthen these intentions provided the government fulfills its part," he said, in an apparent reference to the reopening of the remaining ports. "We serve Libya's interests."

Neither side commented on what rebel demands still needed to be negotiated with the government, leaving room for possible delays in reopening the larger ports.

Zueitina and Hariga ports account for around 200,000 barrels per day of export capacity, while the larger ports previously shipped around 500,000 bpd of Libya's crude.

Storage tanks are full at the ports, and loading the crude will be straightforward. But getting the tanks resupplied from oilfields will take time.

The reopening of the eastern oil ports could bump up Libya's output from around 150,000 bpd, but nowhere near the 1.4 million bpd it produced before last summer.

Disputes over Libya's oil resources underscore how weak the government is to confront brigades of former rebels and militias who refused to disarm after Gaddafi's fall and often use their military muscle to strong-arm the state.

With its national armed forces still in training, Tripoli's government often finds itself at the mercy of rival militias who are loosely allied with competing political factions in the country's parliament.

CORRUPTION PROBE

Rebel leader Ibrahim Jathran, who seized three of the ports with thousands of his troops, is a former anti-Gaddafi fighter, who later become head of a state-run oil facilities guard before he turned against Tripoli.

Hariga port was closed by another group of federalist protesters who sympathized with Jathran's cause.

Jathran's movement set up its own self-declared federal government in the east, where many feel they have long been neglected by Tripoli. They made three key demands on the government, including a system to share oil revenues, a probe of corruption and a committee to oversee oil exports.

The minister said the agreement calls for all charges to be dropped against Jathran's oil protection force, and for them to receive their oil guard salary payments according to the law.

A committee would be formed to investigate financial and administration corruption since Libya's liberation from Gaddafi, one of the demands made by Jathran's movement.

The body will be made up of Libyans from different regions, a fact which the rebels' self-declared prime minister, Abd-Rabbo al-Barassi, pointed out in a speech. His group had demanded that oil sales be monitored by representatives from all regions.

But the one-page agreement did not mention the rebels' other key demand, a greater share of oil revenue for their eastern region, known as Cyrenaica and one of Libya's three regions before Gaddafi. They also have been campaigning for regional autonomy.

"We won't give up Cyrenaica's rights whatever happens but when the government started a dialogue we talked and thank God ... we reached an agreement for the good," he said.

The shutdown has cost the state more than $7 billion in lost oil revenues and forced the central bank to start burning its reserves to keep the state running. Parliament has failed to approve a budget for 2014, as there is almost no state income at the moment due to a wave of oil protests across the OPEC nation.

Support had been waning for Jathran's protest in the east, where divisions had begun to emerge over whether to continue the blockade.

Talks to end the standoff advanced after the federalist rebels last month managed to load crude onto a tanker at Es Sider and forced it out to sea in an attempt to export the cargo.

U.S. commandos later boarded the formerly North Korean-flagged vessel in international waters and returned it to Libya, in a major blow to the federalists' plan to bypass Tripoli and sell oil independently on the global market.

The oil deal does not necessarily end protests that have shut western oil production facilities such as the 340,000-bpd El Sharara oilfield for weeks. Protesters in the west have few ties with the east and are splintered into small groups lacking a joint leadership, which makes it hard to negotiate with them.