The Soviet space station Salyut 3 used to have a gun (an aircraft autocannon) equipped on it (reference).
The cannon was a modification of NR-23 autocannon used in Tu-22 aircraft.
Russian version of Salyut-3 Wikipedia article claims that this weapon system, named "Schchit-1" (in Russian "Щит-1", translated as "Shield-1"), was implemented as a self-defense measure against potential capture of the station by Space Shuttle orbiter:
The leaders of the Soviet military-industrial complex had concerns that one of the "military" applications of the Shuttles would be inspection and removal of Soviet spacecrafts from orbit.
The following quotes (translated from Russian) are from this webpage, based on an article in "Popular Mechanics" ("Популярная механика") magazine, №10 (12), October 2003.
The OPS [OPS-2 (Orbital Piloted Station-2), aka Almaz-2, aka Salyut-3] was equipped with an aircraft rapid-fire NR-23 cannon (modification of Tu-22 jet bomber tail gun) designed by Nudelman - Rikhter.
Estimated firing range for shooting orbital targets was more than three thousand meters. The gun fired 950 shots per minute. The shell weighing 200 g flew at a speed of 690 m/s.
Below, allegedly, is the photo of the cannon (source):
When shooting in space, the recoil of the cannon was equivalent to a thrust of 218.5 kgf [2.143 kN], therefore the station had to be stabilized, which was easily handled by two main engines with a thrust of 400 kgf [3.9 kN] each and engines of rigid stabilization [sic] [attitude control thrusters, perhaps] with a thrust of 40 kgf [0.39 kN] each.
The cannon was mounted rigidly under the belly [sic] of the OPS [-2]. It could be aimed at desired point with help of the sight by the means of turning the entire station manually or by using remote control in order to [automatically?] follow the target. The firing of the gun was controlled by a "programmatic control equipment" ("PKA" [in Russian "ПКА"]), which calculated the salvo [sic] required to destroy the target with the shell flight duration of one to five seconds.
On 24th of January 1975, when the station ... was being deorbited, the gun fired its first (and ... last) salvo [sic]: the developers needed to know how firing the cannon affects dynamics and vibrational stability of the Almaz [-2 station]. The tests were successful ... and shells were fired [in direction] against the orbital velocity vector [firing was controlled from the ground], and burned in atmosphere ... before the station did.