Linux笔记

安装CentOS

安装VirtualBox+CentOS

  • 设备->安装增强功能
    打开目录run/media/root/VBoxAddons…,
    终端运行sh VBoxLinuxAdditions.run
    重启后就可以在”应用程序-系统工具-设置-显示”里面设置分辨率了
  • VitualBox 扩容

    1
    2
    3
    cmd
    cd c:\program files\oracle\vitualbox\
    VboxManage.exe modifyhd "name.vdi" --resize NUM_IN_MB (20480 means 20G)
  • 网络配置
    Host-Only 链接到本机 VirtualBox Host-Only Ethernet Adapter(192.168.56.1)
    NAT 本地网络转换到虚拟机内,使得虚拟机与本机具有相同的网络访问能力
    桥接网卡 连接到本地路由网络并分配地址,使得本地路由下的机器虚拟机能够互相访问

  • CentOS 7.5.1804 host-only无法正常动态分配地址
    cd /etc/sysconfig/network-scripts/
    vi ifcfg-enp0s3
    修改其内容如下,然后systemctl restart network.service

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    TYPE="Ethernet"
    BOOTPROTO="dhcp"
    DEFROUTE="yes"
    PEERDNS="yes"
    PEERROUTES="yes"
    IPV4_FAILURE_FATAL="no"
    IPV6INIT="yes"
    IPV6_AUTOCONF="yes"
    IPV6_DEFROUTE="yes"
    IPV6_PEERDNS="yes"
    IPV6_PEERROUTES="yes"
    IPV6_FAILURE_FATAL="no"
    NAME="enp0s3"
    UUID="a57d4898-6803-49dc-8cd9-01035897cc69"
    DEVICE="enp0s3"
    ONBOOT="yes"

Linux常用命令

1
2
3
4
5
6
7
$ du -h #查看本目录下文件夹大小(--max-depth=? 可指定目录层级,默认1)
$ du -hs #查看本目录大小
$ ls -lah #查看本目录下文件大小
$ rename .uc .mp3 *.uc #将目录下所有uc文件更改为mp3
$ ln -s /opt/solr-5.5.3 /opt/solr #创建软链接
$ crontab -e 与 chkconfig --list
$ find . -iname "*.java" -type f -exec gsed -i '1s/^/123\n/' {} \; # 在本目录下所有java文件开头插入新行,内容为123

CentOS环境

缺少wget以及net-tools(无法使用command:ifconfig)

1
2
$ yum -y install net-tools
$ yum -y install wget

修改yum源

centos7

1
2
3
4
5
6
7
8
$ cd /etc/yum.repos.d
$ mv CentOS-Base.repo CentOS-Base.repo.bak
$ wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo #下载阿里源的文件
# 或者
$ wget -O CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo #下载163源的文件
$ yum clean all #清理缓存
$ yum makecache #重新生成缓存
# 使用阿里repel源:https://www.cnblogs.com/renpingsheng/p/7845096.html

centos8

1
2
3
4
5
6
7
$ mv /etc/yum.repos.d /etc/yum.repos.d.backup
$ mkdir /etc/yum.repos.d
$ wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
$ yum clean all
$ yum makecache
$ yum update -y
$ yum install -y vim

使用xshell+xftp

  • SN:101210-450789-147200
  • xftp实现文件互传
  • xshell用于终端连接

VIM配置

$ yum install -y vim
http://blog.csdn.net/genglei1022/article/details/6331616
http://www.cnblogs.com/tzhangofseu/archive/2011/12/17/2290955.html
设置tab为4个空格

1
2
set ts=4
set expandtab

安装zsh

  • yum -y install zsh
  • 安装oh my zsh: $ sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
  • 配置zsh: vim ~/.zshrc

iterm2 配色

firewalld

  • 安装

    1
    2
    3
    4
    yum install firewalld
    $ systemctl unmask firewalld
    $ systemctl enable firewalld
    $ systemctl start firewalld
  • 查询开启端口

    1
    2
    3
    4
    5
    $ firewall-cmd --query-port=80/tcp
    $ firewall-cmd --zone=public --add-port=80/tcp --permanent
    $ firewall-cmd --add-port=80/tcp
    $ firewall-cmd --reload
    $ firewall-cmd --zone=public --list-ports

Telnet/TelnetServer

1
2
$ yum -y install telnet
$ yum -y install telnet-server

更新locate数据库

如果未安装则执行 yum -y install mlocate, 然后使用updatedb进行更新,使用locate进行查找

设置bash alias

系统级:/etc/bash.bashrc
用户级:~/.bashrc

1
2
3
$ cd ~
$ vim .bashrc
$ source .bashrc

设置环境变量

系统级:/etc/profile
用户级:~/.profile

1
2
3
$ cd ~
$ vim .profile
$ source .profile

swap空间

网络参数调优

1
2
3
$ sysctl -a|grep tcp #查看tcp相关参数
$ vim /etc/sysctl.conf #修改
$ sysctl -p #修改后重新加载

参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# socket缓冲
net.core.rmem_max=16777216
net.core.wmem_max=16777216
# 接收缓冲自动调整
net.ipv4.tcp_moderate_rcvbuf=1
# tcp读写缓冲
net.ipv4.tcp_rmem=4096 87380 16777216
net.ipv4.tcp_wmem=4096 65536 16777216
# syn积压队列
net.ipv4.tcp_max_syn_backlog=4096
# 监听积压队列
net.core.somaxconn=1024
# 设备积压队列
net.core.netdev_max_backlog=10000
# TCP拥塞控制
net.ipv4.tcp_congestion_control=cubic
# TCP选项
net.ipv4.tcp_sack=1
net.ipv4.tcp_fack=1
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_tw_recycle=0
# 网络接口
ipconfig eth0 txqueuelen 10000

CentOS软件

Git

yum -y install git
本地新建文件夹,推送到远端

1
2
3
4
5
6
$ cd <localdir>
$ git init
$ git add .
$ git commit -m 'message'
$ git remote add origin <url>
$ git push -u origin master

PHP

1
2
$ yum install php (安装php同时会安装apache httpd)
$ systemctl enable httpd.service (配置开机启动httpd)

C/C++开发套件

$ yum groupinstall "Development Tools"

MySql

MemCached

Redis

开机自启动redis服务(yum install方式无需设置)
redis cn

  • 安装/服务启动

    1
    2
    3
    $ yum install -y redis
    $ systemctl enable redis
    $ systemctl start redis
  • 它机访问

    1
    2
    # bind 127.0.0.1
    protected-mode no
  • firewall-cmd开启端口

  • 设置密码: /etc/redis.conf
    1
    requirepass ****

NodeJS

$ yum install nodejs npm

MongoDB

MongoDB开机自启动

Nginx

Docker

JDK && Tomcat 安装

开机自启动配置参考自启动

Tomcat

  • 多个springboot项目部署在同一tomcat上,出现jmx错误
    application.properties中添加如下配置
    1
    2
    endpoints.jmx.unique-names=true
    spring.jmx.default-domain=唯一的名字

Maven

POM配置节详解

Zookeeper

自启动设置1
自启动设置2

Dubbo Admin

Supervisor

参考

/etc/init.d/supervisord内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh
#
# /etc/rc.d/init.d/supervisord
#
# Supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord

# Source init functions
. /etc/init.d/functions

RETVAL=0
prog="supervisord"
pidfile="/tmp/supervisord.pid"
lockfile="/var/lock/subsys/supervisord"

start()
{
echo -n $"Starting $prog: "
daemon --pidfile $pidfile supervisord -c /etc/supervisor/supervisord.conf
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch ${lockfile}
}

stop()
{
echo -n $"Shutting down $prog: "
killproc -p ${pidfile} /usr/bin/supervisord
RETVAL=$?
echo
if [ $RETVAL -eq 0 ] ; then
rm -f ${lockfile} ${pidfile}
fi
}

case "$1" in

start)
start
;;

stop)
stop
;;

status)
status $prog
;;

restart)
stop
start
;;

*)
echo "Usage: $0 {start|stop|restart|status}"
;;

esac

install_supervisor.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#/bin/bash

# meld
cd /opt
wget https://pypi.python.org/packages/0f/5e/3a57c223d8faba2c3c2d69699f7e6cfdd1e5cc31e79cdd0dd48d44580b50/meld3-1.0.1.tar.gz#md5=2f045abe0ae108e3c8343172cffbe21d
tar -zxvf meld3-1.0.1.tar.gz
cd meld3-1.0.1
python setup.py install

# supervisor
cd /opt
wget https://pypi.python.org/packages/12/50/cd330d1a0daffbbe54803cb0c4c1ada892b5d66db08befac385122858eee/supervisor-3.1.4.tar.gz#md5=cb64f92409ebabf17aa0884fc407fec6
tar -zxvf supervisor-3.1.4.tar.gz
cd supervisor-3.1.4
python setup.py install

# config
mkdir /etc/supervisor
mkdir /etc/supervisor/conf.d
echo_supervisord_conf > /etc/supervisor/supervisord.conf
chmod +x /etc/init.d/supervisord

# auto-start after bootup
chkconfig supervisord on
service supervisord start

centos7自启动系列

搬瓦工与ShadowSocks

  • 搬瓦工
    搭建ss的过程可参考这里,优惠码为 BWH1ZBPVK 可节省6%
    如果没有ss配置则可以看这里
    若使用ss-server(shadowsocks-libev),其配置文件位于/etc/shadowsocks-libev
    acme

  • ShadowSocks
    各版本shadowsocks客户端下载地址
    shadowsocks manager

    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ yum update -y
    $ yum install -y vim git wget screen
    $ curl -sL https://rpm.nodesource.com/setup_8.x | bash -
    $ yum install -y nodejs # 安装8.*版本的nodejs
    $ npm i -g shadowsocks-manager # npm安装shadowsocks-manager
    $ kill -9 ** # **为默认ss-server的pid
    $ ss-manager -m aes-256-cfb -u --manager-address 127.0.0.1:4004 &
    $ ssmgr -c ~/.ssmgr/default.yml &
    $ ssmgr -c ~/.ssmgr/webgui.yml &

default.yml

1
2
3
4
5
6
7
8
9
10
type: s

shadowsocks:
address: 127.0.0.1:4004

manager:
address: 0.0.0.0:7007
password: '123456789'

db: 'default.sqlite'

webgui.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
type: m

manager:
address: 0.0.0.0:7007
password: '123456789'

plugins:
flowSaver:
use: true
user:
use: true
account:
use: true
macAccount:
use: true
email:
use: true
type: 'smtp' # 配置 smtp
username: 'xxx'
password: 'xxx'
host: 'smtp.xxx.com'
webgui:
use: true
host: '0.0.0.0'
port: '80' # 监听端口
site: 'http://ss.52xckl.cn' # 将这里改成自己的 ip 或者域名

db: 'webgui.sqlite'

V2Ray+TLS解决ss无法使用的问题

  1. 申请域名并支持HTTPS(使用freenom+阿里云https)
    freenom免费域名有效期仅有1年,1年后需重新申请新的域名
    步骤:申请域名->配置域名解析(WWW+空解析)
    阿里云SSL免费版有效期1年,1年后需重新申请免费SSL证书
    步骤:
    购买免费SSL->
    申请域名(手动验证)->
    下载txt到网站根目录(/usr/local/src/icmyfuture.github.io/.well-known/pki-validation)->
    下载ssl证书至nginx目录(/etc/nginx/ssl)
    修改nginx配置-使用新ssl证书,重启nginx
  2. 安装v2ray(选TCP即可),可参考安装
  3. 配置nginx以支持ssl,并配置转发至v2ray
    参考:原生搭建 部分的配置
  4. 下载客户端
    各种系统V2ray下载:
    下载v2ray-core
    下载v2rayN
    对v2ray-windows-64.zip进行解压,然后将下载的V2RayN.exe复制到解压后的目录,即两个下载好的文件需要在同一目录
    注意以管理员身份运行,可选择全局模式

安装Ubuntu

  • 官网下载ubuntu iso
    放入一个盘符的根目录,并解压到当前文件夹(如:放在D盘,且解压后所有文件在D盘根目录)
  • 下载 EasyBCD, 安装
  • Win10中,准备好一个未分配的盘符
  • 添加Ubuntu-iso引导项
    打开EasyBCD,添加新条目->便携式/外接存储介质->ISO,名称:Ubuntu-iso,路径:选择iso所在的位置,模式:从硬盘运行 ->添加条目,编辑引导菜单->保存设置
  • 重启电脑,引导界面选择Ubuntu-iso
  • 安装Ubuntu,分区配置如下
    1
    2
    3
    4
    16778MB     主分区         空间起始位置  Ext3日志文件系统  /
    16778MB 逻辑分区 空间起始位置 交换空间
    263MB 逻辑分区 空间起始位置 Ext3日志文件系统 /boot
    默认 逻辑分区 空间起始位置 Ext3日志文件系统 /home

安装启动引导器的设备:选择/boot所在分区

  • 添加ubuntu引导项
    进入Win10,打开EasyBCD,删除引导项Ubuntu-iso,添加新条目->操作系统->Linux/BSD,名称:Ubuntu,驱动器:选择/boot所在的分区,->添加条目,编辑引导菜单->保存设置
  • 重启系统,选择启动项Ubuntu进入Ubuntu系统

ubuntu环境

shadowsocks

需要在/etc/shadowsocks/config.json里面进行配置
使用以下别名,以守护进程模式启动/停止ss

1
2
alias sson='sudo sslocal -c /etc/shadowsocks/config.json -d start'
alias ssoff='sudo sslocal -c /etc/shadowsocks/config.json -d stop'

在Firefox中则使用FoxyProxy来设置Socks5代理

无法进入ubuntu桌面

在升级重启后,有时候会出现输入密码,但始终无法进入桌面的情况。使用ctrl+alt+f2进入命令行模式,登录后,使用sudo apt-get install ubuntu-desktop来重新安装桌面。