The Americans have encouraged improvements in Iraq’s relations with Sunni Arab states in hopes of reducing Iran’s influence. Mr. Abadi was in Riyadh on Sunday along with Mr. Tillerson to strengthen ties.

Earlier Monday, Mr. Tillerson made a secret two-hour visit to the main American air base in Afghanistan, arriving in a military transport plane to meet top Afghan officials inside a massive bunker.

His visit to Iraq was similarly unannounced before he landed.

That top American officials must use stealth to enter these countries after more than 15 years of war, thousands of lives lost and trillions of dollars spent was testimony to the stubborn problems still confronting the United States in both places.

Mr. Tillerson would not even risk the short trip to Kabul from Bagram Air Base to visit the heavily fortified United States Embassy or Afghan presidential palace, as his predecessors have done. The change reflects the increasingly uncertain security situation in Kabul and the fact that the United States’ presence is now surrounded by vast Taliban-controlled areas.

Mr. Tillerson’s visit was his first to Afghanistan as secretary of state, and like nearly every other top American official to visit over the previous two decades, he said the country’s predicament was not nearly as dire as his own security precautions suggested.

“But I think if you consider the current situation in Afghanistan, and we were talking about this a few minutes ago, and you look a few years in the past to what the circumstances were, Afghanistan has come quite a distance already in terms of creating a much more vibrant population, a much more vibrant government, education system, a larger economy,” he said in a small windowless conference room during a hurried eight-minute news conference. “So there are opportunities to strengthen the foundations of a prosperous Afghanistan society.”

Cloistered in the American military compound, Mr. Tillerson saw none of that hoped-for blooming. Instead, he and his staff exited a huge military transport plane and then piled into a motorcade that drove them the few minutes to the base’s bunkerlike headquarters, passing hangars constructed by Russia, another of the foreign forces to be humbled in Afghanistan. Huge concrete blast walls lined much of the route. Helicopters patrolled the perimeter, and two security blimps equipped with long-range cameras hovered.