National flags from around the world flutter in the bright sunshine by a city gate made of shipping containers painted in the Libyan national colours. A uniformed militiaman examines my passport, then waves me through with a smile. Welcome to the Republic of Misrata.

Libya's third largest city, recipient of a six-month pummelling during last year's revolution against the regime of Muammar Gaddafi, has transformed itself into what is an independent state in all but name. Libya is due to hold national elections in 10 days, but these look like they may be delayed as any sense of post-Gaddafi national unity dissipated long ago.

Misrata is divorced from the new government, which it views as secretive, dictatorial and heavy-handed, and, as a city with a long tradition of trading, is going its own way. Shops and restaurants are being fixed up, business is brisk, and there is enough traffic on the pockmarked streets to create honking traffic jams.

Qasr Ahmed, Libya's biggest container port, is the jewel in the city's crown. The harbour that once spouted the geysers of incoming rockets is now jammed with shipping, and I get a tour in the only tug in Libya that can do something complicated with its engines that allows it to move sideways. The port authority has decided to run the place without reference to central government, which means that the port is open 24 hours a day, and also means that Misrata gets to keep the tugboat.

"In the old days there would be 12 forms and it would take 10 days to pay all the bribes," says Nasser Mokhtar, who printed photographs of the shaheed – martyrs – in the war in his print shop and is now back at his clothing import business.

Now, he explains, there are no bribes; customs officers fear the wrath of the port authority if they try it on.

Misrata held its own city elections in February, the first anywhere in Libya for four decades, and the new council is now busy organising the police, army, education and health services.

And that is the problem. The price of this success has been a divorce from a central government. "We don't want to be independent, we want Libya to be like us," says Farouk Ben Amin, a former rebel fighter now working in the family import business, who has shaved off his rebel beard and looks 10 years younger.

It's not just Misrata. From all points of the compass, revolt, even revolution, is in the air as Libya's former rebel towns go their own way.

More than 100 miles from Misrata is Zintan, a humble metropolis nestling in the cool foothills of the jagged Jabal Nafusa mountains.

In the war, Zintan's rebels were one half of the pincer movement – Misrata was the other – that captured Tripoli. Its units poured out of the mountains and into the west of the city, while Misrata's units punched in from the east. Now the mood in both cities is suspicious about the ruling National Transitional Council; not least about what it is doing with the £1bn a month now being earned as oil exports pick up.

Zintan's uneasiness has seen it change its mind about handing over Libya's top war crimes suspect, Saif al-Gaddafi, son of the late dictator, who continues to languish in a fortified villa on the edge of town. "It is safer to hold his trial here; the government is very weak, they can't control their country," said Attaher Eturki, the ever-smiling city council leader, his crisp English a product of a degree in engineering in Leicester a couple of years ago. "We have good security here."

To the south, meanwhile, battles between the Tibu, a people who inhabit a large stretch of the Sahara, and Arab tribes have left 200 dead and the towns divided into war zones.

The most serious challenge to central authority is Benghazi, where the revolution began in February last year. Like Misrata, Benghazi held its own elections earlier this year, and like Misrata the city council is busy assuming powers for itself at the expense of central government.

Some in the city want to go further. Benghazi is the capital of Cyrenaica, which with the regions of Tripolitania and Fezzan make up Libya, and many citizens are unhappy that the province gets only 60 of the 200 seats in the national elections. A self-proclaimed Council of Barqa – the Arab name for Cyrenaica – is urging a boycott of the national elections unless it gets a bigger slice of seats.

Benghazi is a good place to feel the continuing heartbeat of the revolution: teams of teenage volunteers collect the rubbish, fix up the streets and paint white lines on the highways. Those white lines zigzag alarmingly, but the citizens appreciate the effort; a vivid contrast to the potholed roads of Tripoli.

It's not independence but democracy that the people want, says Hanna El Gallal, a human rights activist. "We got rid of Gaddafi, but not the regime," she tells me. She points to the secrecy of the NTC, which, despite promising democracy, keeps its meetings secret and refuses even to disclose its full membership. "We didn't do a revolution and our people did not die to bring a new dictatorship."

When the NTC does issue decrees, Libyans are aghast; last month it issued law number 37, making it a criminal offence to criticise the "17 February revolution".

Human Rights Watch pointed out in a scathing report that the law is, word for word, almost the same as Gaddafi's rule banning criticism.

In London last month, Libyan prime minister Abdurrahim el-Keib insisted that the law would soon be cancelled, but failed to explain why the government had introduced it in the first place.

"The NTC don't mean to act this way," said an official with a western embassy in Tripoli. "But they don't know any other way."

The NTC took power in the chaos of last year's revolution in Benghazi, led by Mustafa Abdul Jalil, a career judge, and the only politician in Libya to enjoy widespread support.

That support comes from the mark he made when he resigned as Gaddafi's justice minister in 2010, making the announcement on live television, an unheard of event in the former dictatorship. But Jalil's star is starting to wane, with Libyans divided about whether he is responsible for the NTC's heavyhandedness, or too weak to stop it.

And then there is history: Libya is a young country, named as such by its Italian occupiers only in 1934. Before that, Tripolitania, Cyrenaica and Fezzan were separate provinces.

There is wild talk of a second uprising on the streets of former rebel towns, but the weapon of choice is not the gun but the ballot box. City elections have been rushed through while the central authorities dither with the national election, and the municipalities adopt their own powers. El Gallal explained that, if the elections nationally go well, all will be fine. If not, Benghazi will fall back on its own city administration. "If it (the national election) goes wrong, we don't need the national congress," she said.

Back in Tripoli, the signs are that the national elections are going very wrong indeed. The NTC insists that the vote will take place, as promised, on 19 June. But staff at the election commission tell me that they have yet to agree the list of candidates. Giving Libya's enthusiastic political parties only a few days to campaign will cause uproar. But so will a delay, stoking fears by the rebels that the NTC plans to hang on to power.

None of which is good for business: Foreign companies are staying away from Libya, scared off by all the uncertainties. Meanwhile, unemployment remains high, pensions and wages are often unpaid, and rubbish mounts up in vast piles outside the gates of Gaddafi's ruined palace of Bab Azizia.

And then there are the militias. Nowhere has the government's failure to convince Libyans of its good intentions been more visible than with the security forces. The decision to staff the grandly named National Army with Gaddafi-era generals has, unsurprisingly, seen no recruitment from the former rebels.

Instead, security is being entrusted to a national gendarmerie, the 60,000-strong Special Security Committee (SSC). The pay is good and rebels and former Gaddafi units have joined en masse, but the force is distrusted by the armies of Misrata and Zintan.

SSC units last month kidnapped and tortured a prominent health ministry official and, despite pleas from the minister, the government has not called them to account.

Nor has the SSC dared to move against Islamist units in eastern Libya who have vandalised Commonwealth war graves, launched bomb attacks on a UN convoy and a Red Cross office, and last week bombed the US consulate in Benghazi.

And it was Tripoli militia units, not the SSC, which took back control of the international airport last week after it was stormed by a militia group from Tarhuna who were upset about the abduction of their commander.

At Tripoli's luxurious Rixos Hotel, I meet NTC member Musa al-Koni amid rolling lawns and burbling fountains. The fondness of NTC executives for rooms here, at taxpayers' expense, is a staple of the capital's booming media.

"We made so many mistakes, so many," Koni says. He was once Libya's ambassador to Mali, until the revolution broke out, and in March last year he decided to jump ship after being ordered to recruit mercenaries to come to Gaddafi's aid.

"Old people are the problem," he says. "Old people stole the revolution in Tunisia, they stole it in Egypt, and they are stealing it here," he says.

A few days after we met, he announced that he had quit. Two new NTC members had taken his place, he said, though their identities were being kept secret.

Around the back of the Rixos is a unit of former rebels, the national guard, which is part of the Libyan National Shield, a loose alliance of Libya's militias that bypasses the defence ministry. I hitch a lift with them from positions around Bani Walid, a still restive former Gaddafi town. Arriving at the hotel, an argument starts with men across the road in the sprawling blue-collar Abu Salim neighbourhood.

Like Bani Walid, Abu Salim spent the war backing Gaddafi, and now they shout at the guard, accusing them of being out-of-town interlopers. As the argument worsens, several guardsmen come forward and shout back that they are from Tripoli, and the revolution is safe. The shouting gets worse. Traffic stops. The guardsmen cock their weapons.

A tall, bearded, middle-aged guardsman, who worked before the revolution for an oil company in Paris and London, leads me away through the hotel grounds. "I want to leave Libya," he says.

"Why?"

"These people," he says, gesturing to Abu Salim. "They are poor. Gaddafi had all this oil and he gave them nothing. And still they love him."

Diplomats in Libya worry about where all this is going: not least because while the NTC has the power – and the oil – it is the former rebel militias who have the guns.

In the first public in-depth study of Libya's militias, Oxford University researcher Brian McQuinn says that militias in Misrata and Zintan are well organised and disciplined.

Misrata alone accounts for just under half the total militia units in the country, with slightly more than half of Libya's heavy weapons, including 820 tanks. Both are enthusiastic members of the National Shield, which now has four regional divisions. "Unions of revolutionary brigades from across the country have, in co-ordination, created a national army-in-waiting," he writes.

Exactly what they are waiting for remains to be seen.

Much hangs on the elections. If the NTC botches them, or tries to use its proxies to hang on to power, Libya will be in trouble. At best, the former rebel cities will go their own way, creating administrative gridlock for the country and an economic nightmare. At worst, as a rebel militiaman told me last year on the frontline at Ajdabiya, south of Benghazi: "If we don't like the new government, well, now we know how to do revolution."