The trial could also lead to the roll-out of the technology across the other class 158 trains in ScotRail's fleet.
This would cover several rural lines including the West Highlands, Stranraer, Dumfries and the Borders.
ScotRail said it would also help to influence the inclusion of the technology when buying new trains.
The publicly-owned rail operator has partnered with Clarus Networks, which sells access to billionaire Musk's Starlink satellites in the UK.
Business Minister Richard Lochhead said reliable internet connections would make passengers' journeys more enjoyable.
ScotRail strategy and planning director, Scott Prentice, added: "The project addresses the long-standing challenges we've had for train connectivity in the region, and we hope it will lead to a future roll-out of the technology across other rural routes in Scotland."
Musk, the controversial owner of Tesla and currently senior adviser to US President Donald Trump, has been criticised by senior figures in Scotland politics, including First Minister John Swinney.
He has also publicly clashed with Swinney's predecessor Humza Yousaf.