Skip to content

Linux Command Line Basics

No Linux administrator’s toolbox is complete without some knowledge of the Linux command line interface. In fact, many Linux servers don’t even have a GUI installed. In this article, we’ll look at the commands that a Linux server administrator will use to complete normal, day to day tasks such as adding and modifying users, process management, permissions management and network troubleshooting. These commands will be based on Red Hat type systems, including RHEL, Fedora and CentOS. Other distributions may be slightly different.

Working with Users

  • Adding a User
    The simplest way to add a user to a Linux system is with the useradd command.
    useradd foobar Adds a user named foobar, but probably wouldn’t do what you expected. It doesn’t give the user a home directory or a full name. To create the home directory, you must simply add the -m option. To set the full name, add the -c option. The final command would look like this:
    useradd -m -c "Foo Bar" foobar
  • Setting a Password
    You can set or change a user’s password with the passwd command. Usage looks like this:
    passwd foobar
  • Removing a User
    Use the userdel command to delete a user from the system. Add the -r option to also remove their home directory.
    userdel -r foobar

Managing Processes

  • List Processes
    List the running processes with the ps command. To get even more information, add the -ef options.
    ps -ef
  • List Processes Alternative
    Another way to list the processes is with the top command. However, if you’re going to use top, you might as well install htop.
    top or htop
  • Schedule Recurring Process
    Schedule a job to run at certain intervals with the crontab command. Use the -e option to edit the table, and the -l option to list the contents. The cron table has a very specific syntax which we won’t cover here. Use my Crontab Cheat Sheet to help you remember the syntax.
    crontab -e

Working with Permissions

  • Change Permissions
    The command to set and change permissions on a Linux system is chmod. For any file in Linux, and everything is a file, you can set three different permissions(Read, Write, eXecute) to three different Organizational Units (Owner, Group, Others)for a total of nine fields. The permissions are assigned with a three digit number, with the first of the digits setting the permissions for the owner, the second for the group and the third for others. Each field gets its value by adding the corresponding permissions for that group, for example:
    Read = 4
    Write = 2
    Execute = 1
    So, if you wanted to give the owner of the file permission to read, write and execute, but only give read and execute permission to everyone else, you would use the following syntax:
    chmod 755 filename
  • Change Ownership
    The command to change ownership of a file in Linux is chown. If you want to change the ownership of a directory and all of its contents, use the -R option.
    chown newowner filename
    chown -R newowner directory
    You can also specify a new group by adding the new group after the new owner:
    chown newowner:newgroup filename
    chown -R newowner:newgroup directory

Networking Commands

  • Check the IP Address or the MAC Address
    The ifconfig command will give you most of the information you need about your local interface.
    ifconfig
  • Set the IP Address and Netmask
    ifconfig netmask 255.255.255.0 192.168.0.251
  • Check the Routing Table
    route
  • Set the Default Gateway
    route add default gw 192.168.0.1
  • Check if a Host is Up.
    Use the ping command to test for connectivity. In Linux, ping runs until canceled with a CTRL-C unless you specify how many pings to send with the -c option.
    ping -c 4 LinuxBrainDump.org
  • Find the route to a host.
    Use the traceroute to get a list of each machine a packet passes through to get to its destination.
    traceroute LinuxBrainDump.org
  • Find Out Who Owns a Domain
    Use the whois command to get registration info for a domain.
    whois LinuxBrainDump.org
  • Find Out the IP Address for a Domain
    Use the nslookup command to get the IP address, or the dig command for a more detailed report.
    nslookup LinuxBrainDump.org
    dig LinuxBrainDump.org
  • Download a File
    Use the wget command to download a file to the current directory.
    wget http://site.tld/filename
  • The Networking Swiss Army Knife
    The netcat program is one of the most versatile troubleshooting tools on a Linux system. It’s called with nc on most systems. Its usage is worthy of its own article, so I won’t get into that here.

Miscellaneous Commands and Programs

  • Remote Sessions
    Use screen to create a virtual session on a remote machine. This lets you disconnect from the virtual session, logout, log in from another workstation and resume the virtual session without logging out of it. It’s usage will be detailed in a future article.

I know this list is far from complete. What are some of your must have command line tools? Tell me in the comments.

3 Comments

  1. francis

    Can i publish a copy of this article on my blog?

    Posted on 07-Aug-07 at 11:01 pm | Permalink
  2. James House

    Absolutely, just give me a by line and a link back to this site. :-)

    Posted on 08-Aug-07 at 11:45 am | Permalink
  3. edvard

    mc - When you absolutely, positively need a file manager with built-in text editor and you need it NOW…

    ssh - I don’t know how secure ’screen’ is, but when you need a clear line to a remote machine and don’t require a virtual session, ain’t none better.

    Posted on 11-Sep-07 at 1:35 am | Permalink

Post a Comment

Your email is never published nor shared.