Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Makefile for dotfiles_server
# Quick shortcuts for all installation commands

.PHONY: help setup ssh-port ssh-timeout php php-ext lazydocker \
.PHONY: help setup ssh-port ssh-timeout hostname php php-ext lazydocker \
global-dev add-user zabbix-server zabbix-client update-zabbix-ip \
fix-mysql clean

Expand All @@ -14,9 +14,10 @@ help:
@echo "Usage: make <command>"
@echo ""
@echo "System Setup:"
@echo " make setup - Setup the server"
@echo " make ssh-port PORT=XXXX - Change SSH port (default: 22)"
@echo " make ssh-timeout - Configure SSH timeout (5min auto-disconnect)"
@echo " make setup - Setup the server"
@echo " make ssh-port PORT=XXXX - Change SSH port (default: 22)"
@echo " make ssh-timeout - Configure SSH timeout (5min auto-disconnect)"
@echo " make hostname NAME=server - Change system hostname"
@echo ""
@echo "PHP & Development:"
@echo " make php - Install PHP"
Expand All @@ -40,6 +41,7 @@ help:
@echo "Examples:"
@echo " make setup"
@echo " make ssh-port PORT=19742"
@echo " make hostname NAME=myserver"
@echo " make php-ext VER=8.4"
@echo " make global-dev-force"
@echo " make add-user USER=john"
Expand All @@ -62,6 +64,13 @@ ssh-port:
ssh-timeout:
@bash install.sh ssh_timeout

hostname:
@if [ -z "$(NAME)" ]; then \
sudo bash install.sh hostname; \
else \
sudo bash install.sh hostname $(NAME); \
fi

# PHP & Development
php:
@bash install.sh php
Expand Down Expand Up @@ -128,6 +137,7 @@ clean:
s: setup
sp: ssh-port
st: ssh-timeout
hn: hostname
p: php
pe: php-ext
ld: lazydocker
Expand All @@ -146,6 +156,7 @@ shortcuts:
@echo " s = setup"
@echo " sp = ssh-port"
@echo " st = ssh-timeout"
@echo " hn = hostname"
@echo " p = php"
@echo " pe = php-ext"
@echo " ld = lazydocker"
Expand All @@ -161,6 +172,7 @@ shortcuts:
@echo "Examples:"
@echo " make s"
@echo " make sp PORT=19742"
@echo " make hn NAME=myserver"
@echo " make pe VER=8.4"
@echo " make gdf"
@echo " make au USER=john"
Expand Down
103 changes: 51 additions & 52 deletions docs/SSH-TIMEOUT-DEBUG.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
# Hướng dẫn Debug SSH Auto-Logout
# SSH Auto-Logout Debug Guide

## Vấn đề: SSH không tự động logout sau 5 phút
## Problem: SSH does not auto logout after 5 minutes

### Bước 1: Kiểm tra cấu hình hiện tại
### Step 1: Check current configuration

Chạy script verify:
Run the verify script:
```bash
bash /path/to/setup/system/verify-ssh-timeout.sh
```

Hoặc kiểm tra thủ công:
Or check manually:
```bash
grep -E "^ClientAlive" /etc/ssh/sshd_config
```

### Bước 2: Các nguyên nhân thường gặp
### Step 2: Common causes

#### ✅ Nguyên nhân 1: ssh.socket đang override cấu hình
**Triệu chứng:** Port trong sshd_config không có hiệu lực
**Giải pháp:**
#### ✅ Cause 1: ssh.socket is overriding configuration
**Symptom:** Port in sshd_config has no effect
**Solution:**
```bash
sudo systemctl stop ssh.socket
sudo systemctl disable ssh.socket
sudo systemctl daemon-reload
sudo systemctl restart ssh
```

#### ✅ Nguyên nhân 2: Cấu hình bị duplicate hoặc comment
**Kiểm tra:**
#### ✅ Cause 2: Duplicate or commented configuration
**Check:**
```bash
grep -n "ClientAlive" /etc/ssh/sshd_config
```

**Giải pháp:** Xóa tất cả dòng cũ và thêm lại:
**Solution:** Remove all old lines and add again:
```bash
sudo sed -i '/^ClientAliveInterval/d' /etc/ssh/sshd_config
sudo sed -i '/^ClientAliveCountMax/d' /etc/ssh/sshd_config
Expand All @@ -41,76 +41,76 @@ echo 'ClientAliveCountMax 5' | sudo tee -a /etc/ssh/sshd_config
sudo systemctl restart ssh
```

#### ✅ Nguyên nhân 3: Client SSH có keepalive riêng
**Triệu chứng:** Mac/Windows Terminal tự động gửi keepalive
**Kiểm tra client:**
#### ✅ Cause 3: SSH client has its own keepalive
**Symptom:** Mac/Windows Terminal automatically sends keepalive
**Check client:**
- Mac/Linux: `cat ~/.ssh/config | grep ServerAlive`
- Windows: Kiểm tra PuTTY/Terminal settings
- Windows: Check PuTTY/Terminal settings

**Giải pháp:** Tắt keepalive client hoặc giảm thời gian timeout xuống:
**Solution:** Disable keepalive on client or reduce server timeout:
```bash
# Server side - giảm xuống 3 phút để test
# Server side - reduce to 3 minutes for testing
ClientAliveInterval 30
ClientAliveCountMax 6
# = 30s × 6 = 180s (3 phút)
# = 30s × 6 = 180s (3 minutes)
```

#### ✅ Nguyên nhân 4: Service chưa restart đúng cách
**Giải pháp:**
#### ✅ Cause 4: Service not restarted properly
**Solution:**
```bash
# Kiểm tra service name
# Check service name
systemctl list-units | grep ssh

# Restart đúng service
sudo systemctl restart sshd # hoặc
# Restart the correct service
sudo systemctl restart sshd # or
sudo systemctl restart ssh

# Verify
sudo systemctl status sshd --no-pager
```

### Bước 3: Test timeout
### Step 3: Test timeout

**Test 1: Kiểm tra config có load không**
**Test 1: Check if config is loaded**
```bash
sudo sshd -T | grep clientalive
```
Kết quả mong đợi:
Expected result:
```
clientaliveinterval 60
clientalivecountmax 5
```

**Test 2: Mở session mới và chờ**
1. Giữ session hiện tại
2. Mở terminal mới: `ssh user@server`
3. Không làm gì trong 5 phút
4. Session mới phải tự động ngắt
**Test 2: Open a new session and wait**
1. Keep the current session open
2. Open a new terminal: `ssh user@server`
3. Do nothing for 5 minutes
4. The new session should auto disconnect

**Test 3: Kiểm tra log**
**Test 3: Check logs**
```bash
# Xem SSH log realtime
# View SSH log in realtime
sudo tail -f /var/log/auth.log | grep sshd

# Hoặc
# Or
sudo journalctl -u ssh -f
```

### Bước 4: Troubleshooting nâng cao
### Step 4: Advanced troubleshooting

#### Kiểm tra tất cả process liên quan:
#### Check all related processes:
```bash
ps aux | grep sshd
sudo ss -tlnp | grep ssh
```

#### Kiểm tra Drop-in configs:
#### Check Drop-in configs:
```bash
ls -la /etc/ssh/sshd_config.d/
cat /etc/ssh/sshd_config.d/*.conf 2>/dev/null
```

#### Reset hoàn toàn:
#### Full reset:
```bash
# Backup
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup
Expand All @@ -133,40 +133,39 @@ sudo systemctl daemon-reload
sudo systemctl restart ssh
```

### Bước 5: Nếu vẫn không work
### Step 5: If still not working

Thử với TCPKeepAlive (khác với ClientAlive):
Try with TCPKeepAlive (different from ClientAlive):
```bash
echo 'TCPKeepAlive yes' | sudo tee -a /etc/ssh/sshd_config
sudo systemctl restart ssh
```

Hoặc sử dụng cấu hình aggressive hơn:
Or use a more aggressive configuration:
```bash
ClientAliveInterval 30
ClientAliveCountMax 3
# = 30s × 3 = 90s (1.5 phút timeout)
# = 30s × 3 = 90s (1.5 minutes timeout)
```

### ⚠️ LƯU Ý QUAN TRỌNG
### ⚠️ IMPORTANT NOTES

1. **Luôn giữ session hiện tại mở** khi test để tránh bị lock khỏi server
2. **Test với session mới** trước khi đóng session cũ
3. **Firewall** có thể giữ connection alive, kiểm tra:
1. **Always keep the current session open** when testing to avoid locking yourself out
2. **Test with a new session** before closing the old one
3. **Firewall** may keep the connection alive, check with:
```bash
sudo iptables -L -n -v | grep ESTABLISHED
```
4. **Client-side keepalive** có thể override server config
4. **Client-side keepalive** may override server config

### Script tự động fix tất cả
### Auto-fix script

Chạy lại script setup với force:
Rerun the setup script with force:
```bash
sudo bash /path/to/setup/system/ssh_timeout.sh
```

Sau đó verify:
Then verify:
```bash
bash /path/to/setup/system/verify-ssh-timeout.sh
```

16 changes: 16 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ fix_mysql_frozen() {
sudo bash fix-mysql-frozen.sh
}

hostname_setup() {
cd "$CURRENT_DIR/setup/system" || exit
sudo bash hostname-setup.sh "$@"
}

usage() {
echo "Usage: bash $0 [command] [args]"
echo ''
Expand All @@ -111,6 +116,7 @@ usage() {
echo ' zabbix_server Install Zabbix Server (auto-detect Nginx/Apache)'
echo ' zabbix_client Install Zabbix Agent (client) [server_ip]'
echo ' update_zabbix_ip Update Zabbix Server IP for installed agent [new_ip]'
echo ' hostname Change system hostname [new_hostname]'
echo ' fix_mysql Fix MySQL frozen issue after MariaDB to MySQL migration'
echo ''
echo 'Args for global_dev:'
Expand All @@ -132,6 +138,9 @@ usage() {
echo 'Args for update_zabbix_ip:'
echo ' [new_ip] New Zabbix Server IP (optional, will prompt if not provided)'
echo ''
echo 'Args for hostname:'
echo ' [new_hostname] New hostname (optional, will prompt if not provided)'
echo ''
echo 'Example:'
echo " bash $0 setup"
echo " bash $0 ssh_port 12345"
Expand All @@ -149,6 +158,8 @@ usage() {
echo " bash $0 zabbix_client 192.168.1.100"
echo " bash $0 update_zabbix_ip"
echo " bash $0 update_zabbix_ip 192.168.1.200"
echo " bash $0 hostname"
echo " bash $0 hostname myserver"
echo " bash $0 fix_mysql"
echo ''
}
Expand Down Expand Up @@ -202,6 +213,11 @@ case "${1:-}" in
fix_mysql_frozen
;;

hostname | hn)
shift # Remove command name
hostname_setup "$@"
;;

*)
usage
exit 1
Expand Down
21 changes: 18 additions & 3 deletions setup/packages/list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,25 @@ if [ "" = "$PKG_OK" ]; then
fi

echo "=========================== zsh ==========================="
bash zsh.sh
while true; do
if [[ $ACCEPT_INSTALL =~ ^[Yy]$ ]]; then
yn="y"
else
read -r -p "Do you want to install zsh and oh-my-zsh? (Y/N) " yn
fi
case $yn in
[Yy]*)
bash zsh.sh
break
;;
[Nn]*) break ;;
*) echo "Please answer yes or no." ;;
esac
done


installPackages() {
PACKAGE_LIST=("curl" "wget" "vim" "tmux" "nano" "npm" "certbot" "python3-certbot-nginx" "fail2ban" "htop" "btop")
PACKAGE_LIST=("curl" "wget" "make" "vim" "tmux" "nano" "npm" "certbot" "python3-certbot-nginx" "fail2ban" "htop" "btop")

for packageName in "${PACKAGE_LIST[@]}"; do
echo "=========================== $packageName ==========================="
Expand All @@ -21,7 +36,7 @@ installPackages() {
echo "Checking for $packageName: $PKG_OK"
if [ "" = "$PKG_OK" ]; then
echo "No $packageName. Setting up $packageName."
sudo apt-get install -y "$packageName"
sudo apt install -y "$packageName"
fi
echo ""
done
Expand Down
13 changes: 9 additions & 4 deletions setup/packages/nvm-global.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,16 @@ export NVM_DIR="/usr/local/nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

# Install latest LTS Node.js version
echo ""
echo "Installing latest LTS Node.js version..."
if command -v nvm &>/dev/null; then
nvm install --lts
echo "Installing latest LTS Node.js version..."
if ! nvm install --lts --reinstall-packages-from=current; then
echo "Cleaning up existing Node.js installation directories..."
NODE_VERSION=$(nvm version-remote --lts)
if [ -d "$NVM_DIR/versions/node/$NODE_VERSION" ]; then
sudo rm -rf "$NVM_DIR/versions/node/$NODE_VERSION"
fi
nvm install --lts
fi
nvm use --lts
nvm alias default 'lts/*'

Expand Down Expand Up @@ -200,4 +206,3 @@ echo " nvm --version"
echo " node --version"
echo " npm --version"
echo ""

Loading
Loading