M is a codename held by a number of fictional characters in Ian Fleming's "James Bond" book and film series.

Fleming based much of M's character on Rear Admiral John Godfrey, who was Fleming's superior at the Naval Intelligence Division during the Second World War. After Fleming's death, Godfrey complained "He turned me into that unsavory character, M."

Other possible inspirations include Lieutenant Colonel Sir Claude Dansey, the deputy head of MI6 and head of the wartime Z network, who achieved different interpretations of his character from those who knew him: Malcolm Muggeridge thought him "the only professional in MI6", while Hugh Trevor-Roper considered Dansey to be "an utter shit, corrupt, incompetent, but with a certain low cunning". Further inspiration for M was Maxwell Knight, the head of MI5, who signed his memos as "M" and whom Fleming knew well. The tradition of the head of MI6 signing their name with a single letter came from Mansfield Smith-Cumming, who would sign his initial "C", with green ink.

Another possibility for the model of M was William Melville, an Irishman who became the head of the Secret Service Bureau, the forerunner of both MI5 and MI6: Melville was referred to within government circles as M. Melville recruited Sidney Reilly into government service and foiled an assassination plot against Queen Victoria on her 1887 Golden Jubilee.

More Info: en.wikipedia.org