BERLIN — The German government on Wednesday announced that it was taking over Uniper, previously the country’s largest importer of Russian gas, to ensure the supply of energy to homes and businesses.

The German state will spend 8 billion euros ($7.9 billion) to acquire shares in Uniper it does not already own, giving it a 99 percent stake. Fortum, a Finnish energy company, will sell its majority stake to the German government for €1.70 per share, a fraction of what Uniper’s stock was worth before Russia invaded Ukraine in February, when it traded at around €40 per share.

In July, the German government took a 30 percent stake in Uniper and agreed to a €15 billion rescue package to keep it from going under. The company is responsible for supplying about 40 percent of all gas used in Germany, including to hundreds of municipalities that provide heating to homes across the country. The Finnish government has a stake in Fortum, and Helsinki had balked at providing further assistance to the German company.

“This step became necessary because the situation has clearly changed” in recent weeks, Robert Habeck, Germany’s economy minister, told reporters. Conditions have worsened, he said, particularly since Russia halted all gas deliveries through the Nord Stream pipeline in early September.

Uniper lost €12 billion in the first half of the year, a result of having to pay inflated prices to make up for the Russian shortfall.

“Today’s agreement provides clarity on the ownership structure, allows us to continue our business and to fulfill our role as a system-critical energy supplier,” Klaus-Dieter Maubach, Uniper’s chief executive, said in a statement.

“The role of gas in Europe has fundamentally changed since Russia attacked Ukraine, and so has the outlook for a gas-heavy portfolio,” Markus Rauramo, Fortum’s chief executive, said in a statement explaining its decision to divest. Fortum’s share price rose 14 percent in early trading in Helsinki.

Berlin’s decision to nationalize Uniper is the latest example of governments across Europe unraveling decades of promoting a free-market approach to the electricity and natural gas industries, as lawmakers move to ensure supply in the face of record-high energy prices.

Germany has been buying more natural gas from Norway, the Netherlands and other countries that cool and ship the fuel as liquefied natural gas, or L.N.G. But that has driven prices up to previously unseen levels, with the benchmark European gas contract setting a record high last month. It currently trades at just over €200 per megawatt-hour, down from recent highs but more than double the level in early June.