Linux FAQ
Is there a cheat sheet for these shell commands?
Yes, there are countless "Linux newbie" tutorials out there.
The following is a breakdown of the most commonly used commands and the most commonly used arguments for them.
- ls — Lists files and directories in a directory, comparable to dir in Windows®/DOS.
- ls -al — Shows all files (including those with filenames starting with a period), directories, and details for each file.
- cd — Changes the directory in which you are working.
- cd /usr/local/apache — Go to the
/usr/local/apache/ directory.
- cd ~ — Go to your home directory.
- cd - — Go to the last directory you were in.
- cd .. — Go "up" a directory (to the directory containing the current one).
- cat — Print file contents to the screen.
- cat filename.txt — cat the contents of
filename.txt to your screen.
- tail — Like cat, but only reads the end of the file.
- tail /var/log/messages — See the last 20 (by default) lines of
/var/log/messages
- tail -f /var/log/messages — Watch the file continuously, while it's being updated.
- tail -200 /var/log/messages — Print the last 200 lines of the file to the screen.
- more — Like cat, but opens the file one screen at a time, rather than all at once,
- more /etc/userdomains — Browse through the
userdomains file. Hit <space> to go to the next page, and <q> to quit.
- pico — Friendly, easy-to-use text editor for editing files. A clone of it is nano.
- pico /home/user1/public_html/index.html — Open the index page of the user's website for editing.
- vi — Another text editor, harder to use at first than pico, but much more powerful.
- vi /home/user1/public_html/index.html — Open the index page of the user's website for editing.
- grep — Looks for patterns in files.
- grep root /etc/passwd — Shows all matches of
root in the file /etc/passwd.
- grep -v root /etc/passwd — Shows all lines that do not match
root in the file /etc/passwd.
- touch — Creates an empty file.
- touch /home/user1/public_html/404.html — Creates an empty file called
404.html in the directory /home/user1/public_html/.
- ln — Creates links between files and directories.
- ln -s /usr/local/apache/conf/httpd.conf /etc/httpd.conf — This lets you edit
/etc/httpd.conf rather than the original (/usr/local/apache/conf/httpd.conf). Changes will affect the orginal; however, you can delete the link without deleting the original.
- rm — Deletes a file.
- rm filename.txt — Deletes the file
filename.txt, and will more than likely ask if you really want to delete it.
- rm -f filename.txt — Deletes the file
filename.txt, and will not ask for confirmation before deleting.
- rm -rf tmp/ — Recursively deletes the directory
tmp, and all files in it, including subdirectories.
Warning: Be VERY CAREFUL with this command!
- last — Shows who logged in and when.
- last -20 — Shows only the last 20 logins.
- last -20 -a — Shows the last 20 logins, with the hostname in the last field.
- w — Shows who is currently logged in and where they are logged in from.
- netstat — Shows all current network connections.
- netstat -an — Shows all connections to the server, including the source and destination IPs and ports.
- netstat -rn — Shows a routing table for all IPs bound to the server.
- file — Attempts to guess the file type by looking at its content.
- file * : prints out a list of all files and directories within a directory.
- du — Shows disk usage.
- du -sh — Shows a summary, in human-readable form, of total disk space used in the current directory, including subdirectories.
- du -sh * — Same as above, but for each file and directory. This is helpful when finding large files taking up space.
- wc — Displays word count for a file.
- wc -l filename.txt — Tells how many lines are in
filename.txt.
- cp — Copies a file.
- cp filename filename.backup — Copies the file
filename to filename.backup.
- cp -a /home/user1/new_design/* /home/burst/public_html/ — Copies all files, retaining permissions form one directory to another.
- chmod — Changes a file's permissions. Permissions determine who (from among the user, group, and world) can do what (read, write, and/or execute) to the file. For more information, refer to Wikipedia's definition of chmod, which contains a list of examples and links to additional resources.
- chmod 755 myfile.txt — Allows the user to read, write, and execute
myfile.txt. Allows the group and world to read and execute myfile.txt.
- chown — Changes the owner of a file.
- chown joe:joe myfile.txt — Sets both user and group ownership for
myfile.txt to joe.
Putting commands together
Often, you will find that you need to use different commands on the same line. Here are some examples.
Note that the
| character is called a pipe, it takes data from one program and "pipes" it to another.
> means create a new file, overwriting any content already there.
>> means to append data to a file, creating a new one if it does not already exist.
< send input from a file back into a command.
- # grep User /usr/local/apache/conf/httpd.conf |more
- This will find all lines that match User from the
httpd.conf, then print the results to your screen one page at a time.
- last -a > /root/lastlogins.tmp
- This will print all the current login history to a file called
lastlogins.tmp in /root/.
- tail -10000 /var/log/exim_mainlog |grep domain\.com |more
- This will grab the last 10,000 lines from /var/log/exim_mainlog, and find all occurrences of
domain.com (the period normally represents 'anything' — comment it out with a \ so it will be interpreted literally), then send it to your screen page by page.
- netstat -an |grep :80 |wc -l
- Shows how many active connections there are to Apache (
httpd runs on port 80).
- mysql --skip-column-names --batch -e 'show processlist' | wc -l
- This command shows the number of MySQL threads. If subselects start new threads, they will be included in the output of
'show processlist'.
And that is not even scratching the surface! For more information on using Linux and the command line, we have selected some of the following links. Please keep in mind that cPanel, Inc. is not responsible for content on other sites.
Where do I find things?
If you are trying to find a program, log into the shell. Typing
whereis program (where "program" is the program you are looking for) will show you one or more matches for what you are looking for.
If you have more than one copy of a program, say one in
/usr/bin and another in
/usr/local/bin/, typing
which program will show you which one will be called first. If you can't find it at all, typing
locate program will show you every file that matches "program." Some of the most common programs are:
- /usr/sbin/sendmail
- /usr/bin/perl
- /bin/mail
- /usr/bin/php
Default locations for commonly used configuration files and directories
Exim
- /etc/exim.conf
- /var/log/exim_mainlog
- /var/log/exim_rejectlog
- /etc/valiases/
- /etc/vfilters/
- /home/username/.forward
MySQL
- /root/.my.cnf
- /etc/my.cnf
- /var/lib/mysql/
Apache
- /usr/local/apache/conf/httpd.conf
- /usr/local/apache/domlogs/
System
- /var/log/messages
- /var/log/dmesg
Proftpd
- /etc/proftpd.conf
- /var/log/xferlog
- /etc/proftpd/
sshd
Which ports should be open if I'm running my cPanel server behind a firewall?
Ports that must be open to run cPanel behind a firewall:
- 20 — FTP TCP inbound/outbound
- 21 — FTP TCP, UDP inbound/outbound
- 22 — SSH TCP inbound
- 25 — SMTP TCP inbound/outbound
- 26 — SMTP TCP inbound/outbound
- 37 — rdate TCP outbound
- 43 — whois TCP outbound
- 53 — DNS TCP/UDP inbound/outbound
- (inbound is only needed if you run your own public DNS server)
- 80 — HTTP TCP inbound/outbound
- 110 — POP3 TCP inbound
- 113 — ident TCP outbound
- 143 — IMAP4 TCP inbound
- 443 — HTTPS TCP inbound
- 465 — SMTP TLS/SSL TCP/UDP inbound/outbound
- 783 — SpamAssassin TCP/UDP inbound
- 873 — rsync TCP/UDP outbound
- 993 — IMAP4 SSL TCP inbound
- 995 — POP3 SSL TCP inbound
- 2077 — WebDAV TCP/UDP inbound/outbound
- 2078 — WebDAV SSL TCP/UDP inbound/outbound
- 2082 — cPanel TCP inbound
- 2083 — cPanel SSL TCP inbound
- 2086 — WHM TCP inbound
- 2087 — WHM SSL TCP inbound
- 2089 — cPanel license TCP outbound
- 2095 — Webmail TCP inbound
- 2096 — Webmail SSL TCP inbound
- 3306 — MySQL TCP (only if you need to connect remotely)
- 6666 — Chat TCP inbound
(Thanks to cyberspirit on cPanel Forums.)
How do I find all SETUID root files on my server?
This will show all SETUID and SETGID files on the server (assuming a good integrity of the "find" binary):
- find / -type f \( -perm -04000 -o -perm -02000 \) \-exec ls -lg {} \;
Can I use Linux on any computer?
While Linux is a general term for many operating systems, most hardware is supported by most distributions/operating systems. You can find more information at your specific operating system's website.
How do I keep my crontab from sending an email every time it runs?
Add the following to the crontab after
0 * * * * /home/uesr/backup.pl >/dev/null 2>&1:
How can I see what is on the console screen of my Linux server?
Run the following command:
I use Windows. How can I log into my server?
You will need to download an SSH client. We recommend one called PuTTY, which is free and may be downloaded from
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html.
The actual SSH client is a single binary (not an installer):
http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe.
For the installer, download the installer package.
How can I see all running processes from the shell?
Use either the
ps -auxww or
ps -cef command.
For more information use about the
ps command, type the
man ps command.
What is a quick way to see all users with the UID of 0 on the shell?
Execute the following:
How can I see the environment variables a process is running with?
Run the following command:
- ps auxwwe |grep processname
How can I see a list of who has logged into the server?
Use the
last command. Example:
- last -30 — This will displaly the last 30 people who have logged in.
Why do I see the following errors on my FreeBSD server? kvm_nlist failed swapinfo: undefined symbol: _numvnodes
Most likely you have
/boot on its own partition. On FreeBSD,
/boot should be on the root (
/) partition; otherwise, it will not be able to use
/boot/loader(8) and force the system to fall back directly on the
/kernel.
To fix this, you need to copy the data out of
/boot/ to a directory on
/, unmount
/boot, remove it from
/etc/fstab, then move the contents that were in
/boot to the (now plain directory)
/boot/ . Once you reboot, it should be working normally.
I'm running BIND 9 on FreeBSD, but I can't get rndc to work. It's not listening on the command channel (port 953). How do I fix this?
Make sure the following is present in your
/etc/make.conf file:
BATCH=YES
OPENSSL_OVERWRITE_BASE=YES
WITHOUT_X11=YES
SKIP_DNS_CHECK=YES
CRYPT_DES=0
PORT_REPLACES_BASE_BIND8=YES
PORT_REPLACES_BASE_BIND9=YES
WITHOUT_ALT_CONFIG_PREFIX=YES
- Update your ports collection:
- Change directory to /usr/ports/dns/bind9
- Execute the following command:
- Run /scripts/upcp
- Run /scripts/fixndc
- Stop all named processes:
- /etc/rc.d/init.d/named stop; killall -9 named
- Restart named.
It should now be listening on the control channel.
Can I change root’s default shell?
cPanel requires the root user to use the bash shell by default on 11.32+. Non-root users can use shells other than bash if the systems administrator wishes to make this an option.