(Reuters) - A U.S. judge on Tuesday approved a settlement between a federal regulator, Tesla Inc and its chief executive officer, Elon Musk, over his tweets promising to take the company private, signalling an end to a tumultuous period for investors.

Tesla shares rose as much as 5.5 percent to $273.88 before easing to $271.63 in early-afternoon trade on Nasdaq. Despite the gains, the stock is still down more than 20 percent since Aug. 6, the day before Musk said on Twitter he would take the company private and claimed he had secured funding to do so.

Judge Alison Nathan of the U.S. District Court for the Southern District of New York approved a motion filed by the U.S. Securities and Exchange Commission outlining the agreement with Tesla and Musk.

A Tesla spokesperson confirmed the settlement, but said the automaker would make no further comment on the matter.

Under an agreement with the SEC, Musk has agreed to pay a $20 million fine and step aside as Tesla’s chairman for three years to settle charges that could have forced his exit from Tesla.

The company will also pay a $20 million fine, despite not being charged with fraud.

The government lawsuit threatened Tesla and Musk with a long fight that could have undermined its operations and ability to raise capital.

FILE PHOTO: Tesla Motors CEO Elon Musk speaks during the National Governors Association Summer Meeting in Providence, Rhode Island, U.S., July 15, 2017. REUTERS/Brian Snyder/File Photo

Under the settlement announced on Sept.29, Tesla must appoint an independent chairman, two independent directors and a board committee to control Musk’s communications.

Musk must comply with procedures set by the committee, including preapproval of “any such written communications that contain, or reasonably could contain, information material” to Tesla or its shareholders.

Twitter has frequently been Musk’s go-to venue for freewheeling communications and confrontation with Tesla’s critics.

The stock dropped last month after the SEC accused Musk, 47, of fraud over his “false and misleading” tweets on Aug. 7.

On Oct. 4, just hours after Nathan ordered him and the SEC to explain why their settlement was fair and reasonable, Musk appeared to mock the SEC on Twitter.

“Just want to [sic] that the Shortseller Enrichment Commission is doing incredible work,” Musk, a frequent critic of the investors who have bet against the company, wrote. “And the name change is so on point!”

The day after the tweet, Tesla’s shares fell 7 percent when billionaire investor David Einhorn’s Greenlight Capital hedge fund criticized the electric carmaker, saying Musk had been deceptive and the carmaker’s woes resembled those of Lehman Brothers before its collapse.

Musk has gained legions of fans for his bold approach to business and technology, using his 23 million Twitter followers to promote Tesla, his rocket company SpaceX, and tunnel venture Boring Co.

But the Aug. 7 claim that he had the funding to take Tesla private, and a subsequent U-turn, stunned Wall Street and came as Musk was filmed briefly smoking marijuana during a live Web show and when he called a British diver in the Thai cave rescue a “pedo.”

The Financial Times reported last week that outgoing Twenty-First Century Fox Inc CEO James Murdoch, a son of Fox mogul Rupert Murdoch, was the lead candidate to replace Musk as chairman. Musk called the report “incorrect.” Tesla has until Nov. 13 to appoint an independent chairman.

Thanks to Musk’s vision and showmanship, Tesla’s valuation has at times eclipsed that of traditional, established U.S. automakers that make millions of vehicles and billions of dollars in profits annually, and the company has garnered legions of fans despite repeated production issues.