This paper presents a linear model for optimal operation of distributed generations (DGs) including renewable and nonrenewable DGs in the electrical grids. The accurate model of active-reactive losses is formed by two quadratic terms that are linearized by piecewise linear approximation of the quadratic curves. The DGs' technical/economic formulations are also presented based on the linear model. The proposed linear models are combined to implement linear optimal power flow (OPF) in the electrical grid integrated with hybrid diesel/wind/solar/battery. The model is associated with uncertainties and expressed as stochastic mixed integer linear programming. Both the active and reactive powers of the grid and resources are incorporated. The model optimizes the following design variables: sitting and sizing of energy storage systems and DGs, depth of discharge, energy of battery, operation pattern of battery, operation pattern of non-renewables units, and investment/operational costs. The proposed model is tested on 69-bus distribution grid. The simulation results show that the depth of discharge is optimized on 30% to 95%. Active and reactive losses achieved by the proposed model are 72% and 47% more accurate compared to the other existing models. The voltage profile is improved by about 8%. The network losses are reduced by 24%. The peak shaving is performed at hours 18 to 24.