您的位置:首页 > 运维架构 > Linux

linux-常用命令

2016-03-27 13:00 686 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gallanthx/article/details/50991199
  1. 查看系统信息

    查看系统类型及版本

    cat /etc/issue
    lsb_release -a
    ll /etc/*-release
    cat /etc/*-release

    查看内核版本

    uname -r
    uname -a
    cat /proc/version

    查看系统位数(32位还是64位)

    whereis echo
    file /bin/echo

    查看CPU信息(查看是几核)

    cat /proc/cpuinfo | grep processor

    查看内存信息

    free -m

  2. SuSE 中配置默认运行级别
    vi /etc/inittab

    The default runlevel is defined here

    id:3:initdefault:

    运行级别描述如下:

    runlevel 0 is System halt (Do not use this for initdefault!)

    runlevel 1 is Single user mode

    runlevel 2 is Local multiuser without remote network (e.g. NFS)

    runlevel 3 is Full multiuser with network

    runlevel 4 is Not used

    runlevel 5 is Full multiuser with network and xdm

    runlevel 6 is System reboot (Do not use this for initdefault!)

  3. SuSE 中配置开机自启动
    3.1 系统服务

    查看 samba 服务的自启动情况

    chkconfig -l smb

    将 samba 服务设置为开机自启动

    chkconfig smb on
    或者
    chkconfig smb 35

    禁止 samba 服务的开机自启动

    chkconfig smb off
    3.2 自定义脚本

    在 /etc/rc.d 目录中新建一个脚本

    vi /etc/rc.d/auto_start_sh

    ################ 文件内容 start

    ! /bin/bash

    BEGIN INIT INFO

    Provides: auto_start_sh

    Required-Start:

    Required-Stop:

    Default-Start: 3 5

    Default-Stop: 0 1 2 6

    Short-Description:

    Description:

    END INIT INFO

    case “1”instart)echo“Startingautostartsh”echo“systemstartat”(date “+%Y-%m-%d %H:%M:%S”) >> /tmp/auto_start.log
    ;;
    stop)
    echo “Shutting down auto start sh”
    ;;
    status)
    ;;
    *)
    echo “Usage: $0 {start|stop|status}”
    exit 1
    ;;
    esac

    exit 0

    ################ 文件内容 end

    修改脚本权限

    chmod 755 /etc/rc.d/auto_start_sh

    将脚本添加到系统服务中

    chkconfig -a auto_start_sh

    修改为自启动

    chkconfig auto_start_sh on

  4. SuSE 中配置防火墙
    4.1 SuSEfirewall2

    查看防火墙状态

    SuSEfirewall2 status

    停止防火墙

    SuSEfirewall2 stop

    启动防火墙

    SuSEfirewall2 start

    设置允许访问的ip

    cp /etc/sysconfig/SuSEfirewall2 /etc/sysconfig/SuSEfirewall2.bk
    sed -i ‘s/^([[:space:]]FW_SERVICES_ACCEPT_EXT[[:space:]]=).*$/\1 “192.168.1.1”/g’ /etc/sysconfig/SuSEfirewall2
    4.2 iptables (iptables 不能和 SuSEfirewall2 同时配置, SuSEfirewall2 会覆盖掉 iptables 的配置)

    查看 iptables 配置

    iptables -L -n –line-numbers

    限制某个 ip

    iptables -I INPUT -s 192.168.1.100 -j DROP

    删除某条规则(1 是某条规则的编号)

    iptables -D INPUT 1

  5. SuSE 中配置 samba 服务

    查询是否已安装 samba server

    rpm -qa | grep samba

    创建一个系统用户, 用于后续登录 samba 服务

    useradd -d /home/user1 -s /bin/bash -g users -m user1

    创建一个共享目录, 并将所有者修改为 user1

    mkdir /samba_share
    chown user1:users /samba_share

    添加一个 samba 用户, 并设置访问密码

    smbpasswd -a user1

    配置共享目录

    vi /etc/samba/smb.conf
    [samba_share]
    comment = samba share directory
    path = /samba_share
    valid users = user1
    read only = No
    guest ok = No

    启动 samba 服务

    service smb start # 启动 samba 服务
    service smb stop # 停止 samba 服务
    service smb status # 查询 samba 状态

    在 windows 中访问 samba 共享的文件夹

    \192.168.1.120

    在 windows 中查看访问过的 samba 服务

    net use

    在 windows 中删除访问过的 samba 服务

    net use /delete \192.168.1.120\samba_share

    samba 用户管理

    smbpasswd -a user1 # 增加 samba 用户
    smbpasswd -x user1 # 删除 samba 用户
    pdbedit -L # 查看 samba 用户
    cat /etc/samba/smbpasswd # 查看 samba 用户

    在 linux 中查看 samba 服务共享了哪些目录

    smbclient -L 192.168.1.120 -N

    在 linux 中访问 samba 共享的文件夹

    smbclient //192.168.1.120/samba_share -U user1%password

  6. SuSE 中配置NFS服务

    查询是否已安装 nfs server

    rpm -qa | grep nfs-kernel-server

    安装 nfs server

    rpm -ivh nfs-kernel-server-1.2.3-18.29.1.i586.rpm

    创建共享目录

    mkdir /share
    chown nobody:nogroup /share/

    配置共享目录

    rw : 读写

    async : 内存数据定期写入磁盘

    all_squash 和 anonuid 配合起来指定 nfs 客户端使用时的用户身份

    vi /etc/exports
    /share *(rw,async,all_squash,anonuid=65534,anongid=65534)

    启动 nfs server

    service nfsserver start

    查看共享目录

    showmount -e 192.168.1.120

    客户端挂载共享目录

    soft : 软挂载, nfs server 连不上时返回错 1b024 误而不是无限挂死

    retry : mount 命令挂载 nfs server 时的等待时间, 单位为分钟, 超过此时间则挂载失败

    timeo : nfs client 等待响应的时间, 单位为0.1秒

    mkdir /mnt/nfs
    mount -o soft,retry=1,timeo=300 192.168.1.120:/share /mnt/nfs

    查看已挂载的目录

    mount

    开机自动挂载 nfs 文件系统

    vi /etc/fstab
    192.168.1.120:/share /mnt/nfs nfs defaults 0 0

    卸载 nfs 挂载点

    umount /mnt/nfs/

  7. SuSE 中配置 VNC 服务
    7.1 通过 yast 进行配置(推荐这种方式)
    yast -> Network Services -> Remote Administration (VNC) -> Allow Remote Administration
    7.2 通过命令行手动进行配置(不推荐)

    第一次启动 VNC 服务

    suse:~ # vncserver :1
    You will require a password to access your desktops.
    Password:
    Verify:
    Would you like to enter a view-only password (y/n)? n
    xauth: creating new authority file /root/.Xauthority
    New ‘X’ desktop is suse:1
    Creating default startup script /root/.vnc/xstartup
    Starting applications specified in /root/.vnc/xstartup
    Log file is /root/.vnc/suse:1.log
    suse:~ #

    设置图形界面

    vi /root/.vnc/xstartup
    将最后一行的 twm & 修改为
    startgnome
    DISPLAY=:1 gnome-session

    重启 VNC

    vncserver -kill :1
    vncserver :1

    查看 VNC 服务的进程和端口

    ps -ef | grep vnc
    netstat -tlnp | grep vnc
    7.3 访问 VNC 服务

    直接在浏览器中访问

    http://192.168.2.120:5801/

    使用 VNC Viewer 这个工具进行访问

    http://www.realvnc.com/download/viewer/

  8. 其他命令

    查看进程信息(ssh是需要过滤的字符串)

    ps -ef | grep ssh

    通过进程 id 结束进程

    kill -9 pid

    查看端口占用情况

    netstat -ano | grep 445

    查看 samba 服务监听的端口

    netstat -tlnp | grep smb

    根据端口查进程

    lsof -i :445

    查看系统时间

    date -R

    修改系统日期

    date -s 2015-09-07

    修改系统时间

    date -s 12:01:02

    修改系统日期和时间

    date -s “2015-09-08 10:11:12”

    设置命令行的字符集

    export LANG=zh_CN.UTF-8
    export LANG=zh_CN.GB18030

    删除含有特殊符号的文件

    rm -rf ‘$a123 def’
    或者
    ll -i
    find ./ -inum 155044 -exec rm -rf ‘{}’ \;

    获取脚本文件所在目录

    curr_dir=(cd"(dirname 0)”;pwd)echocurr_dir

    在一个网卡上绑定多个 ip

    ifconfig eth0:1 192.168.1.121 broadcast 255.255.255.255 netmask 255.255.255.0
    ifconfig eth0:2 192.168.1.122 broadcast 255.255.255.255 netmask 255.255.255.0

    删除网卡上绑定的多个 ip

    ip addr del 192.168.1.121/255.255.255.0 dev eth0
    ifconfig eth0:2 down

    diff fileName1 fileName2 # 比较两个文件
    chmod 755 test1.sh # 修改文件权限
    chown user1:users test1.sh # 修改文件所有者和所属组

    cut 命令

    -d’:’ 表示分割符为 :

    -f1 表示选择第一列

    cut -d’:’ -f1 /etc/passwd # 显示所有的用户名

    sed 命令(逐行处理)

    替换文件中的内容 sed ‘s///’ test.txt

    echo “one two three four” | sed ‘s/two/2/’

    先过滤掉以’#’开头被注释掉的行, 再筛选出 key = value 形式的行

    sed ‘/[[:space:]]*#/d’ /etc/sysctl.conf | cut -d’=’ -s -f1-2

    awk 命令(逐行处理)

    -F”:” 表示分割符为 :

    awk -F”:” ‘{print 1}’ /etc/passwd              # 显示所有用户名  
    awk -F”:” ‘{print $3 " = " $1}’ /etc/passwd     # 显示用户 id 和 用户名  
    awk -F”:” ‘3 > 500 {print}’ /etc/passwd # 显示用户 id > 500 的用户信息
    awk -F”:” ‘{ if($3 > 500){print} }’ /etc/passwd # 同上

    读取 key = value 形式的数据

    文件内容

    cat file1.txt
    key1 = value1 1234567 xyz
    key1 = value1-2
    key2 = value2

    读取 key1 对应的 value 值(\x09 表示\t, \x20 表示空格)

    awk ‘{if( match(0,"[\x09\x20]∗key1[\x09\x20]∗=[\x09\x20]∗(.∗[\x09\x20])[\x09\x20]∗”, a) ){ print ““a[1]”“; exit 0;} }’ file1.txt

    字符串 trim 操作(删除左右两边的空格)

    echo ” abc 123 def ” | awk ‘{ gsub(/^[\x09\x20]|[\x09\x20]/,"",0); print ““$0”“; }’

    https://www.ibm.com/developerworks/cn/linux/shell/awk/awk-1/
    https://www.ibm.com/developerworks/cn/linux/shell/awk/awk-2/
    https://www.ibm.com/developerworks/cn/linux/shell/awk/awk-3/

– END

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: