The head of spy agency GCHQ has announced he is to step down after just over two years in the post - sparking a search for candidates to take over one of the most senior roles in British intelligence.

Robert Hannigan informed Foreign Secretary Boris Johnson of his intention to leave the role for personal reasons.

He spoke of his pride at how many lives had been saved by the agency's work and its "relentless" efforts to counter terrorism, crime and other threats.

The 51-year-old said he has been " lucky enough to have some extraordinary roles in public service over the last 20 years", adding: " But they have all demanded a great deal of my ever patient and understanding family, and now is the right time for a change in direction."

Mr Hannigan was d irector general of defence and intelligence at the Foreign Office before he succeeded Sir Iain Lobban at GCHQ.

His tenure at the Cheltenham-based agency started in November 2014, following a period of intense scrutiny of its work sparked by revelations by former US intelligence contractor Edward Snowden.

GCHQ is often referred to as Britain's listening post.

Its activities have taken on particular significance in recent years as terrorists use increasingly sophisticated technology to avoid detection, while the threat from cyber crime has evolved.

In a letter to Mr Johnson, Mr Hannigan wrote: "After a good deal of thought, I have decided that this is the right time to move on and to allow someone else to lead GCHQ through its next phase.

"I am, like you, a great enthusiast for our history and I think it is right that a new director should be firmly embedded by our centenary in 2019.

"I am very committed to GCHQ's future and will of course be happy to stay in post until you have been able to appoint a successor."

Mr Hannigan, a married father-of-two, was born in Gloucestershire and studied classics at Wadham College, Oxford.

He described leading the men and women of GCHQ as a "great privilege".

He wrote: "I am proud of what we have achieved in those years, not least setting up the National Cyber Security Centre and building greater public understanding of our intelligence work.

"I am equally proud of the relentless 24-hour operational effort against terrorism, crime and many other national security threats.

"While this work must remain secret, you will know how many lives have been saved in this country and overseas by the work of GCHQ.

"Underpinning this is our world-class technology and, above all, our brilliant people.

"As you know, I have also initiated the greatest internal change within GCHQ for 30 years, and I feel that we are now well on the way to being fit for the next generation of security challenges to the UK in the digital age."

Mr Johnson's response thanked him for his service.

He said: "You have led the renewal of some of our most important national security capabilities, which we continue to depend on every day to save lives from terrorism and to protect our interests and values.

"Following your successful tenure, and thanks to the work of thousands of excellent GCHQ staff, the organisation is well placed to play its part continuing to protect our nation."

An internal competition will be carried out to identify candidates for the job. These will be sent to Mr Johnson and the Prime Minister for a final decision.