Linux笔记
平时主要以Linux为主要的工作环境,经常会遇到一些重复性问题,因此在此进行记录。
Linux自动运行总结
Linux系统中有多种自动运行程序的方式,在此做一个较为全面的总结。自动运行包括:开机自动运行程序、登录自动运行程序、退出运行程序、定期自动运行程序、定时自动运行程序。不同类型的自动运行任务又有多种解决方法。
开机启动时自动运行程序
Linux加载后, 首先会初始化硬件和设备驱动, 然后运行第一个进程init。init根据配置文件继续引导过程,启动其它进程。通常情况下,可以将修改后的脚本放置在下面的某个目录中:
/etc/rc
/etc/rc.d
/etc/rc?.d
目录下的脚本文件,可以使init自动启动其它程序。例如:编辑/etc/rc.d/rc.local 文件(该文件通常是系统最后启动的脚本),在文件最末加上一行“xinit”或“startx”,可以在开机启动后直接进入X-Window。
用户登录时自动运行程序
用户登录时,bash先自动执行系统管理员建立的全局登录script :
/ect/profile
然后bash在用户起始目录下按顺序查找三个特殊文件中的一个:
~/.bash_profile
~/.bash_login
~/.profile
但只执行最先找到的一个。因此,只需根据实际需要在上述文件中加入命令就可以实现用户登录时自动运行某些程序(类似于DOS下的Autoexec.bat)。
退出登录时自动运行程序
退出登录时,bash自动执行个人的退出登录脚本
~/.bash_logout
例如,在/.bash_logout中加入命令“tar -cvzf c.source.tgz *.c”,则在每次退出登录时自动执行 “tar” 命令备份 *.c 文件。
定期自动运行程序
Linux有一个称为crond的守护程序,主要功能是周期性地检查 /var/spool/cron目录下的一组命令文件的内容,并在设定的时间执行这些文件中的命令。用户可以通过crontab 命令来建立、修改、删除这些命令文件。
例如,建立文件crondFile,内容为“00 9 23 Jan * HappyBirthday”,运行“crontabcronFile”命令后,每当元月23日上午9:00系统自动执行“HappyBirthday”的程序(“*”表示不管当天是星期几)。
定时自动运行程序一次
定时执行命令at 与crond 类似(但它只执行一次):命令在给定的时间执行,但不自动重复。at命令的一般格式为:at [ -f file ] time ,在指定的时间执行file文件中所给出的所有命令。也可直接从键盘输入命令:
$ at 12:00
at>mailto Roger -s ″Have a lunch″ < plan.txt
at>Ctr-D
Job 1 at 2000-11-09 12:00
2000-11-09 12:00时候自动发一标题为“Have a lunch”,内容为plan.txt文件内容的邮件给Roger.
开放指定端口
开放服务器的指定端口,用来保证服务器被正常访问。
- 防火墙操作
sudo ufw status # 查看是否启用,开放哪些端口
sudo ufw enable/disable # 开启/关闭
sudo ufw reload # 重启防火墙
sudo ufw allow [port] # 开放端口
sudo ufw deny [port] # 关闭端口
- 安装
iptables
默认情况下,Ubuntu系统自带该软件。
sudo apt-get update
sudo apt-get install iptables
- 开放指定端口
sudo iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
此处以开放 8080 为例。
- 永久保存
为避免服务器重启,导致已经开放的端口不被开启,需要对上述操作保存。
sudo iptables-save
sudo apt-get install iptables-persistent
sudo netfilter-persistent save
sudo netfilter-persistent reload
常用命令总结
sudo apt-get upgrade
sudo apt-get install build-essential # 基本编译器
sudo apt-get install nginx
nginx -V # 查看版本
获得nginx信息:
nginx version: nginx/1.14.0 (Ubuntu)
built with OpenSSL 1.1.1 11 Sep 2018 (running with OpenSSL 1.1.1j 16 Feb 2021)
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-YlUNvj/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
# 下载一个nginx安装包
wget https://nginx.org/download/nginx-1.14.0.tar.gz
tar -xvf nginx-1.14.0.tar.gz
cd nginx-1.14.0/
sudo ./configure --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-YlUNvj/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
报错:
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using –without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using –with-pcre=
option.
解决:
# 安装方法1
apt-get install pcre-devel
# 安装方法2
wget https://sourceforge.net/projects/pcre/files/pcre/8.44/pcre-8.44.tar.gz
tar zxvf pcre-8.44.tar.gz
cd pcre-8.44
./configure
make && make install
pcre-config --version
sudo apt-get update
sudo apt-get install libpcre3=2:8.39-9 libpcre3-dev
sudo apt-get install openssl libssl-dev
sudo ./configure --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-YlUNvj/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --with-openssl=/home/wuhaiming/software/openssl-1.1.0e
报错:
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
配置zerotier
zerotier官网:https://www.zerotier.com/download/
curl -s https://install.zerotier.com | sudo bash
# 输出:
...
*** Success! You are ZeroTier address [ 90a3cd8b7b ].
# 加入网络:
sudo zerotier-cli join 你的network ID
$ sudo zerotier-cli join af78xxxxxxxxxf92e
200 join OK
ssh操作
1.判断是否安装:
ssh localhost
-> ssh: connect to host localhost port 22: Connection refused
2.安装:
sudo apt-get update
sudo apt-get install openssh-server
3.启动服务:
sudo /etc/init.d/ssh start
sudo service sshd start
sudo service sshd stop
4.查看服务是否正确启动:
ps -e|grep ssh
5.修改配置
vi /etc/ssh/sshd_config
sudo apt-get autoremove openssh-client openssh-server
sudo apt-get install openssh-client openssh-server
/etc/init.d/ssh restart 重启ssh ,测试成功
1、先停掉SSH服务:sudo stop ssh
2、卸载openssh-server:apt-get remove openssh-server
3、卸载openssh-client: apt-get remove openssh-server
4、安装openssh-server:apt-get install openssh-server
5、安装openssh-client:apt-get install openssh-client
6、安装完成以后,启动服务:sudo /etc/init.d/ssh start
7、启动后,查看服务是否正确启动: ps -e|grep ssh
8、确认ssh-server已经正常工作: netstat -tlp
生成密钥
ssh-keygen
换源与软件更新
# 1.备份原来的源
sudo cp /etc/apt/sources.list /etc/apt/sources_init.list
# 2.更换源
sudo vim /etc/apt/sources.list
# 将阿里源放进去,并保存,推荐清华源
deb http://mirrors.aliyun.com/ubuntu/ xenial main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe
# 3.退出、保存,而后更新
sudo apt-get update
# 复损坏的软件包,尝试卸载出错的包,重新安装正确版本
sudo apt-get -f install
# 更新软件
sudo apt-get upgrade
开机运行脚本
- 编写要运行的脚本
test.sh和启动脚本start.sh
start.sh:
#!/bin/bash
#此处编写脚本内容
cd /home/Desktop/
./test.sh
exit 0
以#!/bin/bash开头 中间写脚本内容 exit0结尾
- 移动
start.sh到/etc/init.d目录下并增加权限
sudo mv start.sh /etc/init.d
sudo chmod +750 start.sh
# 设置自动启动
sudo update-rc.d test.sh defaults