Linux Administration Guide
This primarily discusses the options available to Ubuntu Linux server administrators. Have the operating system installed and then be logged in.
The "sudo" Command
The sudo command is used to run commands with super user rights. Alternatively, to prevent having to use sudo all the time login in as the root user by issuing a sudo -i or sudo su command. To logout of the root user account issue a exit command. exit again to logout the current user. Sometimes it is neccessary to be logged in as root.
On some Linux distributions, login as root with su -nd then enter the password for root. Now, run all the following commands in this guide without sudo.
Changing the Computer's Clock
To change the computer's clock, issue sudo hwclock --set --date "10/31/2017 24:00:00" and then to see the time it is set to hwclock --show. Notice: there is no hardware clock in the Raspberry Pi.
- To clear the screen simply issue the clear command.
- Issue a shutdown command with sudo shutdown -h now. Issue a restart command with sudo shutdown -r now or sudo reboot.
- Use the env command to show all enviroment variables.
- To copy a file issue a cp file1.txt file2.txt.
- To rename or move a file to another directory mv old_name.txt new_name.txt.
- For a directory listing issue a ls or dir command. Issue ls -al for more detail. Issue ls *.txt to list all text files.
- Change directory by issuing a cd directory command. To change to the subdirectory issue cd ...
- To figure out the current directory pwd.
- To create a directory mkdir dir_name.
- To delete a directory rmdir dir_name.
- To delete a file rm file.txt. Delete all files ending in .txt rm *.txt. Enter rm --help for more options.
- To create a user sudo adduser michelle then, if it does not ask for the new user's password, immediately assign a password sudo passwd michelle.
- Change a user's password with sudo chpasswd username:newpassword.
- To delete a user sudo deluser --remove-all-files --remove-home michelle.
- Issue a du command to show disk usage.
- Use find to search for files. find --help for instructions.
- Use chmod to modify file access permissions. chmod --help for instructions.
- Use chown to change the owner of a file/directory. chown --help for instructions.
- Use chgrp to change the group ownership of a file/directory. chgrp --help for instructions.
- Use man to display manual pages. man --help for instructions.
- top to show resource usage. q to quit this program.
- Issue a cat file.txt to show the contents of a file. Use less (covered below) to see large files.
- which tells the command that would be executed. Try which mv and which sudo.
- Compare two files for differences: diff file1.txt file2.txt.
- Compare two files for similarities: comm file1.txt file2.txt.
- Use the df command to see disk space usage.
- Issue a free command to determine free and used memory.
- Execute multiple commands seperating each by a semicolon, try: w; du; free; ls
- For a list of the processes and the process ID (PID) issue a ps command. To stop a process issue a sudo kill PID command
- The ping command checks that an network interface is functioning properly. Try ping -c 4 localhost or, if connected to the internet, ping -c 4 www.ubuntu.com.
- The traceroute command follows the route that IP packets take. Try traceroute www.ubuntu.com.
Using the Text Editor "nano"
Issue a sudo nano file.txt command to edit a text file or create a text file. There is a menu of options at the bottom of the screen.
The "ifconfig" Command
Use the ifconfig command to display all the networking interfaces installed in the computer including localhost (also known as lo). Change the IP address by issuing a sudo ifconfig eth0 192.168.1.2 command. Change the netmask by issuing a sudo ifconfig eth0 netmask 255.255.255.0 command. Change the broadcast by issuing a sudo ifconfig eth0 broadcast 192.168.1.255 command.
Setting the Nameserver
Use sudo nano /etc/resolv.conf to edit the IP addresses for the nameservers.
nameserver 192.168.1.1 nameserver 220.127.116.11
Static IP Addresses
Login as the root user and issue a nano /etc/network/interfaces command to edit the network interfaces to use a static IP address.
# The primary network interface auto eth0 iface eth0 inet static address 192.168.1.2 netmask 255.255.255.0 network 192.168.1.0 broadcast 192.168.1.255 gateway 192.168.1.1 dns-nameservers 192.168.1.1 dns-search lan
The "less" Command
Issue a dmesg > outputdmesg.txt command then issue less outputdmesg.txt. This will allow reading of the entire file as it is too big to fit on the screen. q to quit the program.
Compressing/Decompressing Files with "tar"
To view the contents of a compressed archive tar tzf archive.tgz or if it is a very large archive then tar tzf archive.tgz | less. To create an archive tar czf archivefilename.tgz directoryname. To decompress an archive tar zxf archive.tgz.
The "man" Command
The man command is used for reading online manuals. Execute man man to read the manual for man.
Using "cron" to Repeatedly Execute Jobs
The cron daemon executes a task frequently. Users listed in the cron.deny file aren't allowed to use cron and users listed in the cron.allow file are allowed. The root user can always set jobs. Learn more by issuing a man cron command.
To enable the firewall, issue a sudo ufw enable command. To enable a port through the firewall, issue a sudo ufw allow 587 command. To delete the same port, issue sudo ufw delete allow 587. Learn more by issuing a man ufw command.
Updating the Server
Issue sudo apt-get update, sudo apt-get upgrade -y and sudo apt-get dist-upgrade -y commands. This will update the server software (if it needs it).
Installing and uninstalling software is done with the sudo apt-get install and sudo apt-get remove commands. For example, to install MySQL database server issue the sudo apt-get install mysql-server command. To remove it sudo apt-get remove mysql-server.
Alternatively, a much simplier method is to use sudo tasksel and then simply check (with the space key) what it is that you want installed or uninstalled. If tasksel is it not available then install it with the command sudo apt-get install tasksel.
Install Apache web server using the sudo tasksel command. Use the LAMP install option. Or, issue a sudo apt-get install apache2 command. Check the version of Apache that is installed on your server with the command /usr/sbin/apache2 -v.
/etc/init.d/apache2 status shows the current status (running or not). sudo /etc/init.d/apache2 start will start Apache. sudo /etc/init.d/apache2 stop will stop Apache. sudo /etc/init.d/apache2 restart will restart Apache.
Configuration Files and Directories
- /etc/apache2/apache2.conf: The main configuration file. It contains the generic configuration for the Apache web server, such as where to find its configuration files (ServerRoot).
- /etc/apache2/httpd.conf: At first, this file is empty. Create additional configuration parameters here that are not in the /etc/apache2/apache2.conf file.
- /etc/apache2/ports.conf: This file contains the port numbers that the Apache server will listen on.
- /etc/apache2/envvars: Place environment variables in this file to tune the operation of the web server.
- /etc/apache2/conf.d/: Place additional configuration files in this directory.
- /etc/apache2/mods-available/: Extend the functionality of the web server by using the modules available in this directory.
- /etc/apache2/mods-enabled/: Enable a module by creating a symbolic link in this directory that refers to the module file in /etc/apache2/mods-available/. To do so, cd /etc/apache2/mods-enabled. To enable the cache_disk module, sudo ln -s ../mods-available/cache_disk.load cache_disk.load. Restart the web server.
- /etc/apache2/sites-available/: Stores all the configuration files for the web sites serviced by the web server.
- /etc/apache2/sites-enabled/: Contains symbolic links to the files in /etc/apache2/sites-available/. To create a symbolic link cd /etc/apache2/sites-enabled then sudo ln -s ../sites-available/website.conf website.conf (make sure that website.conf exists).
Install Samba server using the sudo tasksel command or a sudo apt-get install samba samba-common-bin command.
Share a directory by editing the configuration file with the sudo nano /etc/samba/smb.conf command. Page down to the #=== Share Definitions === section and add these lines:
[www] path=/var/www/html writable=yes browseable=yes only guest=no create mask=0777 directory mask=0777 public=yes guest ok=yes
Save the file and then issue a sudo chmod 0777 /var/www/html command to set file permissions. Restart the server with sudo service smbd restart and sudo service nmbd restart. Browse to the network on a Windows machine and the server should be listed with its new share, www, of the web server's home directory.
Now, learn how to install and setup a Ubuntu mail server.