Setup A New Linux Server

This article talks about how to set up a new Linux server.

This article talks about how to set up a new Linux server. The setup will make following changes:

  • Allow you to log in Linux using ssh without password.
  • Install the commonly used packages.
  • Set up a few important hotkeys.
  • Set up tmux.
  • Allow you to sudo without typing in password.
  • Allow your server to be accessed by its hostname (within LAN).

The tools in this section are highly recommended ones.

Make Sure Your Linux Server Has Ssh

Before everything begins, make sure your Linux server has ssh. If you are using a Linux server version, ssh should be also included. If you are using a Linux desktop version as a server, ssh might not be included by default. In that case, you’ll need to physically access the server, and install ssh by typing in on the Linux machine:

sudo apt update && sudo apt install ssh

Set Up Keys For Login Without Password

The most annoying thing about ssh is that every time you log in to your Linux server, you’ll have to type in your password. Fortunately, there is another way to login, i.e., using keys.

To do that, you’ll need to first generate a private and a public key on your client OS. First, you want to check if they already exists, by checking if there is a id_rsa and id_rsa.pub file under ~/.ssh. If there is, you can skip the key generation part. Otherwise, type in the following command on your client OS:

ssh-keygen

Once it is done, ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub will be generated.

Next, you want to copy the public key to the authorized_keys list on the target Linux server with the following command. Before to replace {username} with your real username on the server, and {server-ip} with an accessible IP of your target server.

ssh-copy-id -i ~/.ssh/id_rsa.pub {username}@{server-ip}

Here {username} is the user to login, {server-ip} is an accessible IP of your target Linux server.

Note that keys are only set up for a single user, in our case, {username}. Other users won’t be able to leverage this key.

Now, let’s log in the Linux server with the following command. Be sure to replace {username} and {server-ip} as well.

ssh {username}@{server-ip}

The rest of the configurations all happen on the server side.

Package Installation

When you get a new Linux server, it does not include some basic tools you might need. Here are some of the basic stuffs to install.

sudo apt update && sudo apt install -y ssh git tmux vim emacs python3 python3-pip net-tools tree htop curl

And this is what they do:

  • ssh: if you are using Linux server version, there is a good chance that ssh is already installed. Otherwise, install it.
  • git: the most famous version control.
  • tmux: terminal multiplexer.
  • vim: a text editor.
  • emacs: another text editor.
  • python3: python version 3.
  • python3-pip: the package management system for python3.
  • net-tools: it includes ping.
  • tree: a tool to print your dir in the form of tree.
  • htop: a process management tool.
  • curl: a page downloader.

These are the most common tools I use almost every single day. Feel free to install more if you need.

Set Up A Few Important Hotkeys

Run the following command to add some hotkeys.

echo '# set tab to cycle through auto-complete suggestions instead of listing them;
# must be placed after setting vi editing-mode.
"\C-i": menu-complete

# support del key.
"\e[3~": delete-char

# Undo in bash with ctrl+/.
"\e[47;5u": undo

# C-backspace to delete the previous word.
"\e[127;5u": backward-kill-word

# C-delete to delete the next word.
"\e[3;5~": kill-word

# C-left to move backward by a word
"\e[1;5D": backward-word

# C-right to move forward by a word
"\e[1;5C": forward-word' >> ~/.inputrc

Once these are added to ~/.inputrc, log out of the server and log back in so that they can take effects.

These hotkeys are extremely helpful, especially the C-backspace, C-delete, C-left, and C-right bindings. When you have a really long command, moving your cursor to a specific position is very slow by default. Ctrl-left and ctrl-right just make things so much faster.

Set Up Tmux

Tmux is definitely one of the most important development tools on Linux, as described in here. You really don’t want to skip tmux in your setup.

Tmux is usable with default config, but it could be configured to be nicer. The recommended config goes in here.

Set Up Sudo Without Password

Sudo is a frequently used command. It is kind of annoying to type in password every time you use it. To achieve that, replace the {username} with your real username, and run the command.

sudo sh -c 'echo "{username}     ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers'

Optional Setup

The tools in this section are helpful in certain use cases. You can install or ignore them base on your use case.

Mdns

mdns is an awesome DNS solution for a small network, e.g., a home network. Basically, with mdns, you are able to ping or connect to the server with its hostname, instead of its IP. This is very helpful in a home network situation, because most of the home network doesn’t have a dedicated DNS server, using hostnames to connect to the servers is a really handy solution.

Use the following command to install mdns server on your Linux server.

sudo apt-get install avahi-daemon

For mdns client, MacOS and Windows already has it built-in.

Once it is done, you can use {hostname}.local to access your server within you local network. For example, if you server name is ubuntu-server, you can use ssh ubuntu-server.local to connect.