Robert Armstrong, Lord Armstrong of Ilminster, who has died aged 93, was the supreme Whitehall mandarin – discreet, a staunch defender of official secrecy, a loyal servant of his political masters. He held all the key posts as he passed through the Whitehall corridors of power.

He was private secretary first to the liberal, reforming Labour home secretary Roy Jenkins, and subsequently, between 1970 and 1975, to a Conservative prime minister, Edward Heath, and a Labour one, Harold Wilson. He was appointed permanent secretary at the Home Office between 1977 and 1979, before reaching Whitehall’s summit, the post of cabinet secretary, a position he occupied between 1979 and 1987, most of Margaret Thatcher’s premiership.

It was during the Thatcher period that Armstrong’s name became known around the world when, in 1986, he appeared as a key witness in the so-called Spycatcher trial, as the British government attempted to prevent publication in Australia of the memoirs of Peter Wright, a former MI5 officer.

In the witness box in the Sydney court, Armstrong came out with a phrase that captured headlines around the world. The phrase was “economical with the truth”.

He had been asked by Malcolm Turnbull, Wright’s lawyer, why he had written to the publishers Sidgwick & Jackson, saying Thatcher wanted a copy of a book on MI5 by Chapman Pincher, when he was already in possession of the manuscript. The letter, Turnbull suggested, “contains a lie”. Armstrong replied: “It was a misleading impression, it does not contain a lie. I don’t think.”

Turnbull: “What is the difference between a misleading impression and a lie?”

Armstrong: “A lie is a straight untruth.”

Turnbull: “What’s a misleading impression, a kind of bent untruth?”

Armstrong: “As one person said, it is perhaps being economical with the truth.” He pointed out that the phrase was not his own – it was first used by the philosopher Edmund Burke, he told the court. But it was too late.

Armstrong’s response in the Sydney courtroom betrayed a lack of understanding, or at least experience, of the outside world. Comments that may well have earned plaudits in Pall Mall clubs might not necessarily be appreciated on the street.

Armstrong strongly believed that civil servants should remain silent to the grave. A duty of lifelong confidentiality was even more necessary, he believed, when it came to members of the security and intelligence agencies. In his affidavit to the Sydney court, he insisted that the publication of Spycatcher was likely to cause “unquantifiable damage”. But the government lost its case.

Of all his political masters, Armstrong developed a particularly close personal relationship with Heath. They shared a deep love and knowledge of music. Both were accomplished pianists. Armstrong was fond of recalling the evening of 28 October 1971, when Heath secured a healthy Commons majority of 356 to 244 votes in favour of entry into the European Community, or the Common Market as it was more widely called, a success Heath regarded as his greatest political achievement.

Returning to No 10 Heath walked past his staff, briefly acknowledging them but without saying a word went upstairs to his clavichord to play the First Prelude from Book 1 of Bach’s Well Tempered Clavier.

After Heath died, in 2005, Armstrong became the first chair of the Sir Edward Heath Charitable Foundation, a post he retained until 2013. He strongly defended Heath when, more than a decade after his death, the former prime minister was accused of sexually abusing children. Armstrong described the decision by the Wiltshire police to discuss their investigations at a press conference as “disgraceful”. He added: “You usually detect some sexuality. I never heard a whiff of sexuality about Heath, whether women or children. He was completely asexual. He had a girlfriend, a copy of his girlfriend’s photo by his bedside. She went off with someone else.”

Born in Headington, Oxford, Robert was the son of the organist and eventual principal of the Royal Academy of Music Thomas Armstrong, and his wife, Hester (nee Draper). He was educated at the Dragon school, Oxford, at Eton, where he was a King’s scholar, and at Christ Church, Oxford, where he studied classics and philosophy. It was an elitist, privileged education.

It was also then a traditional route to the foothills of Whitehall’s corridors of power via the Treasury – the fact that he had studied classics, rather than economics, was no obstacle to a career there. Far from it, and it was at the Treasury, the most influential of government departments, where Armstrong first made his mark, starting as an assistant principal in 1950.

Yet Armstrong will always be identified most with the Thatcher years, as an eminence grise who was happiest remaining in the shadows, advising the prime minister on the need for secrecy and urging his fellow Whitehall mandarins to take a tough line against leaks.

However, he was thrust into the limelight as he was caught up in a series of spectacular Whitehall crises. Asked to conduct an inquiry into the 1986 Westland helicopter affair, he concluded that Leon Brittan, the trade and industry secretary, had leaked a letter damaging to the defence secretary, Michael Heseltine, without saying that Thatcher knew about the stratagem all along.

He was subsequently asked by a member of the Commons defence committee whether Thatcher’s remark to MPs that she had given her consent to get “accurate information to the public domain”, as she put it, conflicted with the official version of events. Armstrong admitted her remark “did not coincide” with an earlier statement in which she said that her agreement was “neither sought nor given”. He explained: “My understanding is it would be a slip of the tongue.”

Armstrong’s defence of official secrecy led him to assert an absolute constitutional doctrine to prevent government officials from whistleblowing, or in any way publicly questioning government policy. Stung by the acquittal of Clive Ponting, charged with breaking the Official Secrets Act for sending details of the movements of the Belgrano, the Argentinian cruiser torpedoed by a British submarine during the Falklands war, he rejected the argument by defence lawyers that civil servants could appeal above the heads of ministers to the “public interest”.

He issued new guidelines on the duties and responsibilities of government officials. Civil servants may be “servants of the Crown”, he said, but added: “For all practical purposes the Crown in this context means, and is represented by, the government of the day.” He continued: “The civil service as such has no constitutional personality or responsibility separate from the duly elected government of the day.”

In other words, civil servants had no direct constitutional links with parliament and could never appeal to an overriding “public interest”.

Thatcher did not always accept his advice. He told her that Jimmy Savile should not be given a knight- hood on the grounds, he felt, that “things were not quite what they should be”. He noted that Savile had made no attempt to deny the accounts in the press about his private life. Armstrong’s instincts turned out to be correct.

He was a warmer, more sensitive man than he allowed the public – or many of his professional colleagues – to appreciate. He also possessed a dry humour. One encyclical he sent to senior Whitehall officials deploring leaks was itself leaked. When the journalist, now film-maker, Paul Greengrass, raised this during a Granada TV World in Action interview, Armstrong replied: “I was very sad that it took so long as six weeks to leak. I hoped it would leak much sooner than that.”

He was knighted in 1978 and made a life peer 10 years later, after he retired, sitting in the Lords as a crossbencher. Also in 1988, he was appointed chair of the Victoria and Albert Museum trustees, where the deputy was his Oxford contemporary Sir Michael Butler, former ambassador to the European Community. He was appointed non-executive director to many companies, including BAT Industries, the bankers NM Rothschild and Sons, Rio Tinto, Shell, Lucas Industries, Carlton Television and the Bank of Ireland.

But it was music that mattered most to him outside Whitehall. He was secretary and then a board member of the Royal Opera House from 1968 to 1993. He once said he tried to write his cabinet minutes in sonata form, with an exposition, development section, recapitulation and coda. As a guest on BBC Radio 4’s Desert Island Discs, he chose as a luxury some music manuscript paper, a pencil and paper.

In 1985 he married Patricia Carlow, and she survives him along with his two daughters, Jane and Teresa, from his first marriage, to Serena (nee Chance), which ended in divorce.

• Robert Temple Armstrong, Lord Armstrong of Ilminster, civil servant, born 30 March 1927; died 3 April 2020