A Virginia Senate committee, chafing at the threat of losing as much as $30 million a year in federal highway funds, approved a measure today that could raise the state's legal drinking age for beer to 21.

But the committee, long hostile toward an increase in the state's drinking age, also tacked on a controversial amendment that might keep the law from becoming effective. By an 8-to-6 vote, the Rehabilitation and Social Services Committee declared that the age limit would not be raised in Virginia until Congress also requires that the drinking age on all military installations be raised to 21.

Under current Defense Department regulations, military installations generally conform to local and state drinking laws. Legal drinking ages in Virginia are 21 for liquor and wine and 19 for sale of beer for off-premises consumption. Most of the many military bases in the state adhere to the state law.

As initially proposed, the bill would have raised Virginia's drinking age to 21 effective July 1. Anyone who becomes 19 before July 1 could, under a grandfather clause in the bill, continue to buy beer in the state.

The controversial amendment was seen as an attempt by the Virginia legislators to get back at Congress for passing a law that threatens to withhold up to 10 percent of federal highway funding from any state that does not enact a minimum drinking age of 21 by the end of 1986.

"The purpose is for . . . Congress to get off its hypocrisy, demanding the states conform to a federal law" not imposed on military bases, said state Sen. Dudley J. (Buzz) Emick Jr. (D-Botetourt).

State Sen. Charles L. Waddell (D-Loudoun), chairman of the Senate Transportation Committee, said he voted for the higher drinking age for the first time today but opposed the restrictive amendment.

"As transportation chairman I can't sit idly by and see the state lose millions in federal funds," said Waddell in an interview. "It's a charade and is really just playing games to send Congress a message. I'd like to see us get this issue behind us."

Many legislators, even if they were previously opposed to the higher drinking age, see a drinking age of 21 as inevitable given the threat of a loss in federal funds.

Democratic Gov. Charles S. Robb has urged the legislators to approve the age 21 measure, although he disapproves of the pressure from Congress. Robb even suggested the legislature might want to pass a resolution condemning the federal intervention on what many see as a state issue. Robb is not expected to support the restrictive amendment and its fate is not certain, legislators said today.

The bill, sponsored by state Sen. Richard L. Saslaw (D-Fairfax), was approved by a vote of 8 to 6 without the emotional debate that has packed committee rooms and caused tempers to flare in the past.

Saslaw, who opposed the restrictive amendment, suggested in an interview that it was "Massive Resistance 1985," a joking reference to the state's fierce, but futile, effort to oppose desegregation in the 1950s and early 1960s.

"I resent it and disagree with that," said state Sen. Robert C. Scott (D-Newport News), who is black. Scott, a longtime opponent of higher drinking ages, said "it's no surprise" that limiting the number of people who drink will also limit the number of traffic accidents caused by drinking.

The bill heads for a Senate floor vote Tuesday or Wednesday. If approved, it will have to go to the House, where a similar bill is awaiting committee action.