The US has said it wants the standoff over the Iranian nuclear programme solved through diplomacy, but it has not ruled out military action against Iran.
Khamenei said: "They should not intimidate the Iranian people with these things, since the United States has previously attacked Iran."
The US accuses Iran of seeking a nuclear weapon through its atomic programme, a charge denied by Tehran which insists its research is peaceful in nature.
"If the US dares to attack Iran, it will be the end-all and be-all of everything the US ever stood for"
MPerry4721, Shenorock, US
Khamenei did not specify what such retaliation would involve but expressed confidence that the US would have the "sanity" not to be tempted to launch a strike.
He told air force commanders during a ceremony to mark the 28th anniversary of the Islamic revolution: "We believe that no one will make such an irrational and erroneous move and will not jeopardise his nation's interests.
"Of course some say the US president does not think about the consequences of his actions, but some sanity could be put into the head of even these people.
"The American politicians and analysts know that the Iranian people have never let any aggression go unanswered."
The Iranian supreme leader, 67, also scoffed at reports that he was gravely ill or even dead.
Khamenei said: "The enemies of the Islamic republic make different rumours about death or sickness to weaken the spirit of the Iranian nation for a few days, but they do not know that they are not facing a single person, but a nation."
He said Iranians would show "their alertness" in mass street rallies planned for Sunday, the culmination of celebrations to mark the 28th anniversary of the Islamic revolution.
"This is the day when the American and the Israeli and British intelligence people get angry and worried. This year, with the glorious popular presence, our enemies will be inflamed and dismayed."
Iran, which is under UN sanctions regarding its nuclear programme, has repeatedly vowed it will not cave in to the key demand that it freeze uranium enrichment.
However, it also insists that negotiations are the way to solve the standoff.
In Moscow on Thursday, Sergei Lavrov, Russia's foreign minister, told Ali Akbar Velayati, an envoy of Khamenei, that Russia wants to see a positive response from Tehran on resolving the nuclear stand-off.
Javier Solana, the EU foreign policy chief, also said on Thursday that he was open to meeting Iran's chief nuclear negotiator at a security conference in Munich which begins on Friday.
Ali Larijani, Iran's national security chief, said on Wednesday that he planned "negotiations" at the conference, in the first such contacts since the UN imposed sanctions on Iran.