Category: Linux

  • svn 添加路径

    以例子来说明:

    一步步安装phpmyadmin(安装到web根目录里)

    先在web根目录里创建路径 ,用于存放程序

    mkdir phpmyadmin

    这里我是在/root/里下载好phpMyAdmin-3.4.3.2-all-languages.tar.gz

    解压后复制到 /web/phpmyadmin/里面

    tar vxzf phpMyAdmin-3.4.3.2-all-languages.tar.gz
    cp -Rf /root/phpMyAdmin-3.4.3.2-all-languages/* /web/phpmyadmin/

    加入到当前svn,提交此版本到库里

    svn add * --no-ignore --force
    svn ci -m "add path phpmyadmin"

    🙂 EOT

  • linux时区的修改及使用ntpdate同步时间

    时间对linux来说十分重要,很多任务要求时间准确

    时区的修改

    查看当前时区

    # date -R

    通过如下命令,交互式问答来设置时区

    # tzselect

    这时会显示出来选择国家和地区,选好后提示你

    You can make this change permanent for yourself by appending the line
    TZ=’Asia/Shanghai’; export TZ
    to the file ‘.profile’ in your home directory; then log out and log in again.

    想要永久改变就写到你的.profile里,或/etc/profile全局文件中。

    TZ='Asia/Shanghai'; export TZ

    Here is that TZ value again, this time on standard output so that you
    can use the /usr/bin/tzselect command in shell scripts:
    Asia/Shanghai

    或者使所有用户的时区改变

    echo "TZ='Asia/Shanghai'" >> /etc/environment

    对于 centos 6,用如下方法更改时区

    cp /etc/localtime /etc/localtime.old
    ln -nfs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    ls -lh /etc/localtime
    date

    还有一个地方如果能改也要改一下

    vi /etc/sysconfig/clock
    ZONE="Asia/Shanghai"

    对于 centos7,稍微方便一些,原理一样
    查看 timezones

    [root@localhost ~]# timedatectl list-timezones
    [root@localhost ~]# ls -l /etc/localtime 
    lrwxrwxrwx. 1 root root 38 8月   1 2014 /etc/localtime -> ../usr/share/zoneinfo/America/New_York
    [root@localhost ~]# date
    2015年 02月 24日 星期二 22:08:35 EST

    设置 timezone

    [root@localhost ~]# timedatectl set-timezone Asia/Shanghai
    [root@localhost ~]# date 
    2015年 02月 25日 星期三 11:09:14 CST
    [root@localhost ~]# ls -l /etc/localtime 
    lrwxrwxrwx. 1 root root 35 2月  25 11:09 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

    接下来,更新系统时间为Internet时间,权根为root用户。

    先安装ntpdate并与0.cn.pool.ntp.org同步时间:

    apt-get install ntpdate
    ntpdate 0.cn.pool.ntp.org

    系统时钟与硬件时钟同步:

    # hwclock --systohc

    上面命令中,--systohc,简写为 -w 表示SYStem clock to Hardware Clock;
    同理--hctosys,简写为 -s 表示Hardware Clock to SYStem clock,不过我的理解是系统时间更新后把硬件时钟刷新的情况为正统。

    为保证时间的准确性,把时间同步写入到cron里,每小时把当前系统时间与Internet时间同步,每天把系统时间与硬件时间同步。

    * */1 * * * /usr/sbin/ntpdate 0.cn.pool.ntp.org >/dev/null
    0 0 * * * /sbin/hwclock -w >/dev/null

    Linux时钟介绍:

    Linux机器里有两个时钟,一个是硬件时钟(CMOS时钟),一个是内核时钟。硬件时钟是电池驱动的,通过专门的芯片工作。可以通过BIOS设置屏或一些系统命令(如hwclock)进行设置。内核时钟是由内核维护的,启动时从硬件读取时间,之后独立运行。

    RTC(Real Time Clock)

    即Linux实时的时钟驱动,通常它们被嵌在计算机的芯片中,也有一些是在主板上使用Motorola MC146818(或clone)实现的。该硬件设备可映射到/dev/rtc,供root编程访问。

    NTP(Network time protocol)

    NTP为一种网络时间协议,是一种确保网络上的主机调整时间的方法。很多地方都会提供NTP服务器。

    常用 ntp 服务器

    ntpupdate.tencentyun.com

    CentOS 7 更新时间
    1. timedatectl 查看当前同步情况,如果没开,开启

     timedatectl set-ntp 1 

    check 时间
    datetimectl 显示

         NTP enabled: yes
    NTP synchronized: yes
    

    2. 如果是物理机,将时间刷入 hardware

    hwclock –systohc

    check 硬件时间

    hwclock
  • linux设置静态ip及dns

    debian设置静态ip

    #vim /etc/network/interfaces
     auto eth0
     iface eth0 inet static
     address 219.230.144.244
     netmask 255.255.255.192
     gateway 219.230.144.254
     dns-nameservers 8.8.8.8
      重启网络服务
    /etc/init.d/networking restart

    ubuntu 20.04

    cat /etc/netplan/00-installer-config.yaml 
    # This is the network config written by 'subiquity'
    network:
      ethernets:
        enp4s0:
          dhcp4: false
          addresses:
            - 192.168.1.66/24
          gateway4: 192.168.1.1
          nameservers:
              addresses: [192.168.1.1, 223.5.5.5]
      version: 2
    
    netplan apply

    centos设置静态ip

    #vim /etc/sysconfig/network-scripts/ifcfg-eth0  有时会是ifcfg-em1

    DEVICE="eth0"
    BOOTPROTO="static"
    HWADDR="00:A5:D1:EA:23:A4"
    IPV6INIT="yes"
    MTU="1500"
    NM_CONTROLLED="yes"
    ONBOOT="yes"
    TYPE="Ethernet"
    UUID="95fd6b2b-ecfd-4cdf-9833-e3056f22d4e8"
    IPADDR=192.168.8.126
    NETMASK=255.255.255.0
    GATEWAY=192.168.8.1
    • 配置dns解析

    echo "nameserver 211.98.1.28">> /etc/resolv.conf

    弄完后,记得重启网络服务

    /etc/init.d/network restart

    查看网络ip

    ifconfig

    查看网关

    netstat -rn

    查看nameserver

    cat /etc/resolv.conf
  • 连接vps上的ssh

    创建一个权限最低的用户

    useradd -M -s /bin/false username

    创建该用户的密码:

    passwd username
    • windows使用MyenTunnel,输好用户名,密码,ip,端口

    在linux下使用密钥认证可以免输密码,而且参考使用ssh_config,创建并在~/.ssh/config里写入以下内容,参考:http://www.lainme.com/doku.php/blog/2011/01/%E4%BD%BF%E7%94%A8ssh_config

    jpuyy@ubuntu $ vim ~/.ssh/config
     Host vps
     User jpuyy
     Hostname jpuyy.com
     PreferredAuthentications publickey
     IdentityFile ~/.ssh/id_rsavps
     DynamicForward 7070

    这时在shell下使用

    $ssh vps

    更有一步登天之感。

    其他工具

    autossh可以在ssh断掉后重连

    sudo apt-get install autossh

    autossh -M 2000 -N -v  username@hostip

    plink可以直接指定密码

    ssh需要手动输入,因为把密码表现在history中相当恐怖

    sudo apt-get install putty-tools

    plink -N -v username@hostip -pw password

    sshpass也可以指定密码

    Description : Tool for non-interactively performing password authentication with
    : so called “interactive keyboard password authentication” of SSH.
    : Most users should use more secure public key authentication of SSH
    : instead.
    yum install sshpass

    mosh是在网络比较慢的时候使用,尤其是国外线路

    http://mosh.mit.edu/

  • linux几个掌握系统信息的命令

    查看CPU信息

    /proc/cpuinfo

    里面有flag信息,如果有lm,则支持64位

    系统负载命令

    uptime w htop top 都可查看
     ------------23:41:57 up 4:42, 3 users, load average: 0.00, 0.00, 0.00
     ------------当前时间 运行时间 用户数 系统负载 (最近1分钟、5分钟、15分钟系统的负载)

    查看硬盘信息命令

    fdisk -l

    JOBS使用

    # jobs 查看后台运行的进程
    # fg %n 让后台运行的进程n到前台来,再Ctrl+c即可终止该程序.
    # bg %n 让进程n到后台去;

    root@debian:~# nohup ping g.cn &
    [1] 9576
    root@debian:~# nohup: 忽略输入并把输出追加到”nohup.out”

    root@debian:~# jobs
    [1]+ Running nohup ping g.cn &

    接着执行

    root@debian:~# bg %1
    [1]+ nohup ping g.cn &
    root@debian:~# jobs
    [1]+ Running nohup ping g.cn & //变为了running

    Linux Jobs等前后台运行命令详解,停止后台程序:
    Shell支持作用控制,有以下命令:
    1. command& 让进程在后台运行 / nohup command &
    2. jobs 查看后台运行的进程
    3. fg %n 让后台运行的进程n到前台来,再Ctrl+c即可终止该程序.
    4. bg %n 让进程n到后台去;
    PS:”n”为jobs -l查看到的进程编号.

    RX 和 TX

    TX: transmit 传送. RX: receive 接收

    查看系统内核

    uname -r

    查看内核名

    uname -s
    Linux
    

    查看架构

    uname -m
    x86_64

    查看系统的字符集

    echo $LANG $LANGUAGE
    TAIL -F FILENAME

    会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容

    查看内存使用情况

    free -m

    total used free shared buffers cached
    Mem: 7866 6213 1653 0 209 2711
    -/+ buffers/cache: 3292 4574
    Swap: 0 0 0
    真实的空余内存为4574MB

    文件系统使用情况

    df -h (有时可以看到使用率到100%,但是系统会保留5%的空间用于root用户来登陆及其他操作)

    df -T(可查看file system类型)

    df -i 查看已用和未用的inode数量及百分比

    查看模块

    lsmod

    查看UUID:

    方法一:#ls -l /dev/disk/by-uuid/
    方法二:#blkid

    LSOF命令

    先要 apt-get install lsof

    查看tcp端口应用程序#lsof -i tcp

    查看 linux 发行版,尝试以下方法

    cat /etc/redhat-release
    cat /etc/debian_version
    cat /etc/lsb-release
    cat /etc/os-release
    
  • 我的.bashrc

    用了yardvps的tree,里面的.bashrc很不习惯

    改成自已习惯的就好了

    root@localhost:/var/www/web0/addr# cat ~/.bashrc
     # ~/.bashrc: executed by bash(1) for non-login shells.
     # Note: PS1 and umask are already set in /etc/profile. You should not
     # need this unless you want different defaults for root.
     # PS1='${debian_chroot:+($debian_chroot)}h:w$ '
     # umask 022
     # You may uncomment the following lines if you want `ls' to be colorized:
     export LS_OPTIONS='--color=auto'
     eval "`dircolors`"
     alias ls='ls $LS_OPTIONS'
     alias ll='ls $LS_OPTIONS -lhS'
     alias l='ls $LS_OPTIONS -lAh'
     alias vi='vim -n'
     #
     # Some more alias to avoid making mistakes:
     alias rm='rm -i'
     alias cp='cp -i'
     alias mv='mv -i'
     alias grep='grep --color=auto'
     # alias online='last | grep still | grep ppp |sort -k 2,2'
     alias reboot='sync ; sync ;sync ;reboot'

    关于PS1
    export PS1=”\u@\h:\W\$”

    \u表示用户名
    \h主机名
    \W当前目录

    最后记得source ~/.bashrc