dreamable/LinuxNotes
ID 标题 笔记 Tag
12
如何自动重启nginx

服务器nginx偶尔会被杀死,可能是系统资源不够了。

sudo systemctl edit nginx then paste in:

[Service]
Restart=always

check if it works

sudo systemctl status nginx
sudo pkill -f nginx
sudo systemctl status nginx
11
Install MQTT on Ubuntu 22.04

Install

sudo apt-get install mosquitto mosquitto-clients
sudo mosquitto_passwd -c /etc/mosquitto/passwd <mqtt_user> (set passwd here)
sudo vi /etc/mosquitto/conf.d/default.conf # new file with
  allow_anonymous false # disable anonymous user
  listener 1883 0.0.0.0 # open to all IP, open to localhost by default.
  password_file /etc/mosquitto/passwd # make password work
sudo /etc/init.d/mosquitto restart

Test

mosquitto_sub  -h localhost -u netro -P netrohome -t 'test'
mosquitto_pub -h localhost -u netro -P netrohome -t 'test' -m 'hello'

Increase max open file limit issue

The default max open file is 1024, which may not be enough for large MQTT service, increase it to 65535
# vi /etc/systemd/system.conf
DefaultLimitNOFILE=65536:524288
sudo systemctl daemon-reexec
sudo /etc/init.d/mosquitto restart
# cat /proc/<pid>/limits should be: 
Max open files            65535                524288               files
# count the number of file opened by: 
sudo ls /proc/<pid>/fd | wc -l
10
LVM 空间跑哪里去了

安装ubuntu,默认LVM,全盘安装。但是装完发现只有100G,另外的130多G去哪里了?

参见guide

扩展

lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
9
undo remote commits in Git

undo remote commits:

git reset --hard 'xxxxx'

git clean -f -d

git push -f

8
AWS Elastic IP
  • 保留IP,最多5个
  • 这样EC2重启后IP地址不再变化
  • 有些区域的可以设定reverse DNS,对发送email有帮助
7
gio mount smb
gio mount smb://<servername>/<sharename>
# enter password
# once the mount is created it will create a temporary path located at  the following path, for example:
# /run/user/1000/gvfs/smb-share\:server\=host.domain.com\,share\=files/
ln -s /run/user/<userid>/gvfs/smb-share\:server\=<servername>\,share\=<sharename>/ ~/<mountpoint>
6
Ubuntu disable sleep
 sudo -H gedit /etc/systemd/logind.conf
 HandleLidSwitch=ignore
 sudo service systemd-logind restart
 # Still sleep after idle
 sudoedit /etc/UPower/UPower.conf
 Change IgnoreLid to IgnoreLid=true
 service upower restart
# also in /etc/systemd/logind.conf
HandleLidSwitch=ignore
HandleLidSwitchExternalPower=ignore
HandleLidSwitchDocked=ignore
HandleHibernateKey=ignore
HandleSuspendKey=ignore
IdleAction=ignore
IdleActionSec=0
 # Still sleep after idle
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
5
内网穿透

路由器NAT映射端口,尝试并成功

  1. ssh
  2. rdesktop
  3. smb
4
Basic HTTP File Server

python3 -m http.server

3
How do I resolve a ssh connection closed by remote host due to inactivity?

source

  • Add to .ssh/config
TCPKeepAlive yes
ServerAliveInterval 30
  • In command line
ssh -o TCPKeepAlive=yes user@some.host.com
ssh -o ServerAliveInterval=30 user@some.host.com

TCPKeepAlive: This uses the KEEPALIVE option of the TCP/IP protocol to keep a connection alive after a specified interval of inactivity. On most systems, this means 2 hours. So, with the TCPKeepAlive option passed to SSH, the SSH client will send an encrypted packet to the SSH server, keeping your TCP connection up and running.

ServerAliveInterval: This sets a timeout interval in seconds, which is specified by you, from which if no packets are sent from the SSH client to the SSH server, SSH will send an encrypted request to the server for a TCP response. To make that request every 30 seconds:

2
Mongo DB dump with compress and archive

source

mongodump --db country --gzip --archive=country.archive
mongorestore --gzip --archive=country.archive

NOTE: The archive is mongo-specific format, can't be opened by other tools e.g. tar

mongodb
1
How to run a cron job using the sudo command

guide

sudo crontab -e
@hourly rm somefile