Intelligence officers with military ranks

In spy novels and films, you often come across agents of an intelligence service that are referred to by a military rank. This seems to be particularly common in the inteligence services of the Communist countries, but is not exclusive to them; the James Bond franchise, for instance, frequently refers to Bond by his rank of commander.

What is the background to this (assuming that there is a kernel of truth in this literary usage)? Are intelligence services considered part of the military sphere and therefore, by themselves, award military ranks to their staff? Or are the intelligence services civilian rather than military organisations (which I think is the case), and such people would hold a rank from a branch of the armed forces where they served previously and from where they were seconded to intelligence?

IIRC, James Bond was a Naval Officer, who happened to be attached to MI6.

In the US Navy, we have intel Officers, who do tours at other, predominately civilian departments.

Yes, intelligence agencies in many countries are officially part of the military. Sometimes, there exist additional civilian structures as well (as part of the police, interior ministry, or a completely separate structure).

There’s civilian intelligence services and actual military intelligence units. Depending on the country the nominal line where their respective spheres are separated can be in different locations. That line can also be a bit blurry with civilian intelligence officers working in duties in a more military realm and military members assigned duties that are mostly in support of civilian agency goals.

The Skripal nerve agent poisoning is an example where military troops were used for something that we’d typically think of as an intelligence operation. The GRU, Russian military intelligence, long controlled Soviet/Russian special operations troops (Spetznaz) that were well suited to those kinds of clandestine direct action missions.

Bond as written was a military intelligence officer. He’d started in Naval Intelligence and was still in the Royal Navy Reserve. He’s working in a civilian capacity for a civilian agency in the books but he’s still a Royal Navy officer. He’s both in effect. He’s a reservist who’s civilian job is as a spy. I’ve actually known some analysts that mixed reserve component membership with civilian occupations in one of the US intelligence agencies. It’s a less cool parallel since they were analysts not field agents.

The higher-ranking officials of the CIA, who deal with the public, are often retired generals or admirals.

When hiring, most US government agencies give preference to military veterans.

In the Soviet Union, the KGB was a uniformed service, When not undercover, they wore army-style uniforms (their branch color was blue instead of red), and they had army-style rank titles. I think most of the Warsaw Pact countries did the same.