实用UNIX管理脚本


实用UNIX管理脚本
 
1.Linux下强制踢掉登陆用户
 1)查看机器中登陆的用户
  [root@sunsyk ~]# w
  16:29:02 up 2 days, 2:35, 5 users, load average: 0.03, 0.05, 0.01
  USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
  root pts/1 :0.0 Tue15 2days 1:44 0.04s -bash
  root pts/2 :0.0 Tue15 46:42m 0.05s 0.05s bash
  root pts/3 :0.0 Tue15 2days 0.02s 0.02s bash
  root pts/4 172.20.52.114 14:17 58:48 0.16s 0.03s sqlplus
  root pts/5 172.20.52.114 15:31 0.00s 0.03s 0.00s w
 2)把pts/1踢掉(只有root权限才能去踢掉用户)
  [root@sunsyk ~]# pkill -kill -t pts/1
  [root@sunsyk ~]# pkill -kill -t pts/2
  [root@sunsyk ~]# pkill -kill -t pts/3
  查看是不是踢掉
  [root@sunsyk ~]# w
  16:34:16 up 2 days, 2:40, 2 users, load average: 0.00, 0.05, 0.02
  USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
  root pts/4 172.20.52.114 14:17 1:04m 0.16s 0.03s sqlplus
  root pts/5 172.20.52.114 15:31 0.00s 0.03s 0.00s w
  root可以踢掉其他用户包括自己
 
2.Linux怎么查看系统的红帽子版本号
 1.name -a 
 2.cat /etc/version
 3.cat /etc/issue
 4.cat /etc/redhat-release
3.Linux查看系统是32位还是64位
 1.getconf LONG_BIT
 2.file /bin/ls
4.linux设置IP,网关,DNS和MAC地址
  1、IP地址、子网掩码:
 编辑 /etc/sysconfig/network-script/ifcfg-eth0
   DEVICE=eth0
   BOOTPROTO=static    ---静态还是动态
   BROADCAST=192.168.1.255
   IPADDR=192.168.1.35    ---IP地址
   NETMASK=255.255.255.0    ---子网掩码
   NETWORK=192.168.1.0    ---网络ID
   ONBOOT=yes
  2、 网关:
 编辑 /etc/sysconfig/network
   NETWORKING=yes
   HOSTNAME=vmlinux    ---主机名
   GATEWAY=192.168.1.2    ---网关
 
  3、 DNS:
 编辑 /etc/resolv.conf
  nameserver 61.147.37.1
  nameserver 61.177.7.1
 
  4、 MAC地址
 编辑网卡的配置文件/etc/sysconfig/network-script/ifcfg-eth0
 添加上:HWADDR=00:AA:BB:CC:DD:EE
 或:
 1)首先必须关闭网卡设备,否则会报告系统忙,无法更改。
   命令是: /sbin/ifconfig eth0 down
 2) 修改MAC地址。
   命令是: /sbin/ifconfig eth0 hw ether 00 AA BB CC DD EE
 3) 重新启用网卡
   命令是: /sbin/ifconfig eth0 up
 
4.2 AIX查看网络
  ifconfig et0 
 ifconfig -a
 netstat
 
 
 
5.Linux修改用户名和密码
 
 1、改用户名
 usermod -l new(应该写你新的用户名) old(写旧的用户名) 
  然后再设置下密码就好比将mysql更换为pserver usermod -l pserver mysql  
  然后在输入passwd for pserver 123456(自己设置的密码)
 
 2、改密码
  你是普通用户的话,修改自己的密码,用:passwd,就可以了,会让你先输入自己的旧密码,再输入两遍新密码。
 你是root的话,用:password username,就可以修改username的密码了,直接输入两遍新密码就可以了,不用输入旧密码。
 
 3、查看用户
 cat /etc/passwd
    第三项大于等于500的就是创建的用户
6.linux修改默认语言  
   编辑/etc/sysconfig/i18n这个文件,原内容如下:
   LANG="zh_CN.UTF-8"
   SYSFONT="latarcyrheb-sun16"
   SUPPORTED="zh_CN.UTF-8:zh_CN:zh"
   修改为:
   LANG="en_US.UTF-8"
   SUPPORTED="en_US.UTF-8:en_US:en"
   SYSFONT="latarcyrheb-sun16"
重新启动即可!
 
7.解决中文版乱码问题,中文显示乱码具体解决方法是:
  1,修改远程linux机器的配置
 vim /etc/sysconfig/i18n
 把LANG改成支持UTF-8的字符集
 如:LANG="zh_CN.UTF-8"
 或者是 LANG="en_US.UTF-8"
  2,然后再改Secure CRT的设置,选项->会话选项->外观->字符编码->uft-8
  3,退出,再重新登录。发现utf8的文件都能正确读了,utf8的文件名也能正确显
 SecureCRT全屏后,SecureCRT后如何显示菜单?
 Alt+Enter是转换全屏与否的快捷键,或者输入Alt+V弹出菜单,去掉full screen
 在Red hat下面/etc/sysconfig/i18n的每一行都是什么意思?
 LANG="zh_CN.GB18030"
 SUPPORTED="zh_CN.GB18030:zh_CN:zh:en_US.UTF-8:en_US:en"
 SYSFONT="latarcyrheb-sun16"
 /etc/sysconfig/i18n 这里存放的是系统的区域语言设置, 
 i18n是 国际化internationalization的缩写 i和n之间正好18个字母
 第一行 表明你当前系统的语言环境变量设置 ,这里是 zh_CN.GB18030
 第二行 表明系统预置了那些语言支持 ,不在项目中的语言不能正常显示
 第三行 定义控制台终端字体,你文本登录的时候显示的字体就是这个 latarcyrheb-sun16
8.增加修改用户名和组
 Root在根目录下:执行
  groupadd dba 增加组bda
  useradd –个dba oradb 增加用户oradb
  passwd oradb 改变用户oradb的密码
  输入两次密码
  
  usermod -G dba oradb
  mkdir /u01 ##mkdir -p /u01/nihao1/nihao2/nihao3  会一层一层地递归创建目录
  chown -R oradb:dba /u01 改变目录/u01的拥有者和组 #-R递归修改子目录
  chmod -R 775 /u01 递归(-R)改变/u01的读写属性
 
9.Linux修改用户组
 usermod -g group loginname 
  强行设置某个用户所在组
 usermod -G groups loginname
  把某个用户改为 group(s)
 usermod -a -G groups loginname
  把用户添加进入某个组(s)
存储
san lun
# lsvg 
rootvg
datavg
backupvg
47datavg01
47datavg02
47datavg03
# lsvg -p 47datavg01
47datavg01:
PV_NAME           PV STATE          TOTAL PPs   FREE PPs    FREE DISTRIBUTION
hdisk43           active            3999        2           00..00..00..00..02
# lsvg -l 47datavg01
47datavg01:
LV NAME             TYPE       LPs     PPs     PVs  LV STATE      MOUNT POINT
u04                 jfs2       3996    3996    1    open/syncd    /u04
loglv02             jfs2log    1       1       1    open/syncd    N/A
 
10.添加硬盘
 
    AIX查看列表:lsdev -Cc disk
     AIX查看输出:prtconf
    
 
 fdisk  /dev/sdb     (hda/hdb)
 mkfs.ext3 -b 4096 /dev/hdb1
 mount /dev/sdb1 /AAAA
 修改/etc/fstab文件来进行自动挂载
 /etc/fstab
 /dev/sdb1   /www    ext3   defaults  0  0
 
 fdisk -l /dev/sdb     (hda/hdb) 查看磁盘情况
     挂载远程磁盘和卸载远程磁盘
 root@dev01 # mount test01:/u02/backup/rman /finrman
 root@dev01 # umount  /finrman
 root@dev01 #
 
11.linux挂载iso文件命令 
 mount -t iso9660 -o loop /media/nihao/dvd.iso /mnt/iso
 
12.linux安装软件包命令
 rpm -qa   libstdc++-* 查看是否安装过libstdc++软件包
 rpm -ivh  libstdc++-*  --force --nodeps 安装名字包含libstdc++软件包 --force强制安装,--nodep检查依赖性
 
13. 防火墙相关
  1) 重启后生效 
 开启: chkconfig iptables on 
 关闭: chkconfig iptables off
  2) 即时生效,重启后失效 
 开启: service iptables start 
 关闭: service iptables stop
 
14.SElinux
 1)SELinux相关的工具
 /usr/bin/setenforce 修改SELinux的实时运行模式
 setenforce 1 设置SELinux 成为enforcing模式
 setenforce 0 设置SELinux 成为permissive模式
 如果要彻底禁用SELinux 需要在/etc/sysconfig/selinux中设置参数selinux=0 ,或者在/etc/grub.conf中添加这个参数
 
 /usr/bin/setstatus -v
 察看系统的状态
 以下是运行输出,请参考
 SELinux status: enabled
 SELinuxfs mount: /selinux
 Current mode: enforcing
 Policy version: 18
  2)不重启关闭selinux的解决办法:
 执行命令:setenforce 0
 在新版本中的Red Hat 和 Fedora 上,修改档案/etc/sysconfig/selinux:
 # This file controls the state of SELinux on the system.
 # SELINUX= can take one of these three values:
 # enforcing - SELinux security policy is enforced.
 # permissive - SELinux prints warnings instead of enforcing.
 # disabled - SELinux is fully disabled.
 SELINUX=enforcing
 # SELINUXTYPE= type of policy in use. Possible values are:
 # targeted - Only targeted network daemons are protected.
 # strict - Full SELinux protection.
 SELINUXTYPE=targeted
 把 SELINUX设定为disable, 下次启动系统后将会停止SElinux。
 Linux核心参数(Kernel Parameter)
 或者可以在核心参数后加上: selinux=0 (停止) 或 selinux=1 (开启)参数
 文件/boot/grub/menu.lst
 title Fedora Core (2.6.18-1.2798.fc6)
   root (hd0,0)
   kernel /vmlinuz-2.6.18-1.2798.fc6 ro root=LABEL=/ rhgb quiet selinux=0
   initrd /initrd-2.6.18-1.2798.fc6.img
 检查SELinux现时况态
  3)要知到你现在是否使用 SELinux:
 [root@ebs01~]# getenforce
 Disabled
 
15.日志删除 当前目录
 find . -mtime +5 -name "o*.out" -exec rm {} \;
 find . -mtime +10 -name "CUX*.PDF" -exec rm {} \;
 find . -mtime +10 -name "CUX*.EXCEL" -exec rm {} \;
 查找/u01/admin_log/out目录下
 1,时间超过5天的,文件名以o开头,以.out结尾的删掉
 2,时间超过10天的,文件名以CUX开头,以.PDF结尾的删掉
 3,时间超过10天的,文件名以CUX开头,以.EXCEL结尾的删掉
  -mtime n;
      File's data was last modified n*24 hours ago.
  -name pattern;
      Base  of  file name (the path with the leading directories removed) matches shell pattern pattern.  The metacharac-
      ters ('*', '?', and '[]') do not match a '.' at the start of the base name.  To ignore a directory  and  the  files
      under it, use -prune; see an example in the description of -path.
  -exec command ;
      Execute  command;  true  if 0 status is returned.  All following arguments to find are taken to be arguments to the
      command until an argument consisting of ';' is encountered.  The string '{}' is replaced by the current  file  name
      being  processed  everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as
      in some versions of find.  Both of these constructions might need to be escaped (with a '\') or quoted  to  protect
      them from expansion by the shell.  The command is executed in the starting directory.
 
 
 4 cd /u01/admin_log/ora/10.1.3/Apache
  [appprod@ap01 Apache ]$ du -ms .
  6398.76 .
  [appprod@ap01 Apache ]$ pwd
  /u01/admin_log/ora/10.1.3/Apache
  [appprod@ap01 Apache ]$ rm access_log*
  [appprod@ap01 Apache ]$ rm error_log*
  [appprod@ap01 Apache ]$
 5 cd $APPLCSF/$APPLLOG
  find . -mtime +30 -name "l*.mgr" -exec rm {} \;
 5.5  
 
 6 cd /u01/admin_log/ora/10.1.3/opmn
  find . -mtime +10 -name "opmn.log*" -exec rm -rf {} \;
  定期检查/u01/admin_log/ora/10.1.3/opmn目录大小,太大有可能是
oacore溢出了,需要清理。
 
 7      输出日志文件:(小心,没有确认是否能删除)
       /u02/EUT/db/tech_st/11.1.0/appsutil/outbound/EUT_bi02
        诊断日志:/backup/yzz01/db/tech_st/11.1.0/admin/CCTEST01_test01/diag/rdbms/cctest01/CCTEST01/incident
  
 
16. 远程图形显示 
    1)客户机(windows XP)xmanager +服务器端(unix or linux)环境变量
 1、在客户机172.16.3.210打开xterm
 2、服务器ip为172.16.0.22,在term中编辑环境变量DISPLAY: export DISPLAY=172.16.3.210:0.0 --这里 172.16.3.210是客户端的机器,
    服务器ip是172.16.0.22
 3、客户机(172.16.3.210) 安装xmanager,并启动Xmanager -passive后等待。
  4、在客户端的xterm里运行 xclock 会有图像支持。
 
  2)在linux端安装vncserver软件包
 1. 查看當前系統信息redhat enter prise linux 6.1 为类似 tiger vnc server的软件包
  [root@ip-7-0-168-192 ~]# cat /etc/issue
  Red Hat Enterprise Linux Server release 6.0 (Santiago)
  Kernel \r on an \m
 2. 查看系統是否有安裝VNC 安裝包
  [root@ip-7-0-168-192 ~]# rpm -qa|grep vnc
  libvncserver-0.9.7-4.el6.i686
  vnc-4.1.2-14.el5_3.1.i386
  tiger-vnc-server-4.1.2-14.el5_3.1.i386 #沒有VNC 安裝包則安裝
 3. 配置VncServer
   编辑vi /etc/sysconfig/vncservers:
  去掉最後兩行的註釋,并改為如下設置
  VNCSERVERS="2:root"
  VNCSERVERARGS[1]="-geometry 1024x768 -depth 16"
  说明:
  可以在这里添加个多用户,使用户能够使用vnc 客户端连接vnc 服务器,如:
  VNCSERVERS="1:root 2:oracle"
  VNCServer 的默认端口是5801,5901 和6001,这里的"1:root 2:oracle"表示
  为系统用户root 开启5801,5901 和6001 端口,1 为桌面号或者终端号,为系
  统用户oracle 开启5802、5902 和 6002 端口,2 为桌面号,依此类推。"1024x768"
  为屏幕分辨率。为了安全起见,不要在这里设置root 用户登录。
  第一次启动还要修改登录密码,
   #vncpasswd
 4. 启动VncServer
  然后使用系统启动脚本启动VncServer:
  #/etc/rc.d/init.d/vncserver start 或 #service vncserver start
  然后使用netstat 查看是否监听了相应端口,每个终端监听3 个端口
 5.停止服务是,查看状态service vncserver stop/status
 6. 关闭具体的vnc窗口命令为 :vncserver   -kill   :1
   启动具体的vnc窗口服务命令为: vncserver  :1
  7、在客户机机器,一般为windows XP,启动vnvviewer  比如:vnc-4_1_2-x86_win32_viewer.exe
18、文件列表
 ls -l -t为按时间排序显示,默认为新的排在前面,可用下面的命令更改升降序:
 ls -lrt 最新的文件排在后面(升序)
 ls -lnt 最新的文件排在前面(降序)
 du -k *|sort -nr  按大小
19、sftp
 进入本地目录,然后sftp 远程用户@IP地址 
 从172.16.0.202的/u01/archive目录拷贝所有名字为1_46*文件到本地机器的 /u02/archivelogbackup目录
 1  登录本地机器:
 2  进入目录: cd /u02/archivelogbackup
 sftp oraprod@172.16.0.202
  输入yes和密码
 sft>lls,ls,lcd,cd,lpwd,pwd 分别是本地命令和远程命令,get 1_46*是获取远程所有1_46打头的文件。
 
20、 杀死某用户进程中包含固定字符串的进程
 
  有时候因为一些情况,需要把 linux 下符合某一项条件的所有进程 kill 掉,又不能用 killall 直接杀掉某一进程名称包含的所有运行中进程(我们可能只需
要杀掉其中的某一类或运行指定参数命令的进程),这个时候我们需要运用 ps, grep, cut 和 kill 一起操作。
 ok,下面给出具体的参考:
 ps -ef|grep LOCAL=NO|grep -v grep|cut -c 9-16|xargs kill -9
 运行这条命令将会杀掉所有含有关键字"LOCAL=NO"的进程,是不是很方便?
 下面将这条命令作一下简单说明:
 管道符"|"用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入。
 "ps -ef" 是linux里查看所有进程的命令。这时检索出的进程将作为下一条命令"grep LOCAL=NO"的输入。
 "grep LOCAL=NO" 的输出结果是,所有含有关键字"LOCAL=NO"的进程。
 "grep -v grep" 是在列出的进程中去除含有关键字"grep"的进程。
 "cut -c 9-15" 是截取输入行的第9个字符到第15个字符,而这正好是进程号PID。
 "xargs kill -9" 中的 xargs 命令是用来把前面命令的输出结果(PID)作为"kill -9"命令的参数,并执行该命令。"kill -9"会强行杀掉指定进程。
 其它类似的情况,只需要修改"grep LOCAL=NO"中的关键字部分就可以了。
 另一种方法,使用awk
 ps x|grep gas|grep -v grep |awk '{print $1}'|xargs kill -9
 
21 解压和压缩命令
 
 gzip   -d   file.tar.gz 
 tar   xvf   file.tar
 [appprod@ap02 appl ]$ pwd
 /u01/PROD/apps/apps_st/appl
  tar -cvf cux20121012.tar cux 
  tar -xvf cux20121012.tar
 
 gzip   -d   file.tar.gz
 
 gzip -dc ap01_backup08201157.tar.gz | tar -xvf -
 tar -cvf - apps | gzip > /backup/ap01_backup`date +%m%d%H%M`.tar.gz
 
22  在当前目录下的所有文件中,查找包含某个字符串的命令
 grep RCVTP  * | more
 
23 FTP启动和停止
 
[root@ITMS sl2]#  service vsftpd status 
vsftpd 已停
[root@ITMS sl2]#  service vsftpd restart
为 vsftpd 启动 vsftpd:[确定]
[root@ITMS sl2]#  service vsftpd status
 
24 让命令后台执行,终端可以断开
     nohup command &
 

相关内容

    暂无相关文章