Profile photo of Travis Horn Travis Horn

Unix Commands

2016-07-28
Unix Commands

This post is part of a project to move my old reference material to my blog. Before 2012, when I accessed the same pieces of code or general information multiple times, I would write a quick HTML page for my own reference and put it on a personal site. Later, I published these pages online. Some of the pages still get used and now I want to make them available on my blog.

This is a short list of frequently-used commands for OpenBSD. Many of them are common with most Unix-like platforms. The text is broken into sections designated by the titles in larger print.

Logging Out and Shutting Down

Log current user out. Displays login prompt for another user:

exit

Shut down the operating system. Same as typing shutdown -h now:

halt

Reboot the operating system. Same as typing shutdown -r now:

reboot

Directory Management

Output what directory you’re currently in:

pwd

Change directory to x:

cd x

Same as cd, but saves the directory you were in when you executed the command. Type popd to return to the saved directory:

pushd x

Make a new directory called x:

mkdir x

Remove directory x:

rmdir x

Remove directory x and all files and directories within:

rm -r x

List files and directories in current directory. Use -l to get details, -a to get hidden files, -F to id executables, files, and directories:

ls

Search all directories for x:

find / -name x

Mount a Windows share called sharename on the server called servername. Replace username and password with appropriate credentials:

mkdir /mnt/sharename
sudo mount -f cifs //servername/sharename -o username=username,password=password /mnt/sharename

File Management

Remove file x:

rm x

Remove directory x and all files and directories within:

rm -r x

Copy x.txt and name the copy y.txt:

cp x.txt y.txt

Move (rename) x.txt to y.txt:

mv x.txt y.txt

Display disk usage. The -h switch makes numbers human-friendly:

df -h

Disk Management

Adds the disk wd0 (IDE primary master. wd1 would be IDE primary slave, etc.) partition a to directory called files. files must already exist:

mount /wd0a files

Unmount the device mounted to the directory files:

umount files

Miscellaneous

Unix manual. Type man x for a manual on a specific command:

man

Filter that will pause and wait for user input when the command teakes up more than one screen:

x | more

Elevate access to root privileges:

su

See resources each process is using. Use | head -5 to just get the top 5 lines of the command:

ps -aux

Send email to y (email address) with subject x. Type your message and press Ctrl + D:

mail -s "x" y

Set an alias so when you type x, the command y executes

alias x='y'

Text Viewing and Editing

Display contents of file x on screen:

cat x

Open a file full-screen and provide file editing utilities. Exit by hitting Esc, type :wq to save and exit or :q! to exit without saving. Learn more about vi:

vi

User Management

Create a new user. Accept defaults for most prompts. Enter username and password when prompted. If you want user to have access to su, type wheel when prompted for what groups to invite user to:

adduser

Add existing user x to the sudoers list, giving them access to sudo command:

echo ‘x ALL=(ALL) ALL' >> /etc/sudoers

Remove an existing user:

rmuser

List existing users:

cat /etc/passwd | cut -d: -f1 | grep -v \#

See who you’re logged in as:

id

See what users are currently logged in and what they’re doing:

w

Change the default shell to ksh for user x:

chsh -s /bin/ksh x

Archiving

Combine all files in directory y and write them to a file called x.tgz. Will be written back as a directory when extracted:

tar cvzf x.tgz y

Display the contents of x.tgz:

tar tzf x.tgz

Extract contents of x.tgz in current directory:

tar xvzf x.tgz

Apache Control

Start Apache web server:

apachectl start

Stop Apache

apachectl stop

Adding Software

Sets the package path. This is the location new packages will be downloaded from:

export PKG_PATH=ftp://ftp.openbsd.org/pub/OpenBSD/4.7/packages/amd64

Install package x:

pkg_add -r -v x

SSH

Connect to an SSH server as x at location y:

ssh x@y

Generate private and public keys to use with public key authentication. You may leave default filename but enter a long passphrase (16+ characters).

ssh-keygen

After generating keys, append ~/.ssh/id_rsa.pub to server’s ~/.ssh/authorized_keys file using the command below.

Allow client with public key x to connect using public key authentication. x can be found in client’s ~/.ssh/id_rsa.pub file after running command above.

cat x >> ~/.ssh/authorized_keys

Restart SSH daemon after changing config file:

kill -HUP `cat /var/run/sshd.pid`

Boot a remote user off SSH. x is the PID for their sshd process. The PID can be found by running ps -aux and finding the COMMAND labeled sshd: username:

kill x

Adding a new hard drive disk

Cover photo by Torkild Retvedt under CC BY-SA 2.0.

Here are some more articles you might like: