For older versions of Ubuntu, many of the tutorials online show you how to edit your server’s hostname by simply editing /etc/hostname and then restarting the service with “service hostname restart”.
However, attempting this on a 16.04 Ubuntu Server build will most likely fail with the following error message:
Failed to restart hostname.service: Unit hostname.service is masked.
As it turns out, the new way of changing your hostname on systems running systemd (i.e. Ubuntu 16.04) requires you to use the hostnamectl command, meaning that to set your new hostname you need to run:
sudo hostnamectl set-hostname NEWNAME
(Where obviously NEWNAME is the new/desired hostname that you want to use).
You can check that the change has been affected by running:
hostname
Worth jotting down here for future reference.
Ubuntu Server: How to change or set the Timezone Tips, Tricks and Tutorials 29 SEP 2017
Dropping this as a quick reminder to myself. When confronted with a new Ubuntu server instance, I can check the currently set time zone by simply asking for the current time with
date
Now if I need to change the currently set time zone to something else, the easiest would be to just run
sudo dpkg-reconfigure tzdata
This should bring up an easy to use graphical interface that will allow you to select the correct geographical region/city time zone that you wish to have your server reflect.
Note: Because your cron jobs all depends on this information, it is a VERY good idea to restart the cron service after making your timezone change:
sudo service cron restart
Also worth pointing out, if you are not particularly sure on which city time zone to choose, you could always select the geographic area “Etc” and then choose from the more generic options like GMT or GMT+2.
(UPDATE: Oh. Turns out that that I’ve left this note for myself before. I may need to rethink my note system.)
Related Link: Ubuntu Server
Simple way to Backup Files from an Ubuntu Server to Amazon S3 Cloud Tips, Tricks and Tutorials 17 MAY 2016
You can never have too many backups, and this is a simple way of backing up files from an Ubuntu server to the Amazon S3 cloud storage system.
For this, you will obviously need an Ubuntu server, an Internet connection, and of course, an Amazon AWS account.
First things first, you’ll need to generate Amazon AWS access keys, which you do from the AWS Security Credentials page (Access Keys section) in the AWS console.
Write these (both the Access Key ID and Secret Access Key) down somewhere safe, because you definitely don’t want to be losing them unnecessarily. (Maybe a Google Doc might be a good idea here?)
Now head over to the S3 Management page in the AWS console, where you will need to create the new bucket (or folder in an existing bucket) where you want to store your backed up files in.
With your bucket created, and your access details at hand, head into your Ubuntu server and install the super useful Amazon S3 targeted s3cmd package:
sudo apt-get install s3cmd
Next configure it by entering the requested information (your Access Key details will be needed here). Note, you do have the option to encrypt the files in transit, and if you choose to do so, it is probably worth your while to jot down the password in that previously mentioned Google Docs file of yours!
s3cmd --configure
Run the connection test and if everything passes, you should be good to go. You can check your current buckets by doing a directory listing with s3cmd:
s3cmd ls
You are pretty much just about there now. To do the file backup, we’ll use s3cmd’s built in sync command. To push files to Amazon S3, we declare the parameters in the order of local files then target directory. So for example, if we have a S3 bucket called server-backup, and want to back up our user account’s home directory to S3, the sync call would look like this:
s3cmd sync ~/* s3://server-backup
You can of course get all clever and target specific folders, exclude or include files and folders using wildcard characters, etc. (See the documentation for more). For example, here I exclude .svn folder files using:
s3cmd sync --exclude '*.svn' ~/* s3://server-backup
If you are happy with the sync result, then all that is left is to throw the command into a short bash file, give it execute rights and add it to the cron scheduled tasks system. So for example, create the file cron_s3_backup.sh:
nano /home/craiglotter/cron_s3_backup.sh
Add this text:
#!/bin/bash s3cmd sync /home/craiglotter/* s3://server-backup/craiglotter/
Save, and make the file executable:
chmod +x ~/cron_s3_backup.sh
Finally, add it to the cron in the usual manner. Open the crontab for editing:
crontab -e
Add the following line for a daily backup at 07:00 in the morning.
0 7 * * * bash /home/craiglotter/cron_s3_backup.sh >/dev/null 2>&1
Done.
How to Solve the “linux-server : Depends: linux-image-server” apt-get upgrade Error Tips, Tricks and Tutorials 19 FEB 2016
After cleaning out an unexpected /boot full fault on one of the Ubuntu servers that I maintain, I next encountered an equally frustrating problem whereby apt-get stopped working thanks to a mismatched kernel version issue (introduced because of the upgrade that had failed halfway thanks to the earlier running out of disk space in /boot!).
Essentially, any apt-get operation like say “apt-get upgrade” or “apt-get install -f” would result in the operation breaking with the following string appearing in the error message:
dpkg: dependency problems prevent configuration of linux-server: linux-server depends on linux-image-server (= 3.2.0.92.106); however: Version of linux-image-server on system is 3.2.0.92.114. linux-server depends on linux-headers-server (= 3.2.0.92.106); however: Version of linux-headers-server on system is 3.2.0.92.114. dpkg: error processing linux-server (--configure): dependency problems - leaving unconfigured
I spent a lot of time bashing away at this, trying to remove and install specific kernel versions, etc. (a lot of information on the issue can be found here), but in the end stumbled across this silly little fix that worked brilliantly efficiently:
sudo apt-get remove linux-server && sudo apt-get install linux-server
Yeah. Classics still work. When in doubt, uninstall and then reinstall!
How to Solve Ubuntu Server /boot full Issue Tips, Tricks and Tutorials 19 FEB 2016
If you manage an older Ubuntu server and do a lot of updates/upgrades, inevitably you’ll hit the annoying /boot full issue, which is essentially what happens when the ridiculously tiny /boot folder gets filled up with older kernel version as a result of all your upgrading.
To spot if this is in fact what currently plagues you, simply run the following command to get a quick snapshot of disk space usage:
sudo df -h
If your /boot directory is indeed full or close to 100%, you can run the following command that automatically locates and removes older kernels and headers from the system:
sudo apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-z]+//')")
With those gone, you can now clean up a bit further by removing packages that aren’t necessarily needed any more (because they were automatically installed to satisfy dependencies for the newly removed packages):
sudo apt-get autoremove sudo apt-get --purge remove && sudo apt-get autoclean
This should solve your issue.
Note: If you can’t use apt-get at all because of this problem, you can try to manually delete the kernels yourself. Run the following to get an idea of what can be removed:
dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-z]+//')"
With that list now in your possession, craft a remove operation using those version numbers listed above, followed by an attempt to solve the new dependency issue with apt-get e.g.
sudo rm -rf /boot/*-3.2.0-{101,102,103}-* sudo apt-get -f install
(As always though, tread with care though when it comes to messing with this part of your server install…)
Ubuntu Server: Quick method to reset MySQL root user Password Tips, Tricks and Tutorials 09 JUL 2015
The chance of remembering your MySQL root user account password when coming back to an Ubuntu server after a long period of time is pretty much zero – after all, this is seldom a username/password combo that you actually use on a daily basis! Luckily, there is a particularly easy way to reset the root user password for an Ubuntu server…
We’ll simply reconfigure the MySQL package!
First, we need to check the version of the currently installed mysql-server.
apt-cache policy mysql-server
This should give us among all the returned information a line that starts with ‘Installed’. My server instance returned this:
Installed: 5.5.43-0ubuntu0.14.04.1
So I have 5.5 installed then. Next, we start the reconfiguration process with:
sudo dpkg-reconfigure mysql-server-*.*
*.* should obviously be replaced with your version, so using my example, I would have ran:
sudo dpkg-reconfigure mysql-server-5.5
This command should then stop the database daemon and a prompt will then appear where you need to enter your new root password (yay!) and then confirm the reconfiguration. On completion, your instance of mysql-server should automatically be started up again.
As simple as that. You should now be able to login as root using your newly set password:
mysql -u root -p
Nifty.
Ubuntu Server: Add User to Sudo Group Tips, Tricks and Tutorials 06 JUL 2015
Using Ubuntu server, how does one add an existing user to the sudo group (which is the “administrators” group in Ubuntu), thus granting that user pretty much full rights on the box?
Turns out that the answer is pretty much the same way you add a user to any other group in Ubuntu. If the user has already been created via the adduser command earlier (for argument’s sake, you named him bob), you simply now run the following command from the terminal:
sudo usermod -a -G sudo bob
Nifty.
(Note, if logged in, that user must first log off and then back in again for this change to take effect.)
How to Solve: a2ensite “Error, Site does not exist!” Tips, Tricks and Tutorials 04 JUL 2015
To enable a virtual vhost website on your Ubuntu Server running Apache, you need to first create a vhost configuration file (just copy 000-defaul.conf as a quick way to start), and then run the a2ensite command to enable it so that Apache knows about it.
However, nowadays attempting to run a2ensite might just pop up with this particular error message:
ERROR: Site [YOURSITE] does not exist!
This is not a problem that used to occur, but in newer versions of Ubuntu it does – the reason for this is simple – your configuration file is probably missing a .conf extension, and the a2ensite perl script now only works with filenames ending in .conf!
So to fix, simply add the .conf your site vhost configuration file and run a2ensite again.
sudo a2ensite YOURSITE.conf
This time around you should be all good, and can jump straight into the next step of reloading Apache’s configuration with:
sudo service apache2 reload
Nifty.
Ubuntu Server: How to See Group Members Tips, Tricks and Tutorials 12 JUN 2015
The scenario is this. Your file listing is showing you that a file belongs to a particular group, but now the next question that you have is what users are actually members of this group?
Well, getting this information via the command line terminal is pretty simple as it turns out – Just run the getent command:
getent group <GROUPNAME>
or
getent group <GROUPNAME> | awk -F: '{print $4}'
In case you aren’t familiar with getent:
“getent is a unix command that helps a user get entries in a number of important text files called databases. This includes the passwd and group databases which store user information – hence getent is a common way to look up user details on Unix. Since getent uses the same name service as the system, getent will show all information, including that gained from network information sources such as LDAP. The databases it searches in are: ahosts, ahostsv4, ahostsv6, aliases, ethers (Ethernet addresses), group, gshadow, hosts, netgroup, networks, passwd, protocols, rpc, services, and shadow.”
Easy peasy :)
Related Link: https://en.wikipedia.org/wiki/Getent