One of America’s most famous leakers is revealing how close we are to nuclear Armageddon.

“All out-nuclear war — an irreversible, unprecedented and almost unimaginable calamity for civilization and most life on earth — has been, like the disasters of Chernobyl, Katrina, the Gulf oil spill, Fukushima Daiichi, and before these, World War I, a catastrophe waiting to happen, on a scale infinitely greater than any of these,” writes Daniel Ellsberg in his new book, “The Doomsday Machine: Confessions of a Nuclear War Planner” (Bloomsbury USA), out now.

Ellsberg helped write America’s nuclear war plans during the 1960s Cold War years, when the US and the Soviet Union competed to build the greatest and most menacing pile of weapons.

He is proud of helping rework the plans during the Kennedy administration so the president has options besides all-out nuclear war. But over time he learned the system’s greatest — and most obvious — flaw: No matter how efficiently it is run, there is no room for error.

Nuclear bombs “are susceptible to being triggered on a false alarm, a terrorist action, unauthorized launch or a desperate decision to escalate,” Ellsberg writes. “They would kill billions of humans, perhaps ending complex life on earth. This is true even though the Cold War that rationalized their existence and hair-trigger status — and their supposed necessity to national security — ended 30 years ago.”

This is a story Ellsberg first wanted to tell around 1971, when he gained fame as the WikiLeaker of his day.

That year he gave secret government documents about the nation’s Vietnam War history to The New York Times, The Washington Post and other media. That leak is key to the plot of “The Post,” the upcoming movie starring Meryl Streep and Tom Hanks. The documents, known as the “Pentagon Papers,” showed that the government lied for years about the war’s progress and secretly expanded its bombing campaign into neighboring Cambodia and Laos.

At the same time Ellsberg copied the Pentagon Papers from his office at RAND Corp., a consulting firm that advised the Defense Department, he Xeroxed thousands more pages about America’s nuclear-war plans. Ellsberg decided it was a bad idea to leak both sets of documents at once, so he disclosed the more timely Vietnam papers first. “Vietnam is where the bombs are falling right now,” he reasoned.

Meanwhile, Ellsberg gave the nuclear papers to his brother Harry for safekeeping. Harry stored them in the basement of his house in Hastings-on-Hudson before moving them to his backyard compost heap.

While Harry hid the documents, Daniel was being investigated for the Pentagon Papers leak. On June 28, 1971, Ellsberg surrendered to federal authorities in Boston. Around then, Harry buried the papers in what he assumed was an even more secure location — an escarpment at the Hastings-on-Hudson town dump.

But the escarpment proved to be a bad hiding place. After Tropical Storm Doria washed it out in August 1971, the papers were lost forever.

Leaking American nuclear secrets would have landed Ellsberg in prison for decades. He is relieved that no one at the time discovered his act of espionage, writing that his wife, Patricia, saw loss of the papers as “an act of grace . . . It allowed me to sleep next to her, in loving embrace, for the last 40 years instead of in prison.”

Meanwhile, because Richard Nixon’s White House resorted to illegal means to punish Ellsberg for the Pentagon Papers — including burglarizing his psychiatrist’s office in search of blackmail material — a judge dismissed the charges against him in 1973.

Now 86, Ellsberg, who was a Marine lieutenant before he went to work for RAND and the Pentagon, believes the time is right to remind the world that for decades it has lived on the edge of a catastrophe that could wipe out all of human life.

Most Americans think only President Trump can order the use of nuclear weapons. That idea is underscored by the fact that he is always accompanied by a military officer carrying a briefcase — “the football” — containing launch codes.

But Trump isn’t the only military commander authorized to launch nuclear weapons. Ellsberg says the football is “theater — essentially a hoax.”

Lower-level military commanders can act on their own if the US comes under attack and the president can’t respond in time. “There has to be delegation of authority and capability to launch retaliatory strikes, not only to officials outside the Oval Office but outside Washington too,” Ellsberg writes.

The same is true of enemy nuclear powers. Military commanders under attack don’t have to await orders from the top to strike back — even with nuclear weapons.

It almost happened during the 1962 Cuban missile crisis, when a frustrated and scared Soviet sub commander considered launching a nuclear-tipped torpedo at US Navy ships that were harassing his boat with noisy but harmless practice depth charges.

Even though he had no direct order from Moscow, the submariner ordered his crew to prepare the nuclear torpedo for firing, partly because he feared the Communist Party political officer who watched his every move might decide he wasn’t aggressive enough.

(The captain admitted to his second in command he didn’t intend to fire the nuclear missile. “We’d go up with it if we did,” he said.)

Ellsberg also highlights the Soviet Union’s “Perimeter” system, which was designed to automatically launch a destructive nuclear counterattack if its electronic sensors detect an incoming attack on Moscow.

Perimeter is triggered in much the same way as the doomsday device described by the Russian ambassador in the 1964 movie “Dr. Strangelove,” a black comedy about an accidental nuclear war that destroys the planet.

Ellsberg writes that the movie “was, essentially, a documentary.”

The film also accurately explained that, under American war plans at the time, there was no way to recall strategic bombers once they were sent on their missions, nor was there “any physical restraint on the ability of a squadron commander, or even a bomber pilot, to execute an attack without presidential authorization.”

Another frightening aspect of US nuclear plans of the time: They provided only for all-out war. If the Soviet Union launched nukes against the US, American generals would retaliate by striking both the Soviet Union and China, assuming they were allies who would fight together.

A Marine general briefed on the plan at a secret government meeting in 1960 said it was immoral to kill 300 million Chinese in a war they did not start. But the plan stood.

“It was my passion to change it,” Ellsberg writes in the book.

During the Kennedy administration, he succeeded in helping write a new plan that allowed for the possibility of a more limited nuclear war. The US also adopted “fail-safe” systems that allow the president to recall nuclear bombers in the event the White House wants to cancel a strike order.

But over time, Ellsberg realized that adjusting strategy was not enough to guard against nuclear weapons’ accidental use. A president could mistakenly launch as many as 800 weapons in less than 10 minutes, the Arms Control Association estimates. And, unlike planes, warheads on missiles can not be called back.

Currently North Korea is trying to build a nuclear arsenal capable of attacking the United States, heightening worries about Armageddon.

We are right to be terrified: A single 800-kiloton H-bomb exploded over Midtown would instantly vaporize skyscrapers and everything else for miles around and kill millions in the following minutes.

President Trump added to the fear in August, when he said North Korea’s threats to the US “will be met with fire and fury like the world has never seen.”

At a UN speech in September, Trump threatened to “totally destroy” North Korea and said “Little Rocket Man” — dictator Kim Jong-un — “is on a suicide mission for himself and for his regime.”

In November, the Senate held the first congressional meeting in 41 years on the president’s nuclear-weapons authority. US generals have said that in a crisis, they will not obey illegal orders from Trump.

But the aggressive language on both sides underscores a danger that persists even though the world’s overall stockpile of atomic bombs is declining, Ellsberg writes.

Since the 1970s, arms treaties have cut the number of nuclear warheads by more than 80 percent. The US stockpile peaked at about 31,255 warheads in the 1960s. Today, the US has about 4,000 nuclear warheads deployed or ready to deploy, and Russia — the successor to the Soviet Union — has about 4,500.The US and Russian arsenals dwarf those of other nuclear-armed countries, who have a total of around 1,100 to 1,200 nuclear weapons.

That’s still far too many, says Ellsberg, who sees just one way to avoid a mistake that could kill us all.

“The risk that one city will be destroyed by a single (perhaps terrorist) weapon in the next year or the next decade cannot, unfortunately, be reduced to zero,” Ellsberg writes. “But the danger of near-extinction of humanity — a continuous possibility for the past 65 years — can be reduced to zero by dismantlement of most existing weapons in both the United States and Russia.”