|
1 |
| - #!/bin/bash |
2 |
| - |
3 |
| - # Check if the script is run as root |
4 |
| - if [ "$EUID" -ne 0 ]; then |
5 |
| - echo "Please run as root" |
6 |
| - exit 1 |
7 |
| - fi |
8 |
| - |
9 |
| - # Install OpenSSH Server |
10 |
| - echo "Installing OpenSSH Server..." |
11 |
| - apt-get update |
12 |
| - apt-get install -y openssh-server |
13 |
| - |
14 |
| - # Enable and start the SSH service |
15 |
| - echo "Enabling and starting the SSH service..." |
16 |
| - systemctl enable ssh |
17 |
| - systemctl start ssh |
18 |
| - |
19 |
| - # Install UFW (Uncomplicated Firewall) |
20 |
| - echo "Installing UFW (Uncomplicated Firewall)..." |
21 |
| - apt-get install -y ufw |
22 |
| - |
23 |
| - # Allow SSH through the firewall |
24 |
| - echo "Allowing SSH traffic on port 22..." |
25 |
| - ufw allow 22/tcp |
26 |
| - |
27 |
| - # Enable the firewall |
28 |
| - echo "Enabling the firewall..." |
29 |
| - ufw enable |
30 |
| - |
31 |
| - # Get the server's IP address |
32 |
| - IP_ADDRESS=$(hostname -I | awk '{print $1}') |
33 |
| - if [ -z "$IP_ADDRESS" ]; then |
34 |
| - echo "Could not retrieve IP address. Please check your network settings." |
35 |
| - exit 1 |
36 |
| - fi |
37 |
| - |
38 |
| - # Get the current username |
39 |
| - USERNAME=$(whoami) |
40 |
| - |
41 |
| - # Display the login information |
42 |
| - echo "=====================================" |
43 |
| - echo "OpenSSH Server has been installed and configured." |
44 |
| - echo "You can access this server using the following information:" |
45 |
| - echo "" |
46 |
| - echo "IP Address: $IP_ADDRESS" |
47 |
| - echo "Username: $USERNAME" |
48 |
| - echo "" |
49 |
| - echo "To log in from another machine, use the following command:" |
50 |
| - echo "ssh $USERNAME@$IP_ADDRESS" |
51 |
| - echo "=====================================" |
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# setup open ssh server on ubuntu |
| 4 | +# status: tested |
| 5 | +# published by: Deepak Raj |
| 6 | +# published on: 2024-08-28 |
| 7 | + |
| 8 | +# Check if the script is run as root |
| 9 | +if [ "$EUID" -ne 0 ]; then |
| 10 | + echo "Please run as root" |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | + |
| 14 | +# Install OpenSSH Server |
| 15 | +echo "Installing OpenSSH Server..." |
| 16 | +apt-get update |
| 17 | +apt-get install -y openssh-server |
| 18 | + |
| 19 | +# Enable and start the SSH service |
| 20 | +echo "Enabling and starting the SSH service..." |
| 21 | +systemctl enable ssh |
| 22 | +systemctl start ssh |
| 23 | + |
| 24 | +# Install UFW (Uncomplicated Firewall) |
| 25 | +echo "Installing UFW (Uncomplicated Firewall)..." |
| 26 | +apt-get install -y ufw |
| 27 | + |
| 28 | +# Allow SSH through the firewall |
| 29 | +echo "Allowing SSH traffic on port 22..." |
| 30 | +ufw allow 22/tcp |
| 31 | + |
| 32 | +# Enable the firewall |
| 33 | +echo "Enabling the firewall..." |
| 34 | +ufw enable |
| 35 | + |
| 36 | +# Get the server's IP address |
| 37 | +IP_ADDRESS=$(hostname -I | awk '{print $1}') |
| 38 | +if [ -z "$IP_ADDRESS" ]; then |
| 39 | + echo "Could not retrieve IP address. Please check your network settings." |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +# Get the current username |
| 44 | +USERNAME=$(whoami) |
| 45 | + |
| 46 | +# Display the login information |
| 47 | +echo "=====================================" |
| 48 | +echo "OpenSSH Server has been installed and configured." |
| 49 | +echo "You can access this server using the following information:" |
| 50 | +echo "" |
| 51 | +echo "IP Address: $IP_ADDRESS" |
| 52 | +echo "Username: $USERNAME" |
| 53 | +echo "" |
| 54 | +echo "To log in from another machine, use the following command:" |
| 55 | +echo "ssh $USERNAME@$IP_ADDRESS" |
| 56 | +echo "=====================================" |
0 commit comments