如何在Linux中列出用户?含本地、远程、真实及全部用户(多服务器批量操作)
Got it, let's break down how to pull all that user info across your Linux servers in one go. Since you're dealing with multiple machines on the same network, we'll use SSH for remote execution and wrap everything into a reusable script to make this painless.
Prerequisite: Set Up Passwordless SSH
First, make sure you can SSH into each server without entering a password every time—this is critical for batch execution. Run this command from your local machine for each target server:
ssh-copy-id your-username@server-ip-or-hostname
This adds your public key to the server's ~/.ssh/authorized_keys file, letting you connect automatically.
Step 1: Create a Server List File
Make a text file (e.g., servers.txt) where each line is the IP address or hostname of a server you want to query:
192.168.1.10 192.168.1.11 server01.local
Step 2: Batch Script to Fetch All User Data
Save this as fetch_user_details.sh—it will loop through each server and pull all the user info you need:
#!/bin/bash SERVER_LIST="servers.txt" # Iterate over each server in the list while read -r server; do echo "=============================================" echo "📊 Server: $server" echo "=============================================" # 1. All Users (includes local + remote auth users like LDAP/NIS) echo -e "\n--- All Users ---" ssh "$server" 'getent passwd | cut -d: -f1' # 2. Local Users (only from /etc/passwd, excludes remote auth users) echo -e "\n--- Local Users ---" ssh "$server" 'cat /etc/passwd | cut -d: -f1' # 3. Current Remote Users (actively logged in via SSH) echo -e "\n--- Active Remote Users ---" ssh "$server" 'w | grep -v "^USER" | awk "{print \"User: \" \$1, \" | IP: \" \$3}"' # 4. Recent Real Users (local + remote logins from history) echo -e "\n--- Recent Real Users (Last 20 Logins) ---" ssh "$server" 'last -w | head -20' # 5. Detailed User Permissions echo -e "\n--- User Permission Breakdown ---" ssh "$server" 'getent passwd | cut -d: -f1 | while read -r user; do echo -e "\n🔹 User: $user" echo " Groups: $(groups "$user")" echo " Login Shell: $(getent passwd "$user" | cut -d: -f7)" echo " Sudo Access: $(sudo -lU "$user" 2>/dev/null || echo "No sudo privileges")" done' echo -e "\n---------------------------------------------\n" done < "$SERVER_LIST"
Step 3: Run the Script
Make the script executable and run it (you can redirect output to a file for later review):
chmod +x fetch_user_details.sh ./fetch_user_details.sh > server_user_report.txt
Optional: Speed Up with Parallel Execution
If you have many servers, running commands sequentially can be slow. Use the parallel tool to run queries in parallel:
# Install parallel first (on Debian/Ubuntu: sudo apt install parallel) parallel ssh {} 'echo "=== Server: {} ===" && getent passwd | cut -d: -f1' :::: servers.txt
You can extend this parallel command to include all the user/permission checks from the script above.
Notes to Keep in Mind
- Remote Auth Users:
getent passwdpulls users from all configured auth sources (local, LDAP, NIS), whilecat /etc/passwdonly shows local users. Adjust based on your setup. - Sudo Access: The
sudo -lU $usercommand requires your SSH user to have sudo privileges on the target server, or the script will return "No sudo privileges" even if the user has access. - Error Handling: Add
|| echo "Failed to connect to $server"to the SSH command if you want to catch connection issues.
内容的提问来源于stack exchange,提问作者user266746

