The goal of this paper was to study the viscosity of liquids and gases in nanochannels. The classical molecular dynamics method is used to simulate the viscosity of liquids in nanochannels. However, the transport coefficients are determined by the special fluctuation-dissipation theorems constructed by the authors on the basis of nonequilibrium statistical mechanics. These theorems generalize the well-known Green-Kubo formulas. The dependences of the viscosity coefficient of argon and benzene in channels made of copper and aluminum with square and circular cross-sections on the size of the channel are studied. The fluid viscosity in the channel can be either greater or less than its viscosity in the bulk. It is shown that the effective viscosity of the fluid can be controlled by changing the material of the channel walls. The decisive role in this effect played the depth of the well of the interaction potential of the wall atoms; with its increase, the effective viscosity of the liquid increases. To simulate the transport coefficients of rarefied gases in nanochannels, a method of stochastic molecular modeling has been developed. The walls are assumed to be solid, and the interaction of fluid molecules with them is described by specular, diffuse, or specular-diffuse reflection laws. A strong anisotropy of the viscosity of the gases in nanochannels, even with rather large sizes, has been established. The viscosity coefficients in different directions can differ by more than ten times. In this case the determining role plays the interaction of the gas molecules with the walls of the channel again.