President Reagan today turned a tour of the Korean Demilitarized Zone into a bristling defense of his foreign policy and a warning to the North Koreans never again to attack American soldiers.

Reagan, protected by camouflaged netting and tight security, told the 2nd Infantry Division at this U.S. Army outpost less than two miles from North Korea that they are on "the front lines of freedom."

Reagan turned what had been described beforehand as a largely symbolic speech about U.S. security commitments in Korea into a full-scale defense of administration policy in Grenada and Lebanon and a blunt condemnation of communism.

"You're facing a heavily armed, unpredictable enemy with no regard for human life," Reagan said. "Let us always remember Aug. 18, 1976, the day two Army officers--Maj. Arthur Bonifas and 1st Lt. Mark Barrett--were murdered across from here by axe-wielding North Korean troops. Let me state for the record, and I know you feel that way: Nothing like that had better happen again."

Bonifas and Barrett were the last U.S. soldiers killed in military action along the Demilitarized Zone. The last fire fight in the small American sector of this zone occurred two years ago, and the only barrages fired at Americans since then have been propaganda leaflets. But 11 North Korean infiltrators have been killed this year by South Korean troops along the 151-mile-long DMZ, most of which is patrolled by the South Koreans.

"You stand between the free world and the armed forces of a system hostile to everything we believe in as Americans," Reagan said. "The communist system to the north is based on hatred and oppression. It brutally attacks every form of human liberty and declares those who worship God to be enemies of the people. Its attack against the leaders of the South Korean government in Rangoon where four high-ranking South Korean officials were killed in an Oct. 9 bombing made clear what kind of enemy you face across the DMZ."

Reagan was driven into the DMZ to Guardpost Collier, .6 miles from North Korea, where he looked through binoculars at what servicemen say is only a facade of a North Korean village.

"It looks like a Hollywood back lot and it isn't any more important," said Reagan, a former actor.

Earlier, Reagan's limousine, flying a white flag of truce as required under the 1953 armistice, drove him to a chilly outdoor prayer service, held under a canopy of green and brown camouflage netting.

In his speech to the troops, Reagan defended U.S. military commitments throughout the world, saying that "in Lebanon, for example, our Marines are peace-keepers in the truest sense of the word . . . . More than 230 of our marines and soldiers gave the last full measure of devotion in that honorable endeavor and each of us is indebted to everyone of them."

Then Reagan turned to the Grenada operation, saying it was "a slur and misstatement" to call the U.S. military action there an "invasion," even though he himself has used this word to describe it.

The action in Grenada, Reagan said, was the opposite of what has happened in Afghanistan where 100,000 Soviet troops are "trying to force a dictatorship down the throats of the Afghan people.

"Why are the Soviets being attacked by the people of Afghanistan, while our U.S. and Caribbean forces have been greeted as liberators in Grenada?" Reagan asked rhetorically. "The answer is, no people in history have ever chosen to become slaves."

Giving a brief history of U.S. involvement in Korea, Reagan recounted the deeds of some Medal of Honor winners in that war.

"Americans are now standing tall and firm," Reagan declared. "No terrorists should question our resolve. No tyrant should doubt our courage."

Reagan's visit to the DMZ was designed to dramatize his commitment to a continued U.S. military presence in Korea, where the Korean conflict ground to a halt 30 years ago along the line near the 38th Parallel where it began. This temporary truce line became the 2 1/2-mile-wide Demilitarized Zone and is now the boundary dividing the two Koreas.

Reagan spent more than three hours in front-line positions before returning to Seoul.

A U.S. military spokesman who briefed reporters in Seoul yesterday said the president was in relatively little danger because of the tight security and camouflaged netting. "No one is going to have a clear look at him," the spokesman said.

The DMZ visit gave Reagan, the fifth U.S. president to visit South Korea, an opportuntity to express his hostility toward communism and to rally both Americans and South Koreans behind his policies. In his weekly Saturday radio speech to the American people, in which he gave a quick verbal tour of the ceremonies he attended during his week in the Far East, Reagan said: "Free people, no matter where they live, must stand together against terrorism. We stand united with the people of Japan and Korea."

Last night, at a state dinner hosted by South Korean President Chun Doo Hwan, Reagan delivered a fiery condemnation of the Soviets and North Koreans.

"The murder of 269 innocent people in a defenseless airliner, the very absence here tonight of some of your nation's finest servants, these events have written in blood the stark contrast between those nations that respect human life and those that trample it," Reagan said. "The vicious attack in Rangoon dramatizes the threat your people face. We must stand together to confront this dangerous challenge and to preserve the peace. And this we will do."

Reagan then reaffirmed his own basic ideological conviction about what he sees as the worldwide power struggle between communism and freedom.

"The tide of history is a freedom tide and communism cannot and will not hold it back," Reagan said.