Linux 日常使用指令大全

  1. 001、日常维护常用查询命令  
  2. #top    显示系统进程  
  3. #clear  清理屏幕信息  
  4. #cat /etc/redhat-release  查看系统版本信息  
  5. #cat /proc/version  查看系统内核版本等信息  
  6. #uname -a  查看系统内核等信息  
  7. #cat /proc/cpuinfo  查看CPU信息  
  8. #cat /proc/meminfo  查看内存信息  
  9. #cat /proc/swaps  查看交换空间信息  
  10. #getconf LONG_BIT   查看CPU是32位还是64位  
  11. #dmidecode|more 如何查看BIOS信息等。  
  12. #grep MemTotal /proc/meminfo    查看物理内存信息  
  13. #grep SwapTotal /proc/meminfo   查看交换空间的大小  
  14.    
  15. #cat /etc/passwd 查看账户信息  
  16. #cat /etc/shadow 查看密码信息  
  17. #cat /etc/group  查看都有哪些组,每个组包含哪些用户,或#vigr  
  18. #groups username  查看用户属于哪一个组  
  19.    
  20. #ps -aux 显示终端上的所有进程,包括其他用户的进程。  
  21. #ps -x 显示没有控制终端的进程。  
  22. #ps -e 显示所有进程。  
  23. #ps -r 只显示正在运行的进程。  
  24.    
  25. free命令可以查看内存使用率  
  26. top -n1命令中有CPU使用率信息  
  27.    
  28. 002、修改启动界面为文本模式(命令模式)  
  29. 3为文本模式,5为图形模式。  
  30. #vi /etc/inittab  
  31. 修改id:5:initdefault:为id:3:initdefault:  
  32.    
  33. 003、修改远程管理时命令行下中文乱码问题  
  34. Red Hat Enterprise Linux默认的语言编码是“zh_CN.UTF-8”,在X-Window桌面环境下能正常显示汉字,但如果通过SSH或Telnet等方式进行远程管理时,就会发现所有的汉字变成乱码了。  
  35. #vi /etc/sysconfig/i18n  
  36. 将LANG="zh_CN.UTF-8"改为LANG="zh_CN.GB18030",重新登录后即可正常显示汉字。  
  37.    
  38. 系统默认英文语言:  
  39. 编辑/etc/sysconfig/i18n  
  40. LANG="en_US.UTF-8"  
  41. SUPPORTED="en_US.UTF-8:en_US:en"  
  42. SYSFONT="latarcyrheb-sun16"  
  43.    
  44. 系统默认中文语言:  
  45. LANG="zh_CN.UTF-8"  
  46. SUPPORTED="zh_CN.UTF-8:zh_CN:zh"  
  47. SYSFONT="latarcyrheb-sun16"  
  48.    
  49. 系统默认英文语言,同时支持中文语言:  
  50. LANG="en_US"  
  51. LANGUAGE="en_US"  
  52. SUPPORTED="zh_CN.GB18030:zh_CN:zh:en_US.UTF-8:en_US:en"  
  53. SYSFONT="lat0-sun16"  
  54. SYSFONTACM="8859-15"  
  55.    
  56. 004、配置网卡相关信息  
  57. #cd /etc/sysconfig/network-scripts  
  58. #ls  
  59. #vi ifcfg-eth0  
  60. DEVICE=eth0  
  61. BOOTPROTO=static  
  62. BROADCAST=192.168.0.255  
  63. HWADDR=00:17:31:7F:6B:A7  
  64. IPADDR=192.168.0.68  
  65. NETMASK=255.255.255.0  
  66. NETWORK=192.168.0.0  
  67. ONBOOT=yes  
  68. TYPE=Ethernet  
  69. :wq退出。  
  70. #service network restart  重新启动网络  
  71.    
  72. 005、更改主机名  
  73. #vi /etc/sysconfig/network  
  74. 将hostname改名,然后重启即可。  
  75.    
  76. 006、考虑到安全问题,只允许普通账户直接远程登录,不允许root直接远程登录  
  77. 配置sshd  
  78. 启动sshd服务:/etc/init.d/sshd start  
  79. 编辑/etc/ssh/sshd_config文件,找到语句PermitRootLogin yes,将yes修改为no。  
  80. 重新启动sshd服务:/etc/init.d/sshd restart  
  81.    
  82. 007、关闭不需要的系统服务  
  83. 为了安装的系统执行效能更佳,建议关闭一些没有必要的系统服务,例如cups、sendmail、isdn、smartd、iptables、pcmcia...等。  
  84. #chkconfig cups off  
  85. #chkconfig sendmail off  
  86. #chkconfig isdn off  
  87. #chkconfig smartd off  
  88. #chkconfig iptables off  
  89. #chkconfig pcmcia off  
  90.    
  91. 008、修改DNS  
  92. 标准格式:nameserver  DNS地址  
  93. #vi /etc/resolv.conf  
  94. nameserver 192.168.0.36  
  95. nameserver 202.106.0.20  
  96. :wq  #保存退出。  
  97. #service network restart  #重新启动网络  
  98.    
  99. 009、查看系统磁盘的分区情况以及文件系统被挂载的位置  
  100. #df –h  
  101. Filesystem            Size  Used Avail Use% Mounted on  
  102. /dev/hda3              36G  4.1G   30G  12% /  
  103. /dev/hda1             190M  8.4M  172M   5% /boot  
  104. none                  248M     0  248M   0% /dev/shm  
  105. #df -h /home 查看home所在分区的磁盘使用情况  
  106.    
  107. 010、查看目录及该目录下文件的大小,或查看单个文件的大小  
  108. #du -h 目录或文件  
  109. [root@eric68 /]# du -h /tmp  
  110. 12K     /tmp/.font-unix  
  111. 8.0K    /tmp/.ICE-unix  
  112. 12K     /tmp/ssh-wDIORJg370  
  113. 36K     /tmp/test  
  114. 164K    /tmp  
  115. [root@eric68 tmp]# du -h 1.txt  
  116. 8.0K    1.txt  
  117.    
  118. du -sh 目录或文件  //查看目录或文件的总大小  
  119. [root@eric68 /]# du -sh /tmp  
  120. 164K    /tmp  
  121. [root@eric68 tmp]# du -sh 1.txt  
  122. 8.0K    1.txt  
  123.    
  124. 011、查看目录权限:ls –ld 目录名  
  125. [root@DB65 tmp]# ls -ld perl  
  126. drwxr-xr-x 2 root root 4096 Mar 26 12:39 perl  
  127. 查看目录下的文件的权限:ls –lr 目录名  
  128. [root@DB65 tmp]# ls -lr perl  
  129. total 24  
  130. -rwxr-xr-x 1 root root 24 Mar 26 12:39 test2.pl  
  131. -rw-r--r-- 1 root root 65 Mar 26 12:38 test1.pl  
  132. -rw-r--r-- 1 root root 44 Mar 26 12:38 helloworld.pl  
  133.    
  134. 改变目录的权限:chmod 相应权限 目录名/  
  135. 改变目录及其子目录和文件权限:chmod –R 相应权限 目录名/  
  136.    
  137. 012、获取当前所有登陆者信息  
  138. [root@db80 /]# users  
  139. root  
  140. [root@db80 /]# who  
  141. root     pts/1        Mar 20 11:42 (192.168.0.66)  
  142. [root@db80 /]# w  
  143.  15:34:43 up  3:57,  1 user,  load average: 0.00, 0.00, 0.00  
  144. USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT  
  145. root     pts/1    192.168.0.66     11:42    0.00s  0.14s  0.00s w  
  146.    
  147. 013、查看系统里有多少个账户?  
  148. # wc /etc/passwd -l  
  149. 36 /etc/passwd  
  150.    
  151. 014.启动和停止apache服务  
  152. #/etc/init.d/httpd start    //启动httpd服务  
  153. #/etc/init.d/httpd stop    //关闭httpd服务  
  154. #/etc/init.d/httpd restart  //重启httpd服务  
  155. 自动启动WEB服务:  
  156. 如果需要让httpd服务随系统启动而自动加载,可以执行"ntsysv"命令启动服务配置程序,找到"httpd"服务,在其前面加上星号(*),然后选择"确定"即可。  
  157.    
  158. Apache的主配置文件是httpd.conf默认路径:/etc/httpd/conf/httpd.conf  
  159.    
  160. 015、启动和停止Tomcat服务  
  161. # /etc/rc.d/init.d/tomcat5 start   启动Tomcat服务  
  162. # /etc/rc.d/init.d/tomcat5 stop   停止Tomcat服务  
  163. # /etc/rc.d/init.d/tomcat5 restart  重启omcat服务  
  164.    
  165. 自动启动Tomcat服务:  
  166. 如果需要让Tomcat服务随系统启动而自动加载,可以执行"ntsysv"命令启动服务配置程序,找到"Tomcat5"服务,在其前面加上星号(*),然后选择"确定"即可。  
  167.    
  168. 016、Iptables防火墙  
  169. 关闭防火墙:执行“ntsysv”命令启动服务配置程序,取消“iptables”服务前的“*”号。  
  170. 若开启了防火墙,如何开放TCP协议的80端口:iptables -I INPUT -p tcp --dport 80 -j ACCEPT  
  171.    
  172. 关掉SELINUX:  
  173. #vi /etc/selinux/config  
  174. # This file controls the state of SELinux on the system.  
  175. # SELINUX= can take one of these three values:  
  176. #       enforcing - SELinux security policy is enforced.  
  177. #       permissive - SELinux prints warnings instead of enforcing.  
  178. #       disabled - SELinux is fully disabled.  
  179. SELINUX=Disabled  #这样就把SELINUX服务器关掉了,请重新启动系统;  
  180. # SELINUXTYPE= type of policy in use. Possible values are:  
  181. #       targeted - Only targeted network daemons are protected.  
  182. #       strict - Full SELinux protection.  
  183. SELINUXTYPE=targeted  
  184.    
  185. 017、禁止、响应ping  
  186. # echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all 禁止ping,忽略ICMP包。  
  187. # echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all 响应ping  
  188. 以上两个设置在重启后失效,若添加到/etc/rc.local里,重启后仍然有效。  
  189.    
  190. 018、history  
  191. 查看历史记录命令:#history  
  192. 注释:查看以前使用过的命令,默认最大历史记录条数为1000(red hat 中),可以在 "/etc/profile"里面修改"HISTSIZE" 参数来修改默认值  
  193.    
  194. 清除历史记录命令:#history –c  
  195. 立即更新history文件命令:#history -w  
  196. History记录原理:当一个用户用bash登录系统,他所输入的命令会放到一个内存catch中,在他登出时会存入~/.bash_history中,用histroy  -c  命令可以清空这一命令缓存,也就是说,这一用户不想让人知道他做了些什么的话,只要在退出前执行history  -c就行了。  
  197.    
  198. 查看某个用户的history记录:  
  199. #history –w  
  200. #history或#more .bash_history  
  201.    
  202. 如何自动备份某个用户的history记录?  
  203. 1.建立ericyi用户的history每日备份脚本  
  204. #cd tmp  
  205. #mkdir ericyi_history_backup  
  206. #chown root ericyi_history_backup/  
  207. #vi ericyihistorybackupdaily.sh  
  208. -----------------------------------------------------------------  
  209. #!/bin/bash                                                                                                                                                                                                                                                             
  210. day=`date +%Y-%m-%d`                                                                                                                 
  211. basedir="/tmp/ericyi_history_backup "                                                                                                  
  212. cd /home/ericyi                                                                                                                      
  213. history -w                                                                                                                            
  214. cp .bash_history "$basedir"/history."$day" > /dev/null                                                                                    
  215. -----------------------------------------------------------------  
  216.    
  217. 如何记录用户登陆后的命令操作和系统输出?  
  218. 在/etc/profile中添加如下内容:  
  219. --------------------------------------------------------------------------------  
  220. # log user input except test  
  221. if [$USER != "test" ]; then  
  222.         SSHCLIENTIP=`echo $SSH_CLIENT | awk '{ print $1; }'`  
  223.         USERLOGFILENAME=/tmp/${USER}_`date +"%Y%m%d%H%M"`_from_${SSHCLIENTIP}.log  
  224.         echo "SSH CLIENT INFO: "${SSH_CLIENT} >${USERLOGFILENAME}  
  225.         exec /usr/bin/script -a -f -q  ${USERLOGFILENAME}  
  226. fi  
  227. --------------------------------------------------------------------------------  
  228.    
  229. 019、linux下定期备份数据  
  230. # chmod 700 backupdaily.sh   //修改权限,ROOT执行  
  231. # vi /etc/crontab   //任务计划  
  232. #加入下面内容  
  233. 10 1 * * * root /etc/root/backupdaily.sh  
  234. 注释:每天1点10分运行脚本  
  235. :wq保存退出。  
  236.    
  237. 020、Linux下定期同步时间?  
  238. #ntpdate 207.46.232.182  
  239. 做个脚本,放在crontab中定期执行即可。