Robert Gates has been an extraordinarily distinguished public servant. A recipient of the Presidential Medal of Freedom, he has worked for eight presidents, serving as defense secretary under both President George W. Bush and President Barack Obama. The nation owes him a deep and enduring debt of gratitude. But his new memoir, “Duty,” raises troubling ethical questions.

A former public official has a special obligation -- in fact a duty -- not to betray the confidence of a sitting president, especially in the area of national security. Even if private conversations involve people who are no longer in office, the official should hesitate before using them to embarrass or attack those with whom he worked closely -- certainly if his own motivations include selling books, settling scores, justifying his own positions or even defending his place in history.

Of course, Gates isn’t the first person to reveal private discussions, but betrayals of confidence don’t become justified merely because they have precedents.

Gates announces, on his very first page, that he will discuss “political wars with the White House, occasionally with the presidents themselves -- more with President Obama than with President Bush.” Consistent with that announcement, he discloses a large number of confidential conversations.

For example, Gates reports that Bush said he wished he had fired Donald Rumsfeld, Gates’s predecessor as secretary of defense, “a couple of years earlier.” He describes sharp disagreements within the Bush administration about whether to close the prison at Guantanamo Bay, Cuba (and specifically mentions the role of adviser Ed Gillespie, who was concerned about the effects on “the Republican base”). With respect to the conduct of the Iraq War, he lays out his various disagreements with Bush and General David Petraeus (among others).

He quotes John Podesta, the head of the Obama transition team after the 2008 election, as saying that “the Obama team tend to be control freaks” when it comes to dealing with the news media. Naming names, he offers detailed accounts of divisions within the Obama administration about how to handle the Arab Spring and the unrest in Egypt. He provides a similarly detailed account of confidential deliberations about the war in Afghanistan. (Disclosure: I am serving on the President’s Review Group on Intelligence and Communications Technologies. My wife, Samantha Power, who worked on the national security staff in the Obama administration’s first term, is now U.S. ambassador to the United Nations.)

He offers a lot of details about the contentious internal processes that led to elimination of the “don’t ask, don’t tell” policy. He quotes numerous private remarks from Obama, including what Gates remembers as the president’s concern, at one point, that if he didn’t take action, gay-rights groups “will go crazy.” Gates complains that Obama, in announcing that he would get rid of the policy, issued a “pre-emptive strike” that “irked the military -- and me.”

Gates can be generous, and he praises many people with whom he worked (including Bush and Obama), but he has harsh words as well. He says that Vice President Joe Biden “has been wrong on nearly every major foreign policy and national security issue over the past four decades.” He complains that in the Obama administration, a “number of the new appointees, both senior and junior, seemed to lack an awareness of the world they had just entered.”

He “felt that agreements with the Obama White House were good for only as long as they were politically convenient.” He says that the White House’s decision “to take credit for every good thing that happened while giving none to the people in the cabinet departments -- in the trenches -- who had actually done the work, offended Hillary Clinton as much as it did me.” He laments what he considers “the Obama White House’s distrust of the nation’s military leadership.”

Gates’s disclosures of private communications are worse than troubling, and it is unclear why he thought it would be appropriate to mount various attacks on his recent colleagues in the executive branch. Officials should generally be entitled to assume that they can speak to their colleagues in confidence, and that their colleagues won’t reveal what they said or attack them publicly, certainly not while they remain in office. In the area of national security, there is a particular risk, which is that disclosures of high-level conversations, and various statements of opinion (“distrust of military leadership”), might be affirmatively harmful to the United States.

To be sure, one’s colleagues might well be wrong, but they are usually operating in good faith, and they are owed a duty of loyalty. And while Gates appears to have a phenomenal memory, it would be astounding if everything happened in exactly the way he says it did.

Of course, the duty of loyalty isn’t absolute. If an administration has engaged in serious wrongdoing, that duty can be overcome. Undoubtedly, the duty recedes over time. There is a plausible argument that high-level advisers have more room to disclose confidential communications after the president whom they served leaves office.

Maybe Gates meant to write for history; maybe he thought that by offering an insider’s view of what happened, he was doing a service for posterity. But most of his disclosures add nothing of importance to the historical record. He could easily have written a different book, drawing on his extraordinary and admirable public service and exploring past and present issues, without revealing private discussions.

Gates is an honorable man. He was an honorable public servant. But in breaching the trust of his former colleagues, he has committed a dishonorable act.

(Cass R. Sunstein, the Robert Walmsley University Professor at Harvard Law School, is a Bloomberg View columnist. He is the former administrator of the White House Office of Information and Regulatory Affairs, the co-author of “Nudge” and author of “Conspiracy Theories and Other Dangerous Ideas,” forthcoming in March.)

To contact the writer of this article: Cass R. Sunstein at csunstei@law.harvard.edu.

To contact the editor responsible for this article: Katy Roberts at kroberts29@bloomberg.net.