Israeli Prime Minister Ehud Barak appears to be hanging on to power by a thread. He may have narrowly averted the end of his government by surviving a no-confidence motion this week, but the Knesset sent a powerful anti-Barak message when it elected a member of the opposition as Israel’s eighth president.

On Monday, legislators elected Likud Party lawmaker Moshe Katsav president in a vote that was believed to be as much a stinging rebuke to Barak as it was to Katsav’s opponent, former Prime Minister Shimon Peres.

With the Knesset about to go into recess until October, Barak now has three months to stabilize his governing coalition and pursue the peace process.

Barak “is prime minister simply because there are not 61 Knesset members who want to go to elections,” said Yaron Dekel, Israel Television’s chief political commentator.

Barak “cannot pass anything in the Knesset — not a president, and not a budget and not gardening and watering legislation.”

In the no-confidence balloting, legislators voted 50-50 with eight abstentions, which fell short of the 61 votes needed in the 120-member Knesset to topple the government. Twelve Knesset members did not attend the session.

Among those who abstained was Barak’s foreign minister, David Levy, who has accused the premier of making too many concessions during the Camp David summit with Palestinian Authority President Yasser Arafat.

Levy had threatened to resign in the coming days if Barak does not make a serious effort to form a national unity government with Likud.

During the pre-vote debate, opposition leader Ariel Sharon also accused Barak of making too many concessions, saying the offers Barak made at the failed summit “set a dangerous precedent for Israel.”

Before setting off for Camp David, Barak suffered the defections of three parties from his government, a move that left his coalition with only 42 Knesset seats.

During his speech before the no-confidence vote, Barak accused the defecting parties and the opposition of pursuing their own narrow interests instead of serving the public good.

Earlier in the day, Katsav defeated Peres in a secret parliamentary vote that defied all forecasts.

Katsav, 54, beat the 77-year-old Peres, who had been widely expected to win, in a second round of voting by 63-57.

The additional balloting for the largely ceremonial post was held after Katsav defeated Peres in the first round, but failed to receive the necessary 61 votes.

The opposition seized on the results as a renunciation of Barak’s government.

“This was an informal no-confidence vote in the prime minister,” Sharon declared immediately after the results were announced.

“It shows what the national camp can do when it works together.”

President-elect Katsav was to be sworn in at a ceremony Tuesday evening.

Little known abroad, Katsav has served as a right-wing legislator in the Knesset since 1977. He has held several deputy minister portfolios and served as tourism minister in the government of former Prime Minister Benjamin Netanyahu.

Speaking after the Knesset vote, Katsav said he hoped his presidency would be “one of quiet and unity. I will work to reduce the tension in society.”

Katsav’s predecessor, Ezer Weizman, stepped down last month in the shadow of a financial scandal.

Katsav said that despite his lengthy activity in the Likud Party, “Today I have to lock the door on my political views, for the next seven years,” when his term ends.

Born in Iran, the 54-year-old father of five has championed the rights of Israel’s Sephardi population.

Along with the Likud, Orthodox parties welcomed the election of Katsav, who is religiously observant.

Monday’s vote was a painful blow to Peres, for whom the presidency would have topped off a lengthy diplomatic and political career.

It would also have shaken off Peres’ image as a perpetual loser in elections. While he has held a variety of top posts, Peres has lost four out of five elections for prime minister.

Peres had counted on the support of Orthodox legislators.

But officials of the fervently Orthodox Shas Party said all of the party’s 17 lawmakers had supported Katsav. A day before the vote, a Shas leader, Rabbi Yitzhak Kadouri, had a vision that Katsav was God’s favorite.

An architect of the Oslo accords, Peres shared the 1994 Nobel Peace Prize with Prime Minister Yitzhak Rabin and Arafat.

Peres would not immediately comment on the vote or his future plans.

“At this moment, the only thing I have to say is to congratulate Moshe Katsav on his election as president,” Peres said. “I wish him success and all the best.”