Linux一些常用操作

时间:2023-03-10 06:51:58
Linux一些常用操作

1、linux swap分区

  可采用文件的方式
    dd if=/dev/zero of=/var/swap bs=1024 count=2048000
    mkswap /var/swap
    swapon /var/swap
    查看 free
    记录到/etc/fstab
    /var/swap swap swap defaults 0 0
    关闭swap分区: swapoff /var/swap

2、samba挂载目录并共享于多台机器 

  比如挂载并共享 /opt/crawler目录
  (1)vi /etc/samba/smb.conf
  末尾添加
  [weibo]
    comment = weiboresult
    path = /crawler/weibo
    public = yes
    writable = yes
    printable = no
    guest OK = yes
    create mask = 777
    directory mask = 777
  (2)service smb restart
    smbpasswd -a
    输入:123456(密码)
  (3)其他机器创建目录并设置文件共享:
    mkdir -p /crawler/weibo
    mount -t cifs //10.0.5.46/weibo /crawler/weibo -o username=root,password=123456
    mount -t cifs //192.168.8.17/weibo /crawler/weibo -o username=root,password=123456
    mount -t cifs //10.0.5.66/Images /crawler/weibo -o username=root,password=123456
    mount -t cifs //192.168.1.103/weibo /crawler/weibo -o username=root,password=123456

3、安装nload(可查看机器带宽使用情况)  

  wget http://www.roland-riegel.de/nload/nload-0.7.2.tar.gz
  tar -zxvf nload-0.7.2.tar.gz
  cd nload-0.7.2
  ./configure
  make && make install
  1、-u设置屏显网卡带宽单位,子参数分别表示:b: Bit/s, k: kBit/s, m: MBit/s etc. B: Byte/s, K: kByte/s, M: MByte/s etc
    nload -u m
  2、 -t 设置屏显刷新时间,单位是毫秒,默认值是500
    nload -t 80

需要安装gcc c++:yum install gcc-c++
可能出现的错误:
configure: error: ncurses library or development files not found. ncurses is required for nload.
解决:yum install -y ncurses-devel

4、查看Linux版本系统信息方法汇总

  getconf LONG_BIT
  lsb_release -a
  cat /proc/cpuinfo | cat /etc/redhat-release
  cat /etc/issue
  cat /proc/cpuinfo| grep "processor"| wc -l

5、主机间进行信任

  (1)方式一

  缺省情况下,SSH将使用口令方式进行验证,不需要对系统进行任何配置,就可以使用账号和口令登录到远程服务器,也可以使用基于密钥认证的方式,下面是具体的配置步骤:( 两台服务器IP分别为192.168.1.1和192.168.1.2)
  1、创建密钥
    #ssh-keygen -d //加上参数 d是创建一个基于ssh2的密钥对
    #Generating public/private rsa key pair.
    #Enter file in which to save the key (/root/.ssh/id_rsa): [回车]
    #Created directory '/root/.ssh'.
    #Enter passphrase (empty for no passphrase): [回车]
    #Enter same passphrase again: [回车]
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    01:90:08:9e:7d:42:69:01:96:52:d7:ca:9f:53:43:df root@192.168.1.1
  2、发布共钥到服务器上
    将id_dsa.pub 发布到服务器上,更名为authorized_keys
    #scp /root/.ssh/id_rsa.pub root@192.168.1.2:/root/.ssh/authorized_keys
    root@192.168.20.150's password: [输入密码]
  3、修改权限所有者
    # chmod 644 /root/.ssh/authorized_keys

  (2)方式二(改进)
  在主信任机上:
    1、/usr/bin/ssh-keygen -t rsa -N ""
  敲两回车
    2、cat /root/.ssh/id_rsa.pub | ssh 192.168.80.128 'cat >> .ssh/authorized_keys'
    cat /root/.ssh/id_rsa.pub | ssh 192.168.80.129 'cat >> .ssh/authorized_keys'
    cat /root/.ssh/id_rsa.pub | ssh 192.168.80.130 'cat >> .ssh/authorized_keys'
    输入密码

6、查看机器上次重启

 last reboot

 who -b

 uptime

7、查看机器出口IP  

  curl http://ip.3322.org
  或者: curl http://1212.ip138.com/ic.asp

8、查看、统计网络连接情况

  netstat -na|grep ESTABLISHED|wc -l 连接数

  服务器的TCP状态(连接状态数量统计):
  netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

9、查看端口占用情况

  netstat -apn | grep 17786

  注:17786为端口号

10、查看进程的状态(资源消耗等)

  cat /proc/5741/status

  其中5741为进程id

11、linux格式化windows上编写的脚本(.sh文件)

  dos2unix *.sh

  相应的也有unix2dos(linux转windows)、unix2mac(linux转mac)、mac2unix命令。

12、centos更换yum源

  先进行备份:mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  1) 使用163 yum源:
    CentOS7
      wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
    CentOS6
      wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
    CentOS5
      wget http://mirrors.163.com/.help/CentOS5-Base-163.repo
    运行yum makecache生成缓存:yum makecache
    yum -y update
  2) 使用aliyun yum源:
  (1) 备份
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  (2) 下载新的CentOS-Base.repo 到/etc/yum.repos.d/
    CentOS 5
      wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
      或者
      curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
    CentOS 6
      wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
      或者
      curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
    CentOS 7
      wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
      或者
      curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  (3)之后运行yum makecache生成缓存

13、Linux查看僵尸进程

  ps -A -ostat,ppid,pid,cmd | grep -e '^[Zz]'
   命令选项说明:
    -A 参数列出所有进程
    -o 自定义输出字段 我们设定显示字段为 stat(状态), ppid(进程父id), pid(进程id),cmd(命令)这四个参数
    因为状态为 z或者Z的进程为僵尸进程,所以我们使用grep抓取stat状态为zZ进程。

14、安装校时服务 

  yum -y install ntp
  service ntp start
  ntpdate asia.pool.ntp.org
  chkconfig ntpd on