3 min read

从零到壹-Centos7.2

查看系统版本

cat /etc/os-release

查看当前IP

sudo yum install curl # 安装curl(已安跳过)
curl ip.cn

修改计算机名

hostnamectl status
hostnamectl set-hostname --static <xx计算机名xx>
退出登录生效

新建用户

sudo useradd -d /home/<要创建的用户主目录> -m <要创建的用户名> -s /bin/bash

netstat命令

安装:sudo yum install net-tools
使用:sudo netstat -lntp

htop

安装:
    sudo yum install epel-release -y
    sudo yum install htop -y
使用:htop

redis

安装:sudo yum install redis -y
使用:
    sudo systemctl status|start|restart|stop|enable|disable redis #状态|#启动|#重启|#停止|#开机自启|#禁止开机自启
    redis-cli -n <指定数据库> #连接到指定数据库

mariadb

安装:sudo yum install mariadb-server mariadb-client
使用:
    sudo systemctl status|start|restart|stop|enable|disable mariadb #状态|#启动|#重启|#停止|#开机自启|#禁止开机自启

mysql

安装:
    sudo yum install wget -y
    sudo wget -i http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
    sudo yum install mysql-community-server -y
    sudo systemctl status|start|restart|stop|enable|disable mysql.service #状态|#启动|#重启|#停止|#开机自启|#禁止开机自启

时间

date #查看当前时间
timedatectl #查看所有时间(GMT、UTC、CST、DST)

同步时间(ntp)

安装:sudo yum install ntp
使用:
    sudo ntpdate -u cn.pool.ntp.org #同步时间
    sudo ntpq -p #查看同步情况

网络配置

sudo nmcli d #查看网络状态
sudo nmtui #网络管理器UI界面
sudo systemctl restart network #重启网络服务

ip

ip ad #查看ip信息(ifconfig)

SELinux 服务

sestatus #查看状态
禁用:
    sudo vi /etc/selinux/config
    # SELINUX=enforcing
    SELINUX=disabled

SSH配置

sudo vim /etc/ssh/sshd_config
#删除
Port 22
#增加
Port <要改的端口>

UseDNS no
# 禁用root登录

# PermitRootLogin yes
PermitRootLogin no
# PasswordAuthentication yes
PasswordAuthentication no

防ping设置

# 临时生效,重启失效
# 禁Ping
echo 1 | sudo tee /proc/sys/net/ipv4/icmp_echo_ignore_all
# 开启ping
echo 0 | sudo tee /proc/sys/net/ipv4/icmp_echo_ignore_all

# 永久生效
# 修改配置文件/etc/sysctl.conf
vim /etc/sysctl.conf
net.ipv4.icmp_echo_ignore_all=1

killall

安装:yum install psmisc
使用:killall python
参考:https://my.oschina.net/zhangxu0512/blog/383297

防火墙配置

安装:sudo yum install firewall iptables
使用:sudo systemctl status/start/stop firewalld.service
安装:sudo yum install iptables-services
使用:sudo systemctl status/start/stop iptables

# iptables 规则 (/etc/sysconfig/iptables)
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80  -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

定时任务

crontab -e
# *  * * * *  command
# 分 时 日 月 周  命令

# 第20分钟同步时间
*/20 * * * * /sbin/ntpdate pool.ntp.org > /dev/null 2>&1

# 第月1号更新更新
* * 1 * * /usr/local/python/bin/certbot renew --renew-hook "/etc/init.d/nginx reload"

# 第天0点重启shadowsocks服务
0 0 * * * /usr/bin/supervisorctl restart shadowsocks

文件传输

scp [-P port] <remote_username>@<remote_ip>:<remote_folder> <local_folder>

例:
    scp -P 60222 root@helibin.com:/tmp/test.txt ~/tmp

删除文件指定行

sed -e '/abc/d'  a.txt   // 删除a.txt中含"abc"的行,但不改变a.txt文件本身,操作之后的结果在终端显示
 
sed -e '/abc/d'  a.txt  > a.log   // 删除a.txt中含"abc"的行,将操作之后的结果保存到a.log
 
sed '/abc/d;/efg/d' a.txt > a.log    // 删除含字符串"abc"或“efg"的行,将结果保存到a.log

例:
sed -e '/binsten.com/d' ~/.ssh/known_hosts > ~/.ssh/known_hosts

添加用户到组

usermod -aG <分组名> <用户名>

例:usermod -aG runtime root