CentOS 7 Linux Commands

CentOS LogoMy preferred Linux distro at the moment is CentOS 7. It is a community-supported distribution that follows the work that Red Hat does with Red Hat Enterprise Linux (RHEL). It is stable operating system for web servers and something I use every day. Unlike MacOS or Windows, which have lots of internet resources for support, Linux is a bit different. It inherits most of the UNIX terminology and documentation, and perhaps because of this, you will find it harder to research things. After a while, I started to make notes for myself and this post is the end result of some of that note taking. Many things in Linux are step oriented. For example, you should not install PHP, before you have Apache or some other web server installed. Below I have documented some of the steps I take after installing a bare minimum install of CentOS 7 without any GUI. I plan on revising this post in the future as I add or revise my post installation steps.

Note: It is assumed that you are aware that all administrative commands in Linux require root privileges, so I have left out the sudo part. Learn more about how to become root on the CentOS Wiki.

Post Installation Tasks:

1. Update System (Update YUM and Install Updates)

This will automatically update the system. The -y option will suppress any prompting to accept the changes.

yum -y update && yum -y upgrade
2. Enable Repositories

Before installing some packages in the next section, you will need to enable some repositories. The most common are EPEL, IUS, and Remi. Unlike Ubuntu, CentOS is a linux distro that caters to users interested in an enterprise platform. This means that CentOS chooses stability over newer updated software. The EPEL, IUS, and Remi repositories aim to bring newer versions of software to CentOS, without compromising the overall goal of stability. I leave it up to you to read about what repositories to enable and why. In this tutorial, we will enable EPEL and Remi.

Enable EPEL Repo:

To enable EPEL, just use the YUM command. If this command does not work, reference the EPEL Wiki for more information.

yum install epel-release

Enable Remi Repo:

The primary reason for enabling Remi is for testing out newer versions of PHP. You can reference the Remi site for more information, however if you primarily interested in PHP, it is better to use the Remi Configuration Wizard to learn about the various way you want to setup PHP. Note that PHP is also available through the IUS repository. For this tutorial we will install only PHP version 7.1 from Remi.

In order to install PHP from Remi, we must enable EPEL. We have already done this so we will skip the first step.

# EPEL already enabled #yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install yum-utils
yum-config-manager --enable remi-php71
yum update

To install additional packages:

yum install php-xxx

We then can verify the PHP Version and PHP Extensions installed:

php --version
php --modules
3. Programs and Applications to Install

The following commands, applications, packages are useful to install on a new system. You can use yum to install each of them. Skip down to the YUM Commands section to learn more about yum commands. In Linux most anything installed by yum is called an package, however most Windows users are familiar with the terms application or program. I use the term application interchangeably with package.

To install a specific application/package…

yum install package-name

YUM Utils

These are required in order to remove old kernels and use YUM Plugins

yum install -y yum-utils 
nano

Nano is an easy to use text editor for those of us who are not Vim or eMacs masters. I highly recommend using Nano if you are new to Linux.

net-tools

If you installed a minimal install of CentOS 7, the ifconfig command will be missing. Installing net-tools will install ifconfig. This is the equivalent of the ipconfig command in Windows.

nmap

Nmap is a utility for network exploration or security auditing. Once installed, use it to list all open ports and services.

nmap 127.0.0.1
rkhunter

Rootkit Hunter is an easy-to-use tool which checks computers running UNIX (clones) for the presence of rootkits and other unwanted tools.

rkhunter --check
telnet

Telnet is a popular protocol for logging into remote systems over the Internet. The package provides a command line Telnet client.

tree

The tree utility recursively displays the contents of directories in a tree-like format. Tree is basically a UNIX port of the DOS tree utility.

wget

GNU Wget is a file retrieval utility which can use either the HTTP or FTP protocols.

unzip

The unzip utility is used to list, test, or extract files from a zip archive.

zip

The zip program is a compression and file packaging utility.

4. CentOS VirtualBox Guest

If you are setting up CentOS as a guest operating system on VirtualBox, there are some additional steps outlined on the CentOS Wiki.

Directory Structure:

With the Tree command installed, you can get a good view of the system.

tree -C -L 1 /
  • /bin – command binaries (this is actually a symbolic link to usr/bin)
  • /boot – boot system
  • /dev – is for system devices
  • /etc – system configuration files
  • /home – user home directories
  • /lib – library for system binaries(this is actually a symbolic link to usr/lib)
  • /lib64 – (this is actually a symbolic link to usr/lib64)
  • /media – mount point for removable media
  • /opt – third-party software packages
  • /proc – system and process information
  • /root – root user home folder
  • /run – information about running processes
  • /sbin – system binaries (this is actually a symbolic link to usr/sbin)
  • /srv – files for various services
  • /sys – similar to /proc
  • /tmp – temporary files
  • /usr – another place for applications
  • /var – variables files, such as logs

For a more detailed explanation of the directory structure see LinuxInsider’s The Filesystem Hierarchy Standard.

YUM Commands:

Search for an application and description

yum search zip

Display information for an application

yum info zip

Install an application

yum install zip

Check if an application is installed

yum list zip

See all applications installed

yum list installed | less

Remove or Uninstall an Application

yum remove zip

Check What Needs Updating

yum check-update
yum list updates
yum list available

Update All

yum update

Yum Commands for Groups

yum grouplist
yum groupinfo "group name"
yum groupinstall "group name"
yum groupremove "group name"
yum groupupdate "group name"

Repositories are setup under:

/etc/yum.repos.d/

List Enabled Repositories

yum repolist

Display All Repositories (Both Enabled and Disabled)

yum repolist all

YUM Clean Commands

yum clean expire-cache
yum clean packages
yum clean headers
yum clean metadata
yum clean dbcache
yum clean rpmdb
yum clean plugins
yum clean all

Yum Plugins

yum info yum

Yum Delta RPMS

To save bandwidth Linux has a feature that allows you to download only partial changes instead of entire packages. Not all repositories utilize this and the minimal install of CentOS does not enable delta rpms. Enabling this feature is a trade off between CPU utilization versus bandwidth. Learn more about delta rpms.

yum install -y deltarpm

Disk Space:

Use the Tree command to list out directories. To see disk space use the df and du commands.

df -h
du -hd1 / | sort -h

Find Large Files

Use the find command to list out the largest 10 files.

find -type f -exec du -Sh {} + | sort -rh | head -n 10
find / -type f -exec du -Sh {} + | sort -rh | head -n 10
find /home/username -type f -exec du -Sh {} + | sort -rh | head -n 10
find /var/log -type f -exec du -Sh {} + | sort -rh | head -n 10

Find files larger than 100 Megabytes (ignore the false positives for /proc)

find / -size +100M

Find files created in the last day

afind -ctime -1 -ls

Run the YUM Clean command

yum clean all

Remove Old Kernels

This will remove all kernels, except the previous one. Note that if you recently installed a new kernel, you need to reboot the system first and then run this command. You can also increase the count to equal higher than 1 to keep more than the previous kernel file.

package-cleanup --oldkernels --count=1

Delete Old and Rotated Log Files

List out the log files first and then delete.

find /var/log -type f -regex ".*\.gz$"
find /var/log -type f -regex ".*\.gz$" -delete
find /var/log -type f -regex ".*\.[0-9]$"
find /var/log -type f -regex ".*\.[0-9]$" -delete

Additional Resources:

Filed under: Linux, Webmastering

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *

Comment *

Name *
Email *
Website