Ryan Harrison My blog, portfolio and technology related ramblings

Ubuntu Server Setup Part 1 - Logging In

In this set of posts I’ll be running through setting up a new Ubuntu server - from initial setup and security through to installation of some of the most common packages. You can get your very own server/VPS through companies such as DigitalOcean or OVH - in this case we’ll be using Ubuntu Server 18.04, but the same concepts should apply to previous and future releases as well.

In this post we’ll be covering how to login to your server for the first time through SSH and creating a new user with root privileges.

Logging in as Root

When you create your new server, you should receive an email with directions on how to login to your server. This will normally include the public IP address of your server and a randomly generated password which will allow you to login as the main root user. The root user is the main administrator in a Linux system and so has privileges to do pretty much anything. You therefore generally don’t want to be logged in as root for day-to-day operations where you can potentially cause damage quite easily. We do however need to login as root initially (as at this point we don’t have any other users).

In this post we’ll be using PuTTY as an SSH client, however all other alternatives will work just as well. Inside your initial email you should have the public IP of your server along with the root password. In the PuTTY client window enter the IP address into the Host Name field and press ‘Open’ (assuming SSH is selected and the port is set to the default 22).

PuTTY

You should be greeted with a black window prompting you for the user your wish to login as - in this case ‘root’. Enter the root password when prompted. As this is the first time you are logging in, you will also be prompted to change the default root password (make sure to choose something strong).

PuTTY Login Screen

If you see a new bash shell, then you have successfully logged in to your server and you can move on to setting up your own new user.

PuTTY Bash Shell

Creating a New User

As I said before, working as root is generally considered a bad idea. Instead, we will be creating a new user which we will be logging in as in all future sessions.

$ adduser demo

This command will create a new user called ‘demo’ (change the name to be something more relevant). You will be prompted for a new password (again choose a strong one) along with some general information for the new user (you can skip this be pressing Enter).

Granting Root Privileges

We have just created a new user, but by default they have very limited privileges which prevents them from installing new packages or modifying any system files. Obviously for us this is not ideal, so instead we can grant the new user ‘sudo’ privileges, which allows us to run commands as root without having to actually be logged in as root. To grant these privileges, run the following as root:

$ gpasswd -a demo sudo

or

$ sudo usermod -aG sudo demo

You can now run commands as root by simply adding sudo to the beginning - essentially meaning that you never need to login as root anymore. For additional security we can also completely disable root login through ssh (which stops a lot of brute force attacks) which will be covered in a future post.

Now we have a new user setup, we still need to do some more work to better secure our server. In the next post we will set up public key authentication which is a much more secure method than the simple password we have at the moment.

Read More

Bash - Redirecting stdout and stderr

Redirect stdout/stderr to a truncated file:

# StdOut
cmd > out.txt # (stderr printed to console)
# StdErr
cmd 2> err.txt # (stdout printed to console)

Redirect stdout/stderr to a file (appending):

# StdOut
cmd >> out.txt # (stderr printed to console)
# StdErr
cmd 2>> err.txt # (stdout printed to console)

Redirect both stdout and stderr in same command to different files (truncating):

cmd > out.txt 2> err.txt

Redirect both stdout and stderr to same output (truncating):

cmd > out.txt 2>&1

“Redirection statements are evaluated, as always, from left to right. >> file - STDOUT to file (append mode) (short for 1>> file) 2>&1 - STDERR to ‘where stdout goes’ Note that the interpretation ‘redirect STDERR to STDOUT’ is wrong”

Source

Read More

Htop Colours Explained

Htop is a great expansion to the top command in Unix based systems. Rather than just spitting out metrics to the console, htop provides a complete ncurses-based interface which makes it extremely useful when managing processes.

The header of htop contains a number of bars that show CPU usage, memory usage and swap usage. The bars are all colour coded to show different aspects of the usage. This is great when you know the what the colours mean, but htop doesn’t make it too obvious what they mean. The F1 (Help) menu offers a guide to the colours of the usage bars:

htop colour guide

Read More

Python - Simple HTTP Server

Python comes with a really handy built-in HTTP server that you can get up and running in a matter of seconds - and without having to mess around with something like Apache. This is great for serving simple web pages locally through HTTP to get around the limitations of the file:// protocol within the browser.

The command to start the server unsurprisingly differs from Python 2 to Python 3, but either way it’s really easy to remember. Simply open up a terminal, change directory to the folder containing the files you want to serve and either of the following to start the server depending on your Python version:

Python 2:

cd files/my-directory
python -m SimpleHTTPServer

Serving HTTP on 0.0.0.0 port 8000 ...

Python 3:

cd files/my-directory
python -m http.server

Serving HTTP on 0.0.0.0 port 8000 ...

Then navigate to http://localhost:8000 in your browser to see your files (by default you get a directory listing if there is no index.html file).

You can also specify the port you want to use if 8000 doesn’t work for you:

python -m SimpleHTTPServer 8080
python -m http.server 8080

Serving HTTP on 0.0.0.0 port 8080 ...
Read More

Common Port Mappings

List of common ports and their uses/associated urls:

Port Application URL/Command
4000 Jekyll Serve http://localhost:4000/
9091 Transmission Web Client http://localhost:9091/
22 SSH (default) ssh ip.address
80 HTTP http://localhost
443 HTTPS https://localhost/
32400 Plex Media Server http://127.0.0.1:32400/web
8080 Tomcat Server http://localhost:8080/
3000 BrowserSync http://localhost:3000/
9200 ElasticSearch http://localhost:9200/
5000 Flask http://localhost:5000/
8000 Python HTTP Server python2 -m SimpleHTTPServer / python3 -m http.server
3306 MySQL Server  
25 SMTP (Postfix Mail Forwarding)  
587 SMTP (Postfix Outgoing Mail Server)  
Read More