That seems to be a gray area. It is agreed by most that undocumented aliens are not entitled to the second amendment, while it is disputed whether legal non-permanent aliens are (as they are indeed entitled to other rights, and many at least would not consider the right to bear arms as a minor or secondary one, for reasons you're free to wonder about, and to argue the virtue of). It is generally held that resident aliens are covered by the constitution's "the people," and do have that right, even though there are some others that they, as well as non-permanent visitors do not (for example being a card carrying communist, presumably because being a communist is a bar to immigration in the first place).
That's a very cursory account. If you want to go further, take a pickaxe and a headlight. The rabbit hole is deep.