先上一张福利!

Linux-Cheat-Sheet
Linux-Cheat-Sheet

系统环境

CentOS 7 x64

添加源

注意:如果失败,请在浏览器输入相应网址查看最新rpm
注意:一定要选择对应的系统版本的源

添加官方源(国外VPS)

EPEL 源

sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm

REMI 源

sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

国内源(国内主机)

中科大镜像源 高质量源

sudo rpm -Uvh http://mirrors.ustc.edu.cn/centos/7/extras/x86_64/Packages/epel-release-7-6.noarch.rpm

浙大源 也非常不错

sudo rpm -Uvh http://mirrors.zju.edu.cn/epel/7/x86_64/e/epel-release-7-8.noarch.rpm

上海交大源,更新比较快

sudo rpm -Uvh http://ftp.sjtu.edu.cn/fedora/epel/7/x86_64/e/epel-release-7-8.noarch.rpm

东北的活雷锋东软的源

sudo rpm -Uvh http://mirrors.neusoft.edu.cn/epel/7/x86_64/e/epel-release-7-8.noarch.rpm

注意:如果失败,请在浏览器输入相应网址查看最新rpm

检查源是否已添加

sudo yum repolist

运行命令生成缓存

sudo yum clean all && yum makecache

选择速度最快的yum源

sudo yum install yum-plugin-fastestmirror

常用命令

普通用户添加 SSH 登录

修改 SSH 端口

sudo vim /etc/ssh/sshd_config

将 Port 后面的数字改为自定义端口号:

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 22

修改 SELinux

sudo yum install policycoreutils-python
sudo semanage port -a -t ssh_port_t -p tcp 22
sudo semanage port -l | grep ssh

添加 SSH 公钥

将 root 用户的 ssh 公钥复制到普通用户根目录(若无公钥则新建):

sudo -s
cp /root/.ssh /home/username/

.ssh 权限(700):

drwx------ 2 username username 4096 Dec 20 07:51 .ssh/

authorized_keys 权限(700):

-rwx------ 1 username username  748 Dec 20 07:51 authorized_keys*

添加防火墙端口

sudo firewall-cmd --permanent --zone=public --add-port=22/tcp
sudo firewall-cmd --reload

重启 sshd 服务

sudo systemctl restart sshd

新建后台窗口

screen -S shadowsocks  #新建后台窗口 shadowsocks
screen -r shadowsocks  #查看后台窗口 shadowsocks

检查端口被哪个进程占用

sudo yum install net-tools
netstat -lnp|grep 26000
lsof -i

防火墙

iptables(Centos6)

sudo vim /etc/sysconfig/iptables

then add these lines, and save quit:

-A INPUT -p tcp -m tcp --dport 29900 -j ACCEPT
-A INPUT -p udp -m udp --dport 29900 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -p udp -m udp --dport 443 -j ACCEPT

finally:

sudo service iptables restart
sudo iptables-save

FirewallD(Centos7)

sudo systemctl enable firewalld #设置开机启动
sudo systemctl start firewalld #开启
sudo systemctl stop firewalld #关闭
sudo systemctl status firewalld #状态
sudo systemctl restart firewalld #重启
sudo systemctl unmask firewalld #unmask

开放端口:

sudo firewall-cmd --state #显示状态
sudo firewall-cmd --zone=public --list-ports #查看打开的端口
sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp #添加端口8080
sudo firewall-cmd --reload #更新规则使修改生效

修改主机名hostname

sudo hostnamectl status #查看主机名信息
sudo hostnamectl --static set-hostname <host-name>  #修改静态主机名

清空文件内容

echo "" > ssserver.log

安装新内核

最新内核地址

cat /etc/redhat-release #查看系统版本
sudo rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
sudo rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm #安装RPM
sudo vim /etc/yum.repos.d/elrepo.repo
sudo yum update -y
sudo yum --enablerepo=elrepo-kernel install  kernel-ml-devel kernel-ml -y #安装最新内核
sudo cat /boot/grub2/grub.cfg |grep menuentry #查看已安装内核选项
sudo grub2-set-default "CentOS Linux (4.9.2-1.el7.elrepo.x86_64) 7 (Core)" #配置默认内核为已安装的内核的最新内核
sudo grub2-editenv list #查看是否配置成功
sudo reboot #重启后即可
uname -r #查看内核版本

优化网络,开启 bbr

sudo vim /etc/sysctl.conf

添加以下内容

net.ipv6.conf.all.accept_ra = 2

fs.file-max = 51200
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_fastopen = 3

# increase TCP max buffer size settable using setsockopt()
net.core.rmem_max = 67108864
net.core.wmem_max = 67108864
# increase Linux autotuning TCP buffer limit
net.ipv4.tcp_rmem = 4096 87380 67108864
net.ipv4.tcp_wmem = 4096 65536 67108864
# increase the length of the processor input queue
net.core.netdev_max_backlog = 250000
# recommended for hosts with jumbo frames enabled
net.ipv4.tcp_mtu_probing=1

net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
#net.ipv4.tcp_congestion_control=hybla

保存生效:

sysctl -p