The SRU has announced that Townsend will take over in June 2017 when Cotter’s contract expires.
Townsend, 43, has been at Glasgow Warriors since 2012 and guided them to the Guinness Pro12 title in 2015. They have reached the play-offs in every season he has been at the club. The former stand-off was previously part of the Scotland coaching team under Andy Robinson.
One of the most gifted Scotland players of his generation, he was capped 82 times by Scotland and helped them win the 1999 Five Nations championship, scoring a try in each match.
His Glasgow contract also expires in June 2017.
Cotter, 54, left French club Clermont in 2014 to become Scotland coach.
He had a disappointing first season which ended with a Six Nations whitewash.
But he impressed during last year’s Rugby World Cup, steering Scotland into the quarter-finals where they were unlucky to lose to Australia.
In a statement released by the SRU, Townsend said: “I am incredibly proud to take on the Scotland head coach role at the end of the season. It is a real honour to be given the responsibility of coaching the national team and I look forward to working with such an impressive group of players.
“I’d like to thank Scottish Rugby for this opportunity and also the supporters, players and staff at Glasgow Warriors for their support over the past four years. My focus is firmly on the season ahead with Glasgow and I will be doing all I can to help the club continue to progress this year.”
Cotter said: “Since taking up this role I believe we have moved forward as a group by working to execute the simple things well. Developing skillsets and mindsets has been important to help perform on the international stage.
“The focus has been to respect tradition but also perform in the present, be adaptive and innovative. I have been really pleased with the progress that has been made in all areas.
“The players, and my management group, have enjoyed working hard to give the fans and Scottish Rugby results and performances to be proud of.
“I intend to remain focused on the upcoming Autumn Internationals and remaining Scotland matches in 2017.”
The SRU said it has now begun a recruitment process to find a replacement for Townsend at Glasgow Warriors for the 2017/18 season.
Mark Dodson, the Scottish Rugby chief executive, said: “Gregor’s abilities as a head coach are evident and he is increasingly becoming one of the most sought after coaches in the game. I have been charged with developing Scottish coaches and believe the time is now right to secure Gregor’s talents, as we build towards the 2019 Rugby World Cup in Japan.
“Vern Cotter has made a considerable impact on the development of the Scotland national side during his tenure. His rugby philosophy of setting high standards with a big emphasis on skill development has had a positive effect throughout Scottish rugby.
“He has helped instil self-belief, determination and put a lot of pride into the jersey for players and fans alike and for this, both myself, and Scottish Rugby would like to thank him for this significant contribution to the game in Scotland.
“We will continue to work with Vern in the season ahead as we are both determined to have a successful autumn test series and 2017 RBS 6 Nations.
“He has worked to bring out the best in our Scottish players and secured some notable victories along the way. No-one will forget our Rugby World Cup 2015 journey as Vern led the team to the quarter-finals and so nearly a semi-final.
“However the Board and I now believe the time is right to take the development of the national team forward with Gregor.
“Gregor’s playing record as a Scotland internationalist and British Lion showed he is a man of vision and great professionalism, which he has since successfully transferred into his coaching roles, firstly with Scotland and subsequently Glasgow Warriors.
“It is these factors which I feel will enable Scotland to continue its forward momentum, building on the strong foundations laid by Vern, with the current group of players and future Scotland internationals.”