Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Script to add a user to Linux system
- if [ $(id -u) -eq 0 ]; then
- read -p "Enter username : " username
- read -s -p "Enter password : " password
- egrep "^$username" /etc/passwd >/dev/null
- if [ $? -eq 0 ]; then
- echo "$username exists!"
- exit 1
- else
- pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
- useradd -m -p $pass $username
- [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
- fi
- else
- echo "Only root may add a user to the system"
- exit 2
- fi
- # adding user to sudoers list
- echo $username" ALL=(ALL) ALL" >> /etc/sudoers
- # Configuring ssh file. Removing root login, allowing only the user to login etc.
- sed '/PermitRootLogin yes/d' /etc/ssh/sshd_config
- sed '/#Port 22/d' /etc/ssh/sshd_config
- echo "PermitRootLogin no" >> /etc/ssh/sshd_config
- echo "AllowUsers "$username >> /etc/ssh/sshd_config
- echo "Port 17576" >> /etc/ssh/sshd_config
- echo "service sshd restart"
- # Blocking ssh from other IPs.
- echo "sudo iptables -A INPUT -p tcp -s 180.151.30.100 --dport 17576 -j ACCEPT"
- echo "sudo iptables -A INPUT -p tcp -s 180.151.30.99 --dport 17576 -j ACCEPT"
- echo "sudo iptables -A INPUT -p tcp --dport 22 -j DROP"
- echo "sudo iptables -A INPUT -p tcp --dport 17576 -j DROP"
- echo "sudo iptables-save"
- echo "service sshd restart"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement