At the Republican National Convention, where Mr. Trump secured his party’s nomination, Mr. Giuliani waved his arms, shouted, clenched his fists and thundered against Mr. Trump’s Democratic opponent. “Hillary Clinton is for open borders,” Mr. Giuliani claimed, warning ominously of “operatives who are terrorists, who are going to come to Western Europe and here and kill us.”

It was a classic Giuliani performance that lit up the crowd in Cleveland at a high point for Mr. Trump. And months later, when the release of an “Access Hollywood” tape revealed Mr. Trump speaking graphically about assaulting women, Mr. Giuliani was one of the few people to publicly defend the candidate.

Mr. Trump rewarded that loyalty by seriously considering Mr. Giuliani for a series of posts in the administration. Transition officials informally discussed with Mr. Giuliani the positions of attorney general, secretary of homeland security, and director of national intelligence. Mr. Giuliani wanted none of them.

What he did want was secretary of state, and his decision to make that plain as day to anyone who asked might have helped scuttle his chances. Some transition officials were also concerned that Mr. Giuliani, 72, might not have the stamina for the globe-trotting job.

In the end, Mr. Giuliani removed his name from consideration in mid-December after the drawn-out public audition, shortly before Mr. Trump announced he had chosen Mr. Tillerson.

Now, the ill-defined cybersecurity post may be Mr. Giuliani’s best hope of adding some Trump administration luster to his private security business. But his previous forays into the national and international security arena have been less than a success.

After leaving the mayor’s office, he received a multimillion-dollar signing bonus to join a Houston-based law firm with oil industry connections, but spent a majority of his time working on a fast-growing international security business called Giuliani Partners.

In 2004, Mr. Giuliani pushed President George W. Bush to choose Bernard Kerik, his former police commissioner and an associate at Giuliani Partners, as homeland security secretary. Mr. Kerik’s nomination was abruptly pulled after it was revealed that he had employed an unauthorized immigrant as a nanny. Mr. Giuliani’s influence with the Bush team evaporated, and Mr. Kerik was later sentenced to four years in prison on federal tax charges.